From e7e1b7c2fe040587586a8cf2f5db4c1e6cacdc44 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 11 May 2023 13:42:31 +0100 Subject: [PATCH] Improve Nav block loading UX by preloading Navigation menu requests (#48683) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Scaffold out preloading * All GET and OPTIONS preloading * Make preload paths more readable via add_query_arg * Add comment to unusual usage * Resolve PHPCS * Fix formatting * Rename away from “permissions” * Limit to Site Editor * Add context to doc block * Preload Browse Mode sidebar Navigation * Remove redundant preload * Use int not string for numeric --- .../navigation-block-preloading.php | 63 +++++++++++++++++++ lib/load.php | 1 + 2 files changed, 64 insertions(+) create mode 100644 lib/compat/wordpress-6.3/navigation-block-preloading.php diff --git a/lib/compat/wordpress-6.3/navigation-block-preloading.php b/lib/compat/wordpress-6.3/navigation-block-preloading.php new file mode 100644 index 00000000000000..82fe81b236a590 --- /dev/null +++ b/lib/compat/wordpress-6.3/navigation-block-preloading.php @@ -0,0 +1,63 @@ +name ) && 'core/edit-site' !== $context->name ) { + return $preload_paths; + } + + $navigation_rest_route = rest_get_route_for_post_type_items( + 'wp_navigation' + ); + + // Preload the OPTIONS request for all Navigation posts request. + $preload_paths[] = array( $navigation_rest_route, 'OPTIONS' ); + + // Preload the GET request for ALL 'published' or 'draft' Navigation posts. + $preload_paths[] = array( + add_query_arg( + array( + 'context' => 'edit', + 'per_page' => 100, + '_locale' => 'user', + // array indices are required to avoid query being encoded and not matching in cache. + 'status[0]' => 'publish', + 'status[1]' => 'draft', + ), + $navigation_rest_route + ), + 'GET', + ); + + // Preload request for Browse Mode sidebar "Navigation" section. + $preload_paths[] = array( + add_query_arg( + array( + 'context' => 'edit', + 'per_page' => 1, + 'status' => 'publish', + 'order' => 'desc', + 'orderby' => 'date', + ), + $navigation_rest_route + ), + 'GET', + ); + + return $preload_paths; +} +add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_preload_navigation_posts', 10, 2 ); diff --git a/lib/load.php b/lib/load.php index 79b076251e54ad..37dc2305678940 100644 --- a/lib/load.php +++ b/lib/load.php @@ -49,6 +49,7 @@ function gutenberg_is_experiment_enabled( $name ) { require_once __DIR__ . '/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php'; require_once __DIR__ . '/compat/wordpress-6.3/rest-api.php'; require_once __DIR__ . '/compat/wordpress-6.3/theme-previews.php'; + require_once __DIR__ . '/compat/wordpress-6.3/navigation-block-preloading.php'; // Experimental. if ( ! class_exists( 'WP_Rest_Customizer_Nonces' ) ) {