Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Preload: fix e2e test
  • Loading branch information
ellatrix committed Dec 2, 2024
commit 434a2502f634ceab429ec5da8c87461089228b95
3 changes: 3 additions & 0 deletions lib/compat/wordpress-6.8/preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ function gutenberg_block_editor_preload_paths_6_8( $paths, $context ) {
*/
$context = current_user_can( 'edit_theme_options' ) ? 'edit' : 'view';
$paths[] = "/wp/v2/global-styles/$global_styles_id?context=$context";

// Used by getBlockPatternCategories in useBlockEditorSettings.
$paths[] = '/wp/v2/block-patterns/categories';
}
return $paths;
}
Expand Down
44 changes: 28 additions & 16 deletions test/e2e/specs/site-editor/preload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,38 @@ test.describe( 'Preload', () => {
page,
admin,
} ) => {
// Do not use `visitSiteEditor` because it waits for the iframe to load.
await admin.visitAdminPage( 'site-editor.php' );

const requests = [];
let isLoaded = false;

page.on( 'request', ( request ) => {
if ( request.resourceType() === 'document' ) {
// The iframe also "requests" a blob document. This is the most
// reliable way to wait for the iframe to start loading.
// `waitForSelector` is always a bit delayed, and we don't want
// to detect requests after the iframe mounts.
isLoaded = true;
} else if ( ! isLoaded && request.resourceType() === 'fetch' ) {
requests.push( request.url() );
function onRequest( request ) {
if (
request.resourceType() === 'document' &&
request.url().startsWith( 'blob:' )
) {
// Stop recording when the iframe is initialized.
page.off( 'request', onRequest );
} else if ( request.resourceType() === 'fetch' ) {
const url = request.url();
const urlObject = new URL( url );
const restRoute = urlObject.searchParams.get( 'rest_route' );
if ( restRoute ) {
requests.push( restRoute );
} else {
requests.push( url );
}
}
} );
}

page.on( 'request', onRequest );

await page.waitForFunction( ( _isLoaded ) => _isLoaded, [ isLoaded ] );
await admin.visitSiteEditor();

expect( requests ).toEqual( [] );
// To do: these should all be removed or preloaded.
expect( requests ).toEqual( [
// There are two separate settings OPTIONS requests. We should fix
// so the one for canUser and getEntityRecord are reused.
'/wp/v2/settings',
Comment on lines +46 to +48
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should delay the destruction of the preloaded cache by a couple of MS instead of removing it after the first match. Technically, this should help resolve anything in the critical path via cache without "affecting" the PreloadingMiddleware behavior.

// Unsetting the cache key ensures that the data is only used a single time.
delete cache[ method ][ path ];

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe next idle callback? Still, it doesn't feel like clean solution but it could work for now I guess.

// Seems to be coming from `enableComplementaryArea`.
'/wp/v2/users/me',
] );
} );
} );
Loading