Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preload: fix e2e test #67497

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions backport-changelog/6.8/7687.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
https://github.com/WordPress/wordpress-develop/pull/7687

* https://github.com/WordPress/gutenberg/pull/66488
* https://github.com/WordPress/gutenberg/pull/67497
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