Skip to content

Commit 42c3a97

Browse files
committed
Fix initial page for full-site CSN
1 parent 116aad9 commit 42c3a97

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

packages/interactivity-router/src/index.ts

+24-20
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
parseServerData,
1818
populateServerData,
1919
batch,
20+
initDone,
2021
} = privateApis(
2122
'I acknowledge that using private APIs means my theme or plugin will inevitably break in the next version of WordPress.'
2223
);
@@ -166,27 +167,30 @@ window.addEventListener( 'popstate', async () => {
166167
}
167168
} );
168169

169-
// Initialize the router and cache the initial page using the initial vDOM.
170-
// Once this code is tested and more mature, the head should be updated for
171-
// region based navigation as well.
172-
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
173-
if ( navigationMode === 'fullPage' ) {
174-
// Cache the scripts. Has to be called before fetching the assets.
175-
[].map.call(
176-
document.querySelectorAll( 'script[type="module"][src]' ),
177-
( script ) => {
178-
headElements.set( script.getAttribute( 'src' ), {
179-
tag: script,
180-
} );
181-
}
182-
);
183-
await fetchHeadAssets( document );
170+
( async () => {
171+
await initDone;
172+
// Initialize the router and cache the initial page using the initial vDOM.
173+
// Once this code is tested and more mature, the head should be updated for
174+
// region based navigation as well.
175+
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
176+
if ( navigationMode === 'fullPage' ) {
177+
// Cache the scripts. Has to be called before fetching the assets.
178+
[].map.call(
179+
document.querySelectorAll( 'script[type="module"][src]' ),
180+
( script ) => {
181+
headElements.set( script.getAttribute( 'src' ), {
182+
tag: script,
183+
} );
184+
}
185+
);
186+
await fetchHeadAssets( document );
187+
}
184188
}
185-
}
186-
pages.set(
187-
getPagePath( window.location.href ),
188-
Promise.resolve( regionsToVdom( document, { vdom: initialVdom } ) )
189-
);
189+
pages.set(
190+
getPagePath( window.location.href ),
191+
Promise.resolve( regionsToVdom( document, { vdom: initialVdom } ) )
192+
);
193+
} )();
190194

191195
// Check if the link is valid for client-side navigation.
192196
const isValidLink = ( ref: HTMLAnchorElement ) =>

packages/interactivity/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ export const privateApis = ( lock ): any => {
5050
parseServerData,
5151
populateServerData,
5252
batch,
53+
initDone,
5354
};
5455
}
5556

5657
throw new Error( 'Forbidden access.' );
5758
};
5859

5960
registerDirectives();
60-
init();
61+
const initDone = init();

0 commit comments

Comments
 (0)