From 5e992a464ef5d133eb62e0ef8cfa365a6751c20e Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Mon, 7 Mar 2022 13:49:54 +0100 Subject: [PATCH 1/4] Optimize preload paths for post editor --- lib/init.php | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/lib/init.php b/lib/init.php index c84bb91155cc2e..b9bd786c14b5d7 100644 --- a/lib/init.php +++ b/lib/init.php @@ -148,3 +148,49 @@ function register_site_logo_to_rest_index( $response ) { } add_filter( 'rest_index', 'register_site_logo_to_rest_index' ); + +/** + * Optimizes the preload paths registered in Core (`edit-form-blocks.php`). + * + * @param array $preload_paths Preload paths to be filtered. + * @return array + */ +function optimize_preload_paths( $preload_paths ) { + // remove preload of the `/` route. + $root_index = array_search( '/', $preload_paths, true ); + if ( false !== $root_index ) { + array_splice( $preload_paths, $root_index, 1 ); + } + + // change `/types` context from `edit` to `view` (requested in `loadPostTypeEntities`). + $types_index = array_search( '/wp/v2/types?context=edit', $preload_paths, true ); + if ( false !== $types_index ) { + $preload_paths[$types_index] = '/wp/v2/types?context=view'; + } + + // start preloading `/taxonomies` in `view` context (requested in `loadTaxonomyEntities`). + $tax_index = array_search( '/wp/v2/taxonomies?per_page=-1&context=edit', $preload_paths, true ); + if ( false === $tax_index ) { + array_push( $preload_paths, '/wp/v2/taxonomies?context=view' ); + } else { + $preload_paths[$tax_index] = '/wp/v2/taxonomies?context=view'; + } + + // start preloading `/settings`. + $settings_index = array_search( '/wp/v2/settings', $preload_paths, true ); + if ( false === $settings_index ) { + array_push( $preload_paths, '/wp/v2/settings' ); + } + + // modify the preload of `/users/me` to match the real request. + foreach ( $preload_paths as $user_index => $user_path ) { + if ( 0 === strpos( $user_path, '/wp/v2/users/me' ) ) { + $preload_paths[$user_index] = '/wp/v2/users/me'; + break; + } + } + + return $preload_paths; +} + +add_filter( 'block_editor_rest_api_preload_paths', 'optimize_preload_paths' ); From c815ab2441c771b6042eed01c84131b765c5b8d5 Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Mon, 7 Mar 2022 13:50:59 +0100 Subject: [PATCH 2/4] Optimize preload paths for site editor --- lib/full-site-editing/edit-site-page.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/full-site-editing/edit-site-page.php b/lib/full-site-editing/edit-site-page.php index 3287659385dc90..afc80b8ab09d02 100644 --- a/lib/full-site-editing/edit-site-page.php +++ b/lib/full-site-editing/edit-site-page.php @@ -143,11 +143,10 @@ static function( $classes ) { 'preload_paths' => array_merge( array( array( '/wp/v2/media', 'OPTIONS' ), - '/', - '/wp/v2/types?context=edit', + '/wp/v2/types?context=view', '/wp/v2/types/wp_template?context=edit', '/wp/v2/types/wp_template-part?context=edit', - '/wp/v2/taxonomies?context=edit', + '/wp/v2/taxonomies?context=view', '/wp/v2/pages?context=edit', '/wp/v2/categories?context=edit', '/wp/v2/posts?context=edit', From c15bddb80390042f2c24b2ee9f834a219f4358dd Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Mon, 7 Mar 2022 13:54:54 +0100 Subject: [PATCH 3/4] Fetch the / endpoint with a _fields param --- packages/core-data/src/entities.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index 0d9a9c1b10be12..eb4ab89b649101 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -24,6 +24,19 @@ export const defaultEntities = [ name: '__unstableBase', kind: 'root', baseURL: '/', + baseURLParams: { + _fields: [ + 'description', + 'gmt_offset', + 'home', + 'name', + 'site_icon', + 'site_icon_url', + 'site_logo', + 'timezone_string', + 'url', + ].join( ',' ), + }, }, { label: __( 'Site' ), From 9a941702c67e30202cda311bacebc3b9e1728b62 Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Tue, 8 Mar 2022 10:48:12 +0100 Subject: [PATCH 4/4] Move optimization filter to lib/compat --- lib/compat/wordpress-6.0/edit-form-blocks.php | 52 +++++++++++++++++++ lib/init.php | 46 ---------------- lib/load.php | 1 + 3 files changed, 53 insertions(+), 46 deletions(-) create mode 100644 lib/compat/wordpress-6.0/edit-form-blocks.php diff --git a/lib/compat/wordpress-6.0/edit-form-blocks.php b/lib/compat/wordpress-6.0/edit-form-blocks.php new file mode 100644 index 00000000000000..691ba12b4b9793 --- /dev/null +++ b/lib/compat/wordpress-6.0/edit-form-blocks.php @@ -0,0 +1,52 @@ + $user_path ) { + if ( 0 === strpos( $user_path, '/wp/v2/users/me' ) ) { + $preload_paths[ $user_index ] = '/wp/v2/users/me'; + break; + } + } + + return $preload_paths; +} + +add_filter( 'block_editor_rest_api_preload_paths', 'optimize_preload_paths' ); diff --git a/lib/init.php b/lib/init.php index b9bd786c14b5d7..c84bb91155cc2e 100644 --- a/lib/init.php +++ b/lib/init.php @@ -148,49 +148,3 @@ function register_site_logo_to_rest_index( $response ) { } add_filter( 'rest_index', 'register_site_logo_to_rest_index' ); - -/** - * Optimizes the preload paths registered in Core (`edit-form-blocks.php`). - * - * @param array $preload_paths Preload paths to be filtered. - * @return array - */ -function optimize_preload_paths( $preload_paths ) { - // remove preload of the `/` route. - $root_index = array_search( '/', $preload_paths, true ); - if ( false !== $root_index ) { - array_splice( $preload_paths, $root_index, 1 ); - } - - // change `/types` context from `edit` to `view` (requested in `loadPostTypeEntities`). - $types_index = array_search( '/wp/v2/types?context=edit', $preload_paths, true ); - if ( false !== $types_index ) { - $preload_paths[$types_index] = '/wp/v2/types?context=view'; - } - - // start preloading `/taxonomies` in `view` context (requested in `loadTaxonomyEntities`). - $tax_index = array_search( '/wp/v2/taxonomies?per_page=-1&context=edit', $preload_paths, true ); - if ( false === $tax_index ) { - array_push( $preload_paths, '/wp/v2/taxonomies?context=view' ); - } else { - $preload_paths[$tax_index] = '/wp/v2/taxonomies?context=view'; - } - - // start preloading `/settings`. - $settings_index = array_search( '/wp/v2/settings', $preload_paths, true ); - if ( false === $settings_index ) { - array_push( $preload_paths, '/wp/v2/settings' ); - } - - // modify the preload of `/users/me` to match the real request. - foreach ( $preload_paths as $user_index => $user_path ) { - if ( 0 === strpos( $user_path, '/wp/v2/users/me' ) ) { - $preload_paths[$user_index] = '/wp/v2/users/me'; - break; - } - } - - return $preload_paths; -} - -add_filter( 'block_editor_rest_api_preload_paths', 'optimize_preload_paths' ); diff --git a/lib/load.php b/lib/load.php index 26cd50608dbdc1..f15db9de33c05d 100644 --- a/lib/load.php +++ b/lib/load.php @@ -106,6 +106,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-6.0/class-wp-webfonts-provider.php'; require __DIR__ . '/compat/wordpress-6.0/class-wp-webfonts-provider-local.php'; require __DIR__ . '/compat/wordpress-6.0/webfonts.php'; +require __DIR__ . '/compat/wordpress-6.0/edit-form-blocks.php'; require __DIR__ . '/compat/experimental/blocks.php'; require __DIR__ . '/blocks.php';