Skip to content
Open
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
4 changes: 4 additions & 0 deletions projects/packages/forms/changelog/optimize-inbox-preloading
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Forms: preload initial inbox data and essential endpoints for faster page load.
45 changes: 39 additions & 6 deletions projects/packages/forms/src/dashboard/class-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function load_admin_scripts() {
'in_footer' => true,
'textdomain' => 'jetpack-forms',
'enqueue' => true,
'dependencies' => array( 'wp-api-fetch' ),
'dependencies' => array( 'wp-api-fetch', 'wp-data', 'wp-core-data', 'wp-dom-ready' ),
)
);

Expand All @@ -83,16 +83,49 @@ public function load_admin_scripts() {
Connection_Initial_State::render_script( self::SCRIPT_HANDLE );

// Preload Forms endpoints needed in dashboard context.
$preload_paths = array(
// Pre-fetch the first inbox page so the UI renders instantly on first load.
$preload_params = array(
'_fields' => 'id,status,date,date_gmt,author_name,author_email,author_url,author_avatar,ip,entry_title,entry_permalink,has_file,fields',
'context' => 'view',
'order' => 'desc',
'orderby' => 'date',
'page' => 1,
'per_page' => 20,
'status' => 'draft,publish',
);
\ksort( $preload_params );
$initial_responses_path = \add_query_arg( $preload_params, '/wp/v2/feedback' );
$initial_responses_locale_path = \add_query_arg(
\array_merge(
$preload_params,
array( '_locale' => 'user' )
),
'/wp/v2/feedback'
);
$preload_paths = array(
'/wp/v2/types?context=view',
'/wp/v2/feedback/config',
'/wp/v2/feedback/config?_locale=user',
'/wp/v2/feedback/integrations?version=2',
'/wp/v2/feedback/integrations?version=2&_locale=user',
Copy link
Contributor

@edanzer edanzer Oct 7, 2025

Choose a reason for hiding this comment

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

We removed ?_locale=user from these endpoints, but I think we need to keep them. I just added them here to fix preloading not working. The issue is that apiFetch adds that to endpoints, so we need the endpoints we list here to exactly match what apiFetch calls. If we're going to remove something, we should remove the versions without the extra query param. That would make sense as long as we're always using apiFetch to call them.

'/wp/v2/feedback/counts',
'/wp/v2/feedback/filters',
$initial_responses_path,
$initial_responses_locale_path,
);
$preload_data = array_reduce( $preload_paths, 'rest_preload_api_request', array() );
$preload_data_raw = array_reduce( $preload_paths, 'rest_preload_api_request', array() );

// Normalize keys to match what apiFetch will request (without domain).
$preload_data = array();
foreach ( $preload_data_raw as $key => $value ) {
$normalized_key = preg_replace( '#^https?://[^/]+/wp-json#', '', $key );
$preload_data[ $normalized_key ] = $value;
}

wp_add_inline_script(
self::SCRIPT_HANDLE,
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( ' . wp_json_encode( $preload_data ) . ' ) );',
sprintf(
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
wp_json_encode( $preload_data )
),
'before'
);
}
Expand Down
Loading