Skip to content
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
27 changes: 27 additions & 0 deletions lib/experimental/dataform-inspector-preload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Preload paths for the DataForm-based editor inspector experiment.
*
* @package gutenberg
*/

/**
* Filters the block editor preload paths.
*
* @param array $paths REST API paths to preload.
* @param WP_Block_Editor_Context $context Block editor context.
* @return array Filtered preload paths.
*/
function gutenberg_dataform_inspector_preload_paths( $paths, $context ) {
if ( 'core/edit-post' !== $context->name || ! isset( $context->post ) ) {
return $paths;
}

// `DataFormPostSummary` requests the post type form config through
// `useViewConfig` with `fields: [ 'form' ]`, so the preload path must
// match the resulting `_fields=form` REST API request.
$paths[] = '/wp/v2/view-config?kind=postType&name=' . $context->post->post_type . '&_fields=form';

return $paths;
}
add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_dataform_inspector_preload_paths', 10, 2 );
3 changes: 3 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/experimental/script-modules.php';
require __DIR__ . '/experimental/pages/site-editor.php';
require __DIR__ . '/experimental/extensible-site-editor.php';
if ( gutenberg_is_experiment_enabled( 'gutenberg-dataform-inspector' ) ) {
require __DIR__ . '/experimental/dataform-inspector-preload.php';
}
if ( gutenberg_is_experiment_enabled( 'gutenberg-media-editor' ) ) {
require __DIR__ . '/experimental/media-editor/load.php';
}
Expand Down
11 changes: 11 additions & 0 deletions packages/edit-post/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ async function preloadResolutions( postType, postId ) {
kind: 'postType',
name: 'wp_template',
} ),
// The DataForm-based inspector requests the entity form config
// when the document sidebar mounts. The args must match the
// `useViewConfig` call in `@wordpress/views` exactly for the
// resolution (and its preload entry) to be shared.
...( postType && window?.__experimentalDataFormInspector
? [
unlock( core ).getViewConfig( 'postType', postType, {
fields: 'form',
} ),
]
: [] ),
// Per-post resolvers. `getPostType` and `getEditedEntityRecord`
// are shorthand/forward-resolver aliases with their own
// resolution metadata, so they need separate kicks.
Expand Down
56 changes: 56 additions & 0 deletions test/e2e/specs/preload/post-editor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,59 @@ test.describe( 'Preload', () => {
);
} );
} );

test.describe( 'Preload with the DataForm inspector experiment', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.setGutenbergExperiments( [
'gutenberg-dataform-inspector',
] );
} );

test.afterAll( async ( { requestUtils } ) => {
await requestUtils.setGutenbergExperiments( [] );
} );

test( 'Should serve the view config form request from the preload cache', async ( {
page,
admin,
editor,
} ) => {
const { requests, stop } = recordRequests( page );

// `clearPreloadedData` warns if any preload entry went unused —
// including the view config path, should it drift from the request
// issued by the `getViewConfig` resolver kickoff.
let preloadStatus;
page.on( 'console', ( msg ) => {
const text = msg.text();
if ( text.startsWith( '[api-fetch][preload] ' ) ) {
preloadStatus = text;
}
} );

await admin.createNewPost();
await openPostSummary( { editor, page } );
await waitForRequestsToSettle( requests );
stop();

// Every preloaded path — including the view config form request —
// should be consumed by the kickoff.
expect( preloadStatus ).toBe(
'[api-fetch][preload] All preloads consumed.'
);
// The DataForm-based inspector's form config must be served from the
// preload cache; a real request here means the sidebar renders an
// empty form first and flashes once the response arrives.
expect(
requests.filter( ( request ) =>
request.includes( '/wp/v2/view-config' )
)
).toEqual( [] );
} );
} );

async function openPostSummary( { editor, page } ) {
await editor.openDocumentSettingsSidebar();

await expect( page.locator( '.editor-post-summary' ) ).toBeVisible();
}
Loading