From 8bb51f5b4c4becd2ad93172b977e28a24cfdf84e Mon Sep 17 00:00:00 2001 From: David Date: Tue, 1 Apr 2025 13:52:28 +0200 Subject: [PATCH 1/6] Add a failing test --- .../specs/interactivity/router-styles.spec.ts | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/test/e2e/specs/interactivity/router-styles.spec.ts b/test/e2e/specs/interactivity/router-styles.spec.ts index 9c6864909ea2a7..1539ef2b3f0445 100644 --- a/test/e2e/specs/interactivity/router-styles.spec.ts +++ b/test/e2e/specs/interactivity/router-styles.spec.ts @@ -310,4 +310,58 @@ test.describe( 'Router styles', () => { await expect( blue ).toHaveCSS( 'color', COLOR_WRAPPER ); await expect( all ).toHaveCSS( 'color', COLOR_WRAPPER ); } ); + + test( 'should not cause race conditions during render', async ( { + page, + } ) => { + // Setup a route handler to hold requests to the red stylesheet + // until manually resolved. + let resolveStyle: ( value?: unknown ) => void; + const linkPattern = '**/router-styles-red/style-from-link.css*'; + await page.route( linkPattern, async ( route ) => { + await new Promise( ( resolve ) => ( resolveStyle = resolve ) ); + await route.continue(); + } ); + + const csn = page.getByTestId( 'client-side navigation' ); + const red = page.getByTestId( 'red-from-inline' ); + const green = page.getByTestId( 'green-from-inline' ); + const blue = page.getByTestId( 'blue-from-inline' ); + const all = page.getByTestId( 'all-from-inline' ); + const prefetching = page.getByTestId( 'prefetching' ); + + await expect( red ).toHaveCSS( 'color', COLOR_WRAPPER ); + await expect( green ).toHaveCSS( 'color', COLOR_WRAPPER ); + await expect( blue ).toHaveCSS( 'color', COLOR_WRAPPER ); + await expect( all ).toHaveCSS( 'color', COLOR_WRAPPER ); + + await page.getByTestId( 'link red' ).hover(); + + // Wait until the prefetching has finished. + await expect( prefetching ).toHaveText( 'false' ); + + await page.getByTestId( 'link red' ).click(); + + // The red style is not ready yet; colors should stay the same. + await expect( red ).toHaveCSS( 'color', COLOR_WRAPPER ); + await expect( green ).toHaveCSS( 'color', COLOR_WRAPPER ); + await expect( blue ).toHaveCSS( 'color', COLOR_WRAPPER ); + await expect( all ).toHaveCSS( 'color', COLOR_WRAPPER ); + + await page.getByTestId( 'link green' ).click(); + + await expect( csn ).toBeVisible(); + await expect( red ).toHaveCSS( 'color', COLOR_WRAPPER ); + await expect( green ).toHaveCSS( 'color', COLOR_GREEN ); + await expect( blue ).toHaveCSS( 'color', COLOR_WRAPPER ); + await expect( all ).toHaveCSS( 'color', COLOR_GREEN ); + + resolveStyle!(); + + await expect( csn ).toBeVisible(); + await expect( red ).toHaveCSS( 'color', COLOR_WRAPPER ); + await expect( green ).toHaveCSS( 'color', COLOR_GREEN ); + await expect( blue ).toHaveCSS( 'color', COLOR_WRAPPER ); + await expect( all ).toHaveCSS( 'color', COLOR_GREEN ); + } ); } ); From 64dc533d1d51bbec37c0fe1e8fb9065dddb429f5 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 1 Apr 2025 13:53:54 +0200 Subject: [PATCH 2/6] Only enable/disable sheets that are ready --- packages/interactivity-router/src/assets/styles.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/interactivity-router/src/assets/styles.ts b/packages/interactivity-router/src/assets/styles.ts index 8eebcd6da4f9f4..0e087c3630fa8b 100644 --- a/packages/interactivity-router/src/assets/styles.ts +++ b/packages/interactivity-router/src/assets/styles.ts @@ -259,12 +259,14 @@ export const applyStyles = ( styles: StyleElement[] ) => { window.document .querySelectorAll( 'style,link[rel=stylesheet]' ) .forEach( ( el: HTMLLinkElement | HTMLStyleElement ) => { - if ( styles.includes( el ) ) { - const { originalMedia = 'all' } = el.dataset; - el.sheet.media.mediaText = originalMedia; - el.sheet.disabled = false; - } else { - el.sheet.disabled = true; + if ( el.sheet ) { + if ( styles.includes( el ) ) { + const { originalMedia = 'all' } = el.dataset; + el.sheet.media.mediaText = originalMedia; + el.sheet.disabled = false; + } else { + el.sheet.disabled = true; + } } } ); }; From 49931cfd96d0c89ef58c79c0990336a3009b8578 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 1 Apr 2025 14:32:00 +0200 Subject: [PATCH 3/6] Improve failing test --- .../specs/interactivity/router-styles.spec.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/test/e2e/specs/interactivity/router-styles.spec.ts b/test/e2e/specs/interactivity/router-styles.spec.ts index 1539ef2b3f0445..6e7d09df27b0d0 100644 --- a/test/e2e/specs/interactivity/router-styles.spec.ts +++ b/test/e2e/specs/interactivity/router-styles.spec.ts @@ -314,11 +314,19 @@ test.describe( 'Router styles', () => { test( 'should not cause race conditions during render', async ( { page, } ) => { - // Setup a route handler to hold requests to the red stylesheet - // until manually resolved. + let requestStyle: ( value?: unknown ) => void; let resolveStyle: ( value?: unknown ) => void; + + // Promise that will resolve once the target style is intercepted. + // See the `page.route()` below. + const styleHasBeenRequested = new Promise( + ( resolve ) => ( requestStyle = resolve ) + ); + + // Setup a route handler to intercept styles. const linkPattern = '**/router-styles-red/style-from-link.css*'; await page.route( linkPattern, async ( route ) => { + requestStyle(); await new Promise( ( resolve ) => ( resolveStyle = resolve ) ); await route.continue(); } ); @@ -328,7 +336,6 @@ test.describe( 'Router styles', () => { const green = page.getByTestId( 'green-from-inline' ); const blue = page.getByTestId( 'blue-from-inline' ); const all = page.getByTestId( 'all-from-inline' ); - const prefetching = page.getByTestId( 'prefetching' ); await expect( red ).toHaveCSS( 'color', COLOR_WRAPPER ); await expect( green ).toHaveCSS( 'color', COLOR_WRAPPER ); @@ -337,8 +344,8 @@ test.describe( 'Router styles', () => { await page.getByTestId( 'link red' ).hover(); - // Wait until the prefetching has finished. - await expect( prefetching ).toHaveText( 'false' ); + // Wait until the style has been requested. + await styleHasBeenRequested; await page.getByTestId( 'link red' ).click(); @@ -356,8 +363,10 @@ test.describe( 'Router styles', () => { await expect( blue ).toHaveCSS( 'color', COLOR_WRAPPER ); await expect( all ).toHaveCSS( 'color', COLOR_GREEN ); + // Resolve the requested style. resolveStyle!(); + // Styles should not change. await expect( csn ).toBeVisible(); await expect( red ).toHaveCSS( 'color', COLOR_WRAPPER ); await expect( green ).toHaveCSS( 'color', COLOR_GREEN ); From 88334745521371c6d52c12d7512f4548cd3ea8e8 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 1 Apr 2025 14:36:42 +0200 Subject: [PATCH 4/6] Move styles and module preload to `regionsToVdom` --- packages/interactivity-router/src/index.ts | 47 ++++++++++++---------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/packages/interactivity-router/src/index.ts b/packages/interactivity-router/src/index.ts index 34a906be5beecd..c6bef781386199 100644 --- a/packages/interactivity-router/src/index.ts +++ b/packages/interactivity-router/src/index.ts @@ -46,13 +46,13 @@ interface VdomParams { interface Page { url: string; regions: Record< string, any >; - styles: Promise< StyleElement >[]; + styles: StyleElement[]; scriptModules: string[]; title: string; initialData: any; } -type RegionsToVdom = ( dom: Document, params?: VdomParams ) => Page; +type RegionsToVdom = ( dom: Document, params?: VdomParams ) => Promise< Page >; // Check if the navigation mode is full page or region based. const navigationMode: 'regionBased' | 'fullPage' = @@ -87,14 +87,8 @@ const fetchPage = async ( url: string, { html }: { html: string } ) => { // Return an object with VDOM trees of those HTML regions marked with a // `router-region` directive. -const regionsToVdom: RegionsToVdom = ( dom, { vdom, url } = {} ) => { +const regionsToVdom: RegionsToVdom = async ( dom, { vdom, url } = {} ) => { const regions = { body: undefined }; - const styles = prepareStyles( dom, url ); - const scriptModules = [ - ...dom.querySelectorAll< HTMLScriptElement >( - 'script[type=module][src]' - ), - ].map( ( s ) => s.src ); if ( globalThis.IS_GUTENBERG_PLUGIN ) { if ( navigationMode === 'fullPage' ) { @@ -115,21 +109,30 @@ const regionsToVdom: RegionsToVdom = ( dom, { vdom, url } = {} ) => { } const title = dom.querySelector( 'title' )?.innerText; const initialData = parseServerData( dom ); - return { regions, styles, scriptModules, title, initialData, url }; -}; -// Render all interactive regions contained in the given page. -const renderRegions = async ( page: Page ) => { + // Get module URLs. + const scriptModules = [ + ...dom.querySelectorAll< HTMLScriptElement >( + 'script[type=module][src]' + ), + ].map( ( s ) => s.src ); + // Wait for styles and modules to be ready. - await Promise.all( [ - ...page.styles, - ...page.scriptModules.map( - ( src ) => import( /* webpackIgnore: true */ src ) + const [ styles ] = await Promise.all( [ + Promise.all( prepareStyles( dom, url ) ), + Promise.all( + scriptModules.map( + ( src ) => import( /* webpackIgnore: true */ src ) + ) ), ] ); - // Replace style sheets. - const styles = await Promise.all( page.styles ); - applyStyles( styles ); + + return { regions, styles, scriptModules, title, initialData, url }; +}; + +// Render all interactive regions contained in the given page. +const renderRegions = ( page: Page ) => { + applyStyles( page.styles ); if ( globalThis.IS_GUTENBERG_PLUGIN ) { if ( navigationMode === 'fullPage' ) { @@ -181,7 +184,7 @@ window.addEventListener( 'popstate', async () => { const pagePath = getPagePath( window.location.href ); // Remove hash. const page = pages.has( pagePath ) && ( await pages.get( pagePath ) ); if ( page ) { - await renderRegions( page ); + renderRegions( page ); // Update the URL in the state. state.url = window.location.href; } else { @@ -336,7 +339,7 @@ export const { state, actions } = store< Store >( 'core/router', { ?.clientNavigationDisabled ) { try { - yield renderRegions( page ); + renderRegions( page ); } catch ( e ) { // eslint-disable-next-line no-console console.warn( e ); From 894779b6144bdc573f2ebbd8bd919cfba1ae31d1 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 1 Apr 2025 14:46:22 +0200 Subject: [PATCH 5/6] Add more comments --- test/e2e/specs/interactivity/router-styles.spec.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/e2e/specs/interactivity/router-styles.spec.ts b/test/e2e/specs/interactivity/router-styles.spec.ts index 6e7d09df27b0d0..6ef93ce3348c3f 100644 --- a/test/e2e/specs/interactivity/router-styles.spec.ts +++ b/test/e2e/specs/interactivity/router-styles.spec.ts @@ -314,6 +314,8 @@ test.describe( 'Router styles', () => { test( 'should not cause race conditions during render', async ( { page, } ) => { + // Resolve functions for promises that fulfill once the target + // style is requested and resolved respectively. let requestStyle: ( value?: unknown ) => void; let resolveStyle: ( value?: unknown ) => void; @@ -323,7 +325,7 @@ test.describe( 'Router styles', () => { ( resolve ) => ( requestStyle = resolve ) ); - // Setup a route handler to intercept styles. + // Setup a route handler to intercept a specific style sheet. const linkPattern = '**/router-styles-red/style-from-link.css*'; await page.route( linkPattern, async ( route ) => { requestStyle(); @@ -342,9 +344,10 @@ test.describe( 'Router styles', () => { await expect( blue ).toHaveCSS( 'color', COLOR_WRAPPER ); await expect( all ).toHaveCSS( 'color', COLOR_WRAPPER ); + // Hover the red link. await page.getByTestId( 'link red' ).hover(); - // Wait until the style has been requested. + // Wait until the target style has been requested. await styleHasBeenRequested; await page.getByTestId( 'link red' ).click(); @@ -357,6 +360,7 @@ test.describe( 'Router styles', () => { await page.getByTestId( 'link green' ).click(); + // Colors should change after navigation. await expect( csn ).toBeVisible(); await expect( red ).toHaveCSS( 'color', COLOR_WRAPPER ); await expect( green ).toHaveCSS( 'color', COLOR_GREEN ); From 3201fafec1793381e9912df05a86412ad9a6c65e Mon Sep 17 00:00:00 2001 From: David Date: Wed, 2 Apr 2025 17:08:14 +0200 Subject: [PATCH 6/6] Remove try..catch around renderRegions call --- packages/interactivity-router/src/index.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/interactivity-router/src/index.ts b/packages/interactivity-router/src/index.ts index c6bef781386199..e72649ae8dce4e 100644 --- a/packages/interactivity-router/src/index.ts +++ b/packages/interactivity-router/src/index.ts @@ -338,13 +338,7 @@ export const { state, actions } = store< Store >( 'core/router', { ! page.initialData?.config?.[ 'core/router' ] ?.clientNavigationDisabled ) { - try { - renderRegions( page ); - } catch ( e ) { - // eslint-disable-next-line no-console - console.warn( e ); - yield forcePageReload( href ); - } + renderRegions( page ); window.history[ options.replace ? 'replaceState' : 'pushState' ]( {}, '', href );