From ba1e06f2e3f988f988a37753a371b2d433879273 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Thu, 20 Aug 2020 15:18:49 +1000 Subject: [PATCH] Navigation: Preload Navigation screen API requests Use wp.apiFetch.createPreloadingMiddleware to preload the innitial API requests made by the Navigation screen. This makes the screen appear quicker. --- lib/navigation-page.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index e2e37f16f2859..e41d1c680b87d 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -77,6 +77,31 @@ function gutenberg_navigation_init( $hook ) { $settings['fontSizes'] = $font_sizes; } + $menus = wp_get_nav_menus(); + $first_menu_id = count( $menus ) ? $menus[0]->term_id : null; + + $preload_paths = array( + '/__experimental/menus?per_page=100&context=edit&_locale=user', + array( '/wp/v2/pages', 'OPTIONS' ), + ); + if ( $first_menu_id ) { + $preload_paths[] = "/__experimental/menu-items?menus=$first_menu_id&per_page=100&context=edit&_locale=user"; + $preload_paths[] = "/__experimental/menus/$first_menu_id?context=edit"; + } + $preload_data = array_reduce( + $preload_paths, + 'rest_preload_api_request', + array() + ); + wp_add_inline_script( + 'wp-api-fetch', + sprintf( + 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', + wp_json_encode( $preload_data ) + ), + 'after' + ); + wp_add_inline_script( 'wp-edit-navigation', sprintf(