Skip to content

Commit

Permalink
Delay block hydration to allow stores to initialize (#66772)
Browse files Browse the repository at this point in the history
Co-authored-by: westonruter <[email protected]>
Co-authored-by: michalczaplinski <[email protected]>
Co-authored-by: annezazu <[email protected]>
Co-authored-by: artemiomorales <[email protected]>
Co-authored-by: cbravobernal <[email protected]>
Co-authored-by: t-hamano <[email protected]>
  • Loading branch information
7 people committed Nov 6, 2024
1 parent 04d57ce commit f4b013f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/interactivity/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ export const init = async () => {
`[data-${ directivePrefix }-interactive]`
);

/*
* This `await` with setTimeout is required to apparently ensure that the interactive blocks have their stores
* fully initialized prior to hydrating the blocks. If this is not present, then an error occurs, for example:
* > view.js:46 Uncaught (in promise) ReferenceError: Cannot access 'state' before initialization
* This occurs when splitTask() is implemented with scheduler.yield() as opposed to setTimeout(), as with the former
* split tasks are added to the front of the task queue whereas with the latter they are added to the end of the queue.
*/
await new Promise( ( resolve ) => {
setTimeout( resolve, 0 );
} );

for ( const node of nodes ) {
if ( ! hydratedIslands.has( node ) ) {
await splitTask();
Expand Down
2 changes: 1 addition & 1 deletion packages/interactivity/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const afterNextFrame = ( callback: () => void ) => {
/**
* Returns a promise that resolves after yielding to main.
*
* @return Promise
* @return Promise<void>
*/
export const splitTask =
typeof window.scheduler?.yield === 'function'
Expand Down

0 comments on commit f4b013f

Please sign in to comment.