Skip to content

Commit

Permalink
Improve Nav block loading UX by preloading Navigation menu requests (#…
Browse files Browse the repository at this point in the history
…48683)

* 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
  • Loading branch information
getdave authored May 11, 2023
1 parent cb87840 commit e7e1b7c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
63 changes: 63 additions & 0 deletions lib/compat/wordpress-6.3/navigation-block-preloading.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Patches resources loaded by the block editor page
* to include Navigation posts.
*
* @package gutenberg
*/

/**
* Preloads requests needed for Navigation posts
*
* @param array $preload_paths Preload paths to be filtered.
* @param WP_Block_Editor_Context $context The current block editor context.
* @return array
*/
function gutenberg_preload_navigation_posts( $preload_paths, $context ) {

// Limit to the Site Editor.
if ( ! empty( $context->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 );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) ) {
Expand Down

1 comment on commit e7e1b7c

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in e7e1b7c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4948062512
📝 Reported issues:

Please sign in to comment.