Repository navigation
wp-build: Move the site data preload into wp-build-polyfills - #54460
Conversation
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! Jetpack plugin: The Jetpack plugin has different release cadences depending on the platform:
If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. Boost plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
Code Coverage SummaryCoverage changed in 19 files. Only the first 5 are listed here.
Full summary · PHP report · JS report Coverage check overridden by
Covered by non-unit tests
|
| * | ||
| * @var string[] | ||
| */ | ||
| private const PRELOAD_FILTERS = array( |
There was a problem hiding this comment.
The Monetize page in #54433 isn't in this list, so whichever of the two merges second needs jetpack_monetize_jetpack_monetize_dashboard_wp_admin_preload_paths added here or the new lint fails. If both go green before either merges, trunk's lint fails until someone adds it.
| } | ||
| self::$hooked = true; | ||
|
|
||
| foreach ( self::PRELOAD_FILTERS as $filter ) { |
There was a problem hiding this comment.
This ties the 7.1 preload to register(), and Forms only calls register() on its own page, so a host that loads the Forms build through jetpack_forms_load_wp_build now goes without the preload too. That host already skipped register() before this change, and no host in this repo opts in.
| self::$hooked = true; | ||
|
|
||
| foreach ( self::PRELOAD_FILTERS as $filter ) { | ||
| add_filter( $filter, array( __CLASS__, 'add_site_data_preload' ) ); |
There was a problem hiding this comment.
These names match the {{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_wp_admin_preload_paths filter in the @wordpress/build 0.26.0 wp-admin template. The template applies it from admin_enqueue_scripts, so it runs after register() has added these.
| * @param array $paths REST API paths to preload. | ||
| * @return array REST API paths to preload. | ||
| */ | ||
| public static function add_site_data_preload( $paths ) { |
There was a problem hiding this comment.
Pages built from the new patch get the 7.1 preload only from this method, so an older polyfills copy winning the autoloader, like during a mixed deploy, costs those pages one live site-data request. Every plugin that ships those pages also ships this version, so I'm fine with that.
| */ | ||
| public static function add_site_data_preload( $paths ) { | ||
| // WordPress 7.1's core-data requests this list; removing it adds a live request on those sites. | ||
| // https://github.com/WordPress/gutenberg/pull/86185 lets pages survive a failed request without changing the list. |
There was a problem hiding this comment.
WordPress/gutenberg#86185 is still open. It stops a failed site-data request from taking the page down and leaves the field list alone, so this path is still needed after it lands.
| public static function add_site_data_preload( $paths ) { | ||
| // WordPress 7.1's core-data requests this list; removing it adds a live request on those sites. | ||
| // https://github.com/WordPress/gutenberg/pull/86185 lets pages survive a failed request without changing the list. | ||
| $path = '/?_fields=description,gmt_offset,home,image_max_bit_depth,image_sizes,image_size_threshold,image_strip_meta,name,site_icon,site_icon_url,site_logo,timezone_string,url,page_for_posts,page_on_front,show_on_front'; |
There was a problem hiding this comment.
This path is preloaded on every WordPress version, same as the old patch did, even though only 7.1's bundled core-data asks for it. Every other setup carries one unused preload.
| // WordPress 7.1's core-data requests this list; removing it adds a live request on those sites. | ||
| // https://github.com/WordPress/gutenberg/pull/86185 lets pages survive a failed request without changing the list. | ||
| $path = '/?_fields=description,gmt_offset,home,image_max_bit_depth,image_sizes,image_size_threshold,image_strip_meta,name,site_icon,site_icon_url,site_logo,timezone_string,url,page_for_posts,page_on_front,show_on_front'; | ||
| if ( ! in_array( $path, $paths, true ) ) { |
There was a problem hiding this comment.
If another callback on these filters returns something other than an array, in_array() fails here. The template's own array_reduce() would break on that too, and nothing in this repo hooks these filters besides this class.
| // https://github.com/WordPress/gutenberg/pull/86185 lets pages survive a failed request without changing the list. | ||
| $path = '/?_fields=description,gmt_offset,home,image_max_bit_depth,image_sizes,image_size_threshold,image_strip_meta,name,site_icon,site_icon_url,site_logo,timezone_string,url,page_for_posts,page_on_front,show_on_front'; | ||
| if ( ! in_array( $path, $paths, true ) ) { | ||
| $paths[] = $path; |
There was a problem hiding this comment.
This appends the 7.1 path at the end, where the old patch put it second. Preloaded responses are matched by path, so the order makes no difference.
| EXIT=1 | ||
| echo "::error file=$FILE::Wp-build page filter \`$FILTER\` is missing from PRELOAD_FILTERS. Add it to \`$WP_BUILD_PRELOAD_FILE\`." | ||
| done < <( jq -r --argjson filters "$WP_BUILD_PRELOAD_FILTERS" ' | ||
| .wpPlugin as $plugin | $plugin.pages[]? | |
There was a problem hiding this comment.
If a package.json has a malformed pages entry, jq errors on it and skips the rest of that file's pages without failing the check. Every entry today is a string or an object with an id, so I'm leaving that for now.
| .wpPlugin as $plugin | $plugin.pages[]? | | ||
| (if type == "string" then . else .id end) as $page | | ||
| ((($plugin.name // "gutenberg") + "_" + $page | gsub("-"; "_")) + "_wp_admin_preload_paths") as $filter | | ||
| select($filters | index($filter) | not) | |
There was a problem hiding this comment.
This only checks that each page's filter is in the list, so a removed or renamed page leaves a stale name in PRELOAD_FILTERS. That costs one unused add_filter(), so I didn't add the reverse check.
| /** | ||
| * Each wp-build page preloads the WordPress 7.1 site record without duplicates. | ||
| */ | ||
| public function test_site_data_preload_keeps_the_upstream_path_first_on_every_page() { |
There was a problem hiding this comment.
I put this test first in the class, above the properties and set_up(). No sniff checks member order, so I've left it there.
| $expected = $paths; | ||
| $expected[] = '/?_fields=description,gmt_offset,home,image_max_bit_depth,image_sizes,image_size_threshold,image_strip_meta,name,site_icon,site_icon_url,site_logo,timezone_string,url,page_for_posts,page_on_front,show_on_front'; | ||
|
|
||
| WP_Build_Polyfills::register( 'first-page', array() ); |
There was a problem hiding this comment.
tear_down() doesn't remove the 20 filters this adds, so they stay hooked for the later tests. No other test applies them, so I've left it.
| $expected[] = '/?_fields=description,gmt_offset,home,image_max_bit_depth,image_sizes,image_size_threshold,image_strip_meta,name,site_icon,site_icon_url,site_logo,timezone_string,url,page_for_posts,page_on_front,show_on_front'; | ||
|
|
||
| WP_Build_Polyfills::register( 'first-page', array() ); | ||
| WP_Build_Polyfills::register( 'second-page', array() ); |
There was a problem hiding this comment.
This second call returns early because $hooked is already set, so it never adds the filters a second time. It shows that a second page on the same request still leaves one copy of the path in each list.
| WP_Build_Polyfills::register( 'first-page', array() ); | ||
| WP_Build_Polyfills::register( 'second-page', array() ); | ||
|
|
||
| foreach ( $pages as $page ) { |
There was a problem hiding this comment.
The first failing page stops this loop, and the assertion message names its filter, so I kept one loop rather than a data provider.
| $preload_paths = array( | ||
| '/?_fields=description,generate_animated_image_subsizes,gmt_offset,home,image_max_bit_depth,image_sizes,image_size_threshold,image_strip_meta,name,site_icon,site_icon_url,site_logo,timezone_string,url,page_for_posts,page_on_front,show_on_front', | ||
| + // WordPress 7.1's bundled core-data requests this list, without generate_animated_image_subsizes. | ||
| + '/?_fields=description,gmt_offset,home,image_max_bit_depth,image_sizes,image_size_threshold,image_strip_meta,name,site_icon,site_icon_url,site_logo,timezone_string,url,page_for_posts,page_on_front,show_on_front', |
There was a problem hiding this comment.
The standalone page.php template loses the 7.1 preload with this, and it has no filter to add it back. No page in this repo is served through page.php, so only an outside host opening a bare page id such as ?page=jetpack-forms-responses with that build loaded would make the extra request.
xavier-lc
left a comment
There was a problem hiding this comment.
Checked the filter names against the 0.26.0 template and every register() caller. I also ran the new lint block on this head: it passes, and fails on seo/package.json once that entry is removed from the list. Two small things inline.
| $pages = array( | ||
| 'jetpack_activity_log_jetpack_activity_log_dashboard', | ||
| 'jetpack_backup_jetpack_backup_dashboard', | ||
| 'jetpack_boost_jetpack_boost_dashboard', | ||
| 'jetpack_forms_jetpack_forms_responses', | ||
| 'jetpack_mu_wpcom_site_setup', | ||
| 'jetpack_mu_wpcom_wpcom_backup', | ||
| 'jetpack_mu_wpcom_wpcom_scan', | ||
| 'jetpack_my_jetpack_my_jetpack_dashboard', | ||
| 'jetpack_newsletter_jetpack_newsletter_dashboard', | ||
| 'jetpack_plugin_jetpack_ai_hub', | ||
| 'jetpack_plugin_jetpack_settings_dashboard', | ||
| 'jetpack_podcast_jetpack_podcast_dashboard', | ||
| 'jetpack_protect_jetpack_protect_dashboard', | ||
| 'jetpack_scan_jetpack_scan', | ||
| 'jetpack_search_jetpack_search_dashboard', | ||
| 'jetpack_seo_jetpack_seo_dashboard', | ||
| 'jetpack_sharing_likes_jetpack_sharing_settings', | ||
| 'jetpack_social_jetpack_social_dashboard', | ||
| 'jetpack_videopress_jetpack_videopress_dashboard', | ||
| 'jpa_jetpack_premium_analytics', |
There was a problem hiding this comment.
The test could read PRELOAD_FILTERS through reflection, as the lint does, instead of repeating the 20 names. It would still check that each listed filter is hooked, dedupes and keeps the upstream path first, and adding or renaming a page would mean one edit instead of two.
There was a problem hiding this comment.
Good call, done in c1d859b. The test now reads PRELOAD_FILTERS through reflection, so adding or renaming a page is one edit.
| [[ "$WP_BUILD_PRELOAD_FILTERS" == \[* ]] || { echo "::error file=$WP_BUILD_PRELOAD_FILE::Unable to read PRELOAD_FILTERS from $WP_BUILD_PRELOAD_FILE."; exit 1; } | ||
| while IFS=$'\t' read -r FILE FILTER; do | ||
| EXIT=1 | ||
| echo "::error file=$FILE::Wp-build page filter \`$FILTER\` is missing from PRELOAD_FILTERS. Add it to \`$WP_BUILD_PRELOAD_FILE\`." |
There was a problem hiding this comment.
Nit: "Wp-build page filter" should be "wp-build page filter", the casing used everywhere else.
0b93446 to
c1d859b
Compare
| \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::MODULE_IDS | ||
| ), | ||
| '7.0', | ||
| array( |
There was a problem hiding this comment.
No unit test runs the page settings that this and the other loaders now pass to register(), so I'm leaving the coverage check red. The preload on all 20 pages was checked in a browser on WordPress 7.1 and 7.0, and the 20 filter names match the old PRELOAD_FILTERS list exactly.
| array( | ||
| array( | ||
| 'prefix' => 'jetpack_scan', | ||
| 'slug' => self::PAGE_SLUG, |
There was a problem hiding this comment.
self::PAGE_SLUG is Scan's menu slug, and jetpack-scan is also its wpPlugin.pages id, so it works as the page slug here. Three hard-coded jetpack_scan_jetpack_scan_* function names already depend on that id, so splitting the two would mean changing them too.
xavier-lc
left a comment
There was a problem hiding this comment.
No blockers. Two small things here, two inline.
- The description's "Remove the central page list and its project-structure lint check" describes something added and removed within this branch. It was never on trunk and the net diff doesn't touch
.github/, so that sentence can go. - "Code coverage requirement" is the only failing check. The reason for leaving it red is sound, but it's only in an inline comment. Please add the
Covered by non-unit testslabel and put the reason in the description.
Checked at this head:
- All 20
prefix/slugpairs matchwpPluginand the 0.26.0wp_admin_preload_pathsfilter. The tworegister()calls left unchanged (Newsletter legacy, Publicize editor) don't serve wp-build pages. - Current trunk has the same wp-build pages and
register()caller files as this branch. - An older polyfills copy ignores the extra argument, so the worst case is one extra request, not a fatal.
I didn't run the tests locally, and Simple and Atomic remain untested.
| if ( ! is_array( $page ) || ! isset( $page['slug'] ) || ! is_string( $page['slug'] ) || '' === $page['slug'] ) { | ||
| continue; | ||
| } | ||
| add_filter( self::page_filter( $page, 'wp_admin_preload_paths' ), array( __CLASS__, 'add_site_data_preload' ) ); |
There was a problem hiding this comment.
#54433 adds jetpack-monetize-dashboard and calls register() with two arguments, so once both PRs merge that page makes one live site-data request on 7.1. The patch used to cover new pages automatically, and nothing flags the omission now. Whichever PR merges second should pass '7.0' and array( array( 'prefix' => 'jetpack_monetize', 'slug' => 'jetpack-monetize-dashboard' ) ).
There was a problem hiding this comment.
Good catch, thanks! Agreed, the second one to land will add '7.0' and the monetize page settings.
| @@ -0,0 +1,5 @@ | |||
| Significance: patch | |||
| Type: changed | |||
| Comment: Pass wp-build page configuration to the polyfills package. | |||
There was a problem hiding this comment.
move-site-data-preload-polyfill already holds the real entry, and "Pass wp-build page configuration to the polyfills package" doesn't make sense inside this package. This file can be deleted.
| WP_Build_Polyfills::SCRIPT_HANDLES, | ||
| WP_Build_Polyfills::MODULE_IDS | ||
| ), | ||
| '7.0', |
There was a problem hiding this comment.
I have a feeling all these might be missed in the next version of #51370. There's nothing to indicate this "7.0" is a WordPress version number.
There was a problem hiding this comment.
Good call. The third argument now defaults to null, which leaves the shared threshold alone, so the package loaders pass null and carry no version at all. The only default left is the one in wp-build-polyfills, done in 5ab44ed.
| 'jetpack', | ||
| array_merge( WP_Build_Polyfills::SCRIPT_HANDLES, WP_Build_Polyfills::MODULE_IDS ) | ||
| array_merge( WP_Build_Polyfills::SCRIPT_HANDLES, WP_Build_Polyfills::MODULE_IDS ), | ||
| '7.0', |
There was a problem hiding this comment.
Maybe this?
| '7.0', | |
| JETPACK__MINIMUM_WP_VERSION, |
There was a problem hiding this comment.
Small follow-up on this one. WordPress.com Simple loads this file for the Jetpack AI page without jetpack.php, so the constant can be undefined there and that page would hit a fatal error. In 5f756d3 it's passed when it's defined and null otherwise, so your suggestion still applies everywhere the plugin's main file runs. I also merged trunk in the same push.
Callers pass null to keep the package default threshold; the Jetpack plugin passes its minimum WordPress version constant.
…-upstream-link # Conflicts: # pnpm-lock.yaml
…yfills On WordPress 7.1, this lets the Monetize dashboard preload its site data instead of requesting it live.
…issing WordPress.com loads the Jetpack AI page through jetpack-mu-wpcom without the plugin's main file, so JETPACK__MINIMUM_WP_VERSION can be undefined there. Pass null in that case, which leaves the polyfills' version threshold alone. Use the $page_id argument for the page slug: load() only records the page id when it aliases the screen ID, so the property is not set when a page is loaded without the alias.
Fixes JETPACK-3506
Proposed changes
Move the WordPress 7.1 site-data preload from the
@wordpress/buildpatch intowp-build-polyfills. Each page passes its build prefix and page slug toregister(), which constructs its preload filter. Ignore malformed page entries. The page argument needs@wordpress/build0.26.0 or later.Loaders now pass
nullas the third argument, which leaves the version threshold unchanged, and Jetpack passesJETPACK__MINIMUM_WP_VERSION.Why are these changes being made?
WordPress 7.1 requests a different site-data field list from the build template. Supplying page configuration lets any plugin use the preload; existing three-argument calls keep working. The upstream change does not change the field list.
It ships in the first Jetpack release after 16.4 with no switch. Simple gets it through mu-wpcom; self-hosted and Atomic sites through the applicable plugins. WordPress 7.0 keeps its current live request. Monetize's preload also waits for its off-by-default flag.
Related product discussion/links
Upstream change
Does this pull request change what data or activity we track or use?
No.
Testing instructions
Checked 19 Docker pages in Chrome on 7.0/7.1 at earlier commits, not Monetize or wpcom-my-jetpack; no real Simple or Atomic runtime.
No unit test runs the page settings the loaders now pass to
register(). The Chrome check above covers them.jetpack-monetize-dashboardflag on, Monetize. Reload with Network open; expect zero root/?_fields=REST requests.load_wp_build()and reload: expect one request. Restore it and expect zero.