WinslowFan Claude Opus 5.5 commited on
Commit
8bfba1c
·
1 Parent(s): 5e39175

Wait for HF's parent entry before adding the iframe entry

Browse files
Files changed (1) hide show
  1. frontend/src/lib/hfSpace.ts +28 -9
frontend/src/lib/hfSpace.ts CHANGED
@@ -14,7 +14,9 @@
14
  *
15
  * Back from I(B) lands on P(B), where the iframe is back at I(A): the iframe
16
  * sees a popstate, shows A, and steps back once more over P(B) so the address
17
- * bar shows A too. One press, both in step. Forward first reaches P(B) (address
 
 
18
  * bar only) and then I(B) (page), so it takes two presses inside the Space page.
19
  * Opened directly at omaib-musprot.hf.space the app uses the normal history.
20
  *
@@ -26,8 +28,8 @@ import { useLocation, useNavigate } from 'react-router-dom';
26
 
27
  const HF_ORIGIN = 'https://huggingface.co';
28
  const PARAM = 'p';
29
- /** time for HF to push the parent entry before the iframe pushes its own */
30
- const PARENT_FIRST_MS = 120;
31
  /** a page shown for less than this (an immediate redirect) gets no history entry */
32
  const SETTLE_MS = 150;
33
 
@@ -73,6 +75,18 @@ export function openDeepLink(): void {
73
  if (target) window.history.replaceState(null, '', target);
74
  }
75
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  function queryFor(path: string): string {
77
  // keep slashes readable: ?p=/chain/7ua3/I
78
  return path === '/' ? '' : `${PARAM}=${encodeURIComponent(path).replace(/%2F/g, '/')}`;
@@ -86,6 +100,7 @@ export function HfSpaceSync() {
86
  const stack = useRef<string[]>([path]); // page shown at each iframe entry
87
  const index = useRef(0);
88
  const fromHistory = useRef(true); // the first page is already in the parent URL
 
89
 
90
  // react-router hands out a new navigate() on every page change; the listener
91
  // below is set up once, so it reads the latest one through a ref
@@ -117,12 +132,16 @@ export function HfSpaceSync() {
117
  // wait until the page settles (redirects such as /chain/3vgk.A -> /chain/3vgk/A
118
  // replace it at once), then record it: parent entry first, then ours
119
  const settle = window.setTimeout(() => {
120
- if (path === stack.current[index.current]) return;
121
- window.parent.postMessage({ queryString: queryFor(path), hash: '' }, HF_ORIGIN);
122
- const k = index.current + 1;
123
- stack.current = [...stack.current.slice(0, k), path];
124
- index.current = k;
125
- window.setTimeout(() => window.history.pushState({ musprot: k }, ''), PARENT_FIRST_MS);
 
 
 
 
126
  }, SETTLE_MS);
127
  return () => window.clearTimeout(settle);
128
  }, [path]);
 
14
  *
15
  * Back from I(B) lands on P(B), where the iframe is back at I(A): the iframe
16
  * sees a popstate, shows A, and steps back once more over P(B) so the address
17
+ * bar shows A too. One press, both in step. HF is slow to push (~0.45 s on the
18
+ * live page), so the iframe waits until the joint history.length changes,
19
+ * i.e. the parent entry exists, before pushing its own. Forward first reaches P(B) (address
20
  * bar only) and then I(B) (page), so it takes two presses inside the Space page.
21
  * Opened directly at omaib-musprot.hf.space the app uses the normal history.
22
  *
 
28
 
29
  const HF_ORIGIN = 'https://huggingface.co';
30
  const PARAM = 'p';
31
+ /** longest wait for HF to push the parent entry (it took ~0.45 s on the live page) */
32
+ const PARENT_WAIT_MS = 2000;
33
  /** a page shown for less than this (an immediate redirect) gets no history entry */
34
  const SETTLE_MS = 150;
35
 
 
75
  if (target) window.history.replaceState(null, '', target);
76
  }
77
 
78
+ /** Resolves once the joint session history changes length (the parent pushed), or after a timeout. */
79
+ function parentPushed(before: number): Promise<void> {
80
+ return new Promise((resolve) => {
81
+ const t0 = Date.now();
82
+ const poll = () => {
83
+ if (window.history.length !== before || Date.now() - t0 > PARENT_WAIT_MS) resolve();
84
+ else window.setTimeout(poll, 25);
85
+ };
86
+ poll();
87
+ });
88
+ }
89
+
90
  function queryFor(path: string): string {
91
  // keep slashes readable: ?p=/chain/7ua3/I
92
  return path === '/' ? '' : `${PARAM}=${encodeURIComponent(path).replace(/%2F/g, '/')}`;
 
100
  const stack = useRef<string[]>([path]); // page shown at each iframe entry
101
  const index = useRef(0);
102
  const fromHistory = useRef(true); // the first page is already in the parent URL
103
+ const queue = useRef<Promise<void>>(Promise.resolve()); // one parent+iframe pair at a time
104
 
105
  // react-router hands out a new navigate() on every page change; the listener
106
  // below is set up once, so it reads the latest one through a ref
 
132
  // wait until the page settles (redirects such as /chain/3vgk.A -> /chain/3vgk/A
133
  // replace it at once), then record it: parent entry first, then ours
134
  const settle = window.setTimeout(() => {
135
+ queue.current = queue.current.then(async () => {
136
+ if (path === stack.current[index.current]) return;
137
+ const k = index.current + 1;
138
+ stack.current = [...stack.current.slice(0, k), path];
139
+ index.current = k;
140
+ const before = window.history.length;
141
+ window.parent.postMessage({ queryString: queryFor(path), hash: '' }, HF_ORIGIN);
142
+ await parentPushed(before);
143
+ window.history.pushState({ musprot: k }, '');
144
+ });
145
  }, SETTLE_MS);
146
  return () => window.clearTimeout(settle);
147
  }, [path]);