Skip to content

Commit 14a942d

Browse files
committed
Forms: Preload initial inbox data and essential endpoints
Preloads first page of responses, counts, filters, and entity types for instant rendering. Related: #45339
1 parent 845fb3a commit 14a942d

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: changed
3+
4+
Forms: preload initial inbox data and essential endpoints for faster page load.

projects/packages/forms/src/dashboard/class-dashboard.php

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function load_admin_scripts() {
7171
'in_footer' => true,
7272
'textdomain' => 'jetpack-forms',
7373
'enqueue' => true,
74-
'dependencies' => array( 'wp-api-fetch' ),
74+
'dependencies' => array( 'wp-api-fetch', 'wp-data', 'wp-core-data', 'wp-dom-ready' ),
7575
)
7676
);
7777

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

8585
// Preload Forms endpoints needed in dashboard context.
86-
$preload_paths = array(
86+
// Pre-fetch the first inbox page so the UI renders instantly on first load.
87+
$preload_params = array(
88+
'_fields' => 'id,status,date,date_gmt,author_name,author_email,author_url,author_avatar,ip,entry_title,entry_permalink,has_file,fields',
89+
'context' => 'view',
90+
'order' => 'desc',
91+
'orderby' => 'date',
92+
'page' => 1,
93+
'per_page' => 20,
94+
'status' => 'draft,publish',
95+
);
96+
\ksort( $preload_params );
97+
$initial_responses_path = \add_query_arg( $preload_params, '/wp/v2/feedback' );
98+
$initial_responses_locale_path = \add_query_arg(
99+
\array_merge(
100+
$preload_params,
101+
array( '_locale' => 'user' )
102+
),
103+
'/wp/v2/feedback'
104+
);
105+
$preload_paths = array(
106+
'/wp/v2/types?context=view',
87107
'/wp/v2/feedback/config',
88-
'/wp/v2/feedback/config?_locale=user',
89108
'/wp/v2/feedback/integrations?version=2',
90-
'/wp/v2/feedback/integrations?version=2&_locale=user',
109+
'/wp/v2/feedback/counts',
110+
'/wp/v2/feedback/filters',
111+
$initial_responses_path,
112+
$initial_responses_locale_path,
91113
);
92-
$preload_data = array_reduce( $preload_paths, 'rest_preload_api_request', array() );
114+
$preload_data_raw = array_reduce( $preload_paths, 'rest_preload_api_request', array() );
115+
116+
// Normalize keys to match what apiFetch will request (without domain).
117+
$preload_data = array();
118+
foreach ( $preload_data_raw as $key => $value ) {
119+
$normalized_key = preg_replace( '#^https?://[^/]+/wp-json#', '', $key );
120+
$preload_data[ $normalized_key ] = $value;
121+
}
122+
93123
wp_add_inline_script(
94124
self::SCRIPT_HANDLE,
95-
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( ' . wp_json_encode( $preload_data ) . ' ) );',
125+
sprintf(
126+
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
127+
wp_json_encode( $preload_data )
128+
),
96129
'before'
97130
);
98131
}

0 commit comments

Comments
 (0)