Stats: Deprioritize stats script with fetchpriority=low and remove dns-prefetch#47936
Stats: Deprioritize stats script with fetchpriority=low and remove dns-prefetch#47936
Conversation
… filter Use the WP core wp_script_attributes filter to add fetchpriority=low to the jetpack-stats script tag. This reduces network contention with LCP resources in Safari and Firefox, which don't automatically deprioritize async/defer scripts. See HOG-123
Remove the automatic dns-prefetch resource hint for stats.wp.com since we're deprioritizing the stats script with fetchpriority=low. Front-loading DNS resolution for a deliberately delayed resource is counterproductive. See HOG-123
|
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! |
|
Note: The |
Code Coverage SummaryCoverage changed in 1 file.
|
There was a problem hiding this comment.
Pull request overview
This PR improves frontend performance for Jetpack Stats by lowering the network priority of the Stats script and avoiding an early DNS lookup that would work against that deprioritization.
Changes:
- Add
fetchpriority="low"to thejetpack-statsscript tag via thewp_script_attributesfilter. - Remove the automatic
dns-prefetchhint forstats.wp.comvia thewp_resource_hintsfilter. - Add unit tests for the new attribute/hint-filtering helpers and include a changelog fragment.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| projects/packages/stats/src/class-tracking-pixel.php | Adds the new filters to set low fetch priority on the Stats script and remove the stats.wp.com DNS prefetch hint. |
| projects/packages/stats/tests/php/Tracking_Pixel_Test.php | Adds tests covering the new helper callbacks for script attributes and resource hints. |
| projects/packages/stats/changelog/enhance-stats-fetchpriority-low-HOG-123 | Documents the behavioral change in the Stats package changelog. |
| $this->assertContains( '//stats.wp.com', $result ); | ||
| } | ||
|
|
||
| /** |
There was a problem hiding this comment.
Current tests validate the helper callbacks directly, but they don't verify that enqueue_stats_script() actually registers the wp_script_attributes / wp_resource_hints filters. Adding an integration-style test that calls enqueue_stats_script() and asserts has_filter(...) (and/or runs apply_filters(...)) would prevent regressions where the new behavior is accidentally not hooked up.
| /** | |
| /** | |
| * Test that enqueue_stats_script registers the expected filters and they run when applied. | |
| */ | |
| public function test_enqueue_stats_script_registers_filters() { | |
| $tracking_pixel = new Tracking_Pixel(); | |
| remove_filter( 'wp_script_attributes', array( Tracking_Pixel::class, 'add_low_fetchpriority' ), 10 ); | |
| remove_filter( 'wp_resource_hints', array( Tracking_Pixel::class, 'remove_stats_dns_prefetch' ), 10 ); | |
| $tracking_pixel->enqueue_stats_script(); | |
| $this->assertNotFalse( | |
| has_filter( 'wp_script_attributes', array( Tracking_Pixel::class, 'add_low_fetchpriority' ) ) | |
| ); | |
| $this->assertNotFalse( | |
| has_filter( 'wp_resource_hints', array( Tracking_Pixel::class, 'remove_stats_dns_prefetch' ) ) | |
| ); | |
| $script_attributes = apply_filters( | |
| 'wp_script_attributes', | |
| array( | |
| 'id' => 'jetpack-stats-js', | |
| 'src' => 'https://stats.wp.com/e-202445.js', | |
| ) | |
| ); | |
| $this->assertSame( 'low', $script_attributes['fetchpriority'] ); | |
| $resource_hints = apply_filters( | |
| 'wp_resource_hints', | |
| array( '//stats.wp.com', '//example.com' ), | |
| 'dns-prefetch' | |
| ); | |
| $this->assertNotContains( '//stats.wp.com', $resource_hints ); | |
| $this->assertContains( '//example.com', $resource_hints ); | |
| remove_filter( 'wp_script_attributes', array( Tracking_Pixel::class, 'add_low_fetchpriority' ), 10 ); | |
| remove_filter( 'wp_resource_hints', array( Tracking_Pixel::class, 'remove_stats_dns_prefetch' ), 10 ); | |
| } | |
| /** |
Fixes #43631
Proposed changes
fetchpriority="low"to the Jetpack Stats script tag via the WP corewp_script_attributesfilter, reducing network contention with LCP resources in Safari and Firefox (which don't automatically deprioritize async/defer scripts like Chrome does).dns-prefetchresource hint forstats.wp.comvia thewp_resource_hintsfilter, since front-loading DNS resolution for a deliberately deprioritized resource is counterproductive.Other information
This follows the approach from Weston Ruter's demo plugin and writeup. Uses the WP core
wp_script_attributesfilter (available since WP 6.1) instead of string-replacing onscript_loader_tag.Related product discussion/links
Does this pull request change what data or activity we track or use?
No. This only changes the priority/timing of the existing stats script loading — no changes to what is tracked.
Testing instructions
jetpack-statsscript tag includesfetchpriority="low".<link rel="dns-prefetch" href="//stats.wp.com">in the<head>.jp docker phpunit projects/packages/stats— all 105 tests should pass.