fix(perfmatters): keep WooCommerce assets on pages that render WC content#173
Open
jason10lee wants to merge 13 commits into
Open
fix(perfmatters): keep WooCommerce assets on pages that render WC content#173jason10lee wants to merge 13 commits into
jason10lee wants to merge 13 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refines the Newspack ↔ Perfmatters integration so WooCommerce CSS/JS is only stripped on requests that don’t render WooCommerce content. It introduces a request-scoped detector that identifies WooCommerce blocks/shortcodes across common render sources (queried post, active block widgets, and resolved FSE template) and uses that to veto Perfmatters’ “Disable WooCommerce Scripts” behavior when needed.
Changes:
- Add
WooCommerce_Content_Detectorto determine whether the current request will render WooCommerce blocks/shortcodes (including resolving template-parts and synced patterns with cycle guarding). - Hook
perfmatters_disable_woocommerce_scriptsto keep WooCommerce assets when the detector reports WooCommerce content (while respectingNEWSPACK_IGNORE_PERFMATTERS_DEFAULTS). - Add PHPUnit coverage for both the detector and the Perfmatters veto behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
plugins/newspack-plugin/includes/plugins/class-woocommerce-content-detector.php |
Adds the core WooCommerce content detection logic used to decide whether to keep WC assets. |
plugins/newspack-plugin/includes/plugins/class-perfmatters.php |
Hooks Perfmatters’ WC-strip filter and conditionally vetoes stripping when WC content is detected. |
plugins/newspack-plugin/includes/class-newspack.php |
Ensures the new detector class is included before the Perfmatters integration uses it. |
plugins/newspack-plugin/tests/unit-tests/woocommerce-content-detector.php |
Adds unit tests covering block/shortcode detection, FSE template scanning, reference resolution, memoization, and fail-open behavior. |
plugins/newspack-plugin/tests/unit-tests/perfmatters.php |
Adds unit tests for the perfmatters_disable_woocommerce_scripts veto callback behavior. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
All Submissions:
Changes proposed in this Pull Request:
We strip WooCommerce CSS/JS on every non-WooCommerce page by setting the Perfmatters setting
disable_woocommerce_scripts => true. This doesn't take into account non-WooCommerce pages that happen to have WooCommerce blocks on them, and they wind up unstyled.This PR keeps the opinionated default but detects if WooCommerce blocks, shortcodes, and such will be rendered by the current request, and if so, overrides the above setting using the (apparently undocumented?)
perfmatters_disable_woocommerce_scriptsfilter as follows:WooCommerce_Content_Detectorreports whether the current request renders WooCommerce blocks or classic shortcodes. It scans the queried post (post-type-agnostic), active block widgets, and the resolved FSE template, recursively following template-parts and synced patterns (with a cycle guard). It is fail-open (opts to serve WC styles and scripts in the event of an unexpected error, logged vianewspack_log) and memoized per request.Perfmatters::maybe_keep_woocommerce_assets()hooks theperfmatters_disable_woocommerce_scriptsfilter and vetoes the strip when the detector reports WooCommerce content. RespectsNEWSPACK_IGNORE_PERFMATTERS_DEFAULTS, and bails if it sees that set.disable_woocommerce_scripts => trueis unchanged, so we continue to drop WC assets from non-WC pages otherwise.Fixes NPPM-193: Perfmatters "Disable Scripts" for WooCommerce causes block styles to not load.
Caveats:
How to test the changes in this Pull Request:
plugins/newspack-plugin, runn test-php --filter Newspack_Test_WooCommerce_Content_Detector(15 tests) andn test-php --filter Newspack_Test_Perfmatters(3 tests). The full suite remains green.woocommerce-general,woocommerce-layout,woocommerce-smallscreen,wc-blocks-style) load and the products render as a styled grid.NEWSPACK_IGNORE_PERFMATTERS_DEFAULTSand confirm Newspack no longer influences Perfmatters behavior.Other information:
Cross-repo impact review: assessed against the Newspack dependency manifest — Compatible (additive) change, no consumer references to the changed surface, no breaking impact.