Skip to content

fix(perfmatters): keep WooCommerce assets on pages that render WC content#173

Open
jason10lee wants to merge 13 commits into
mainfrom
fix/nppm-193-perfmatters-woocommerce-block-styles
Open

fix(perfmatters): keep WooCommerce assets on pages that render WC content#173
jason10lee wants to merge 13 commits into
mainfrom
fix/nppm-193-perfmatters-woocommerce-block-styles

Conversation

@jason10lee
Copy link
Copy Markdown
Contributor

@jason10lee jason10lee commented May 31, 2026

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_scripts filter as follows:

  • New WooCommerce_Content_Detector reports 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 via newspack_log) and memoized per request.
  • Perfmatters::maybe_keep_woocommerce_assets() hooks the perfmatters_disable_woocommerce_scripts filter and vetoes the strip when the detector reports WooCommerce content. Respects NEWSPACK_IGNORE_PERFMATTERS_DEFAULTS, and bails if it sees that set.
  • The global default disable_woocommerce_scripts => true is 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:

  • Uses yet another undocumented feature; while the filter seems intentional, it may not stick around in future Perfmatters releases.
  • A WC block that appears sitewide (in a sidebar, perhaps) will effectively force WC assets to load sitewide to support it. If this happens on a lot of sites, we might want to reconsider stripping these assets altogether!

How to test the changes in this Pull Request:

  1. Test suite: from plugins/newspack-plugin, run n test-php --filter Newspack_Test_WooCommerce_Content_Detector (15 tests) and n test-php --filter Newspack_Test_Perfmatters (3 tests). The full suite remains green.
  2. Manual smoke: on a site with WooCommerce and Perfmatters active, add a "Products by Category" block to a regular Page and publish it.
  3. Verify the fix: view that page on the front end — the WooCommerce stylesheets (woocommerce-general, woocommerce-layout, woocommerce-smallscreen, wc-blocks-style) load and the products render as a styled grid.
  4. Verify default behavior is unchanged: view a page with no WooCommerce content — those WooCommerce stylesheets are still stripped (Perfmatters' optimization is untouched there).
  5. Verify the override setting: define NEWSPACK_IGNORE_PERFMATTERS_DEFAULTS and confirm Newspack no longer influences Perfmatters behavior.

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Cross-repo impact review: assessed against the Newspack dependency manifest — Compatible (additive) change, no consumer references to the changed surface, no breaking impact.

@jason10lee jason10lee self-assigned this May 31, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_Detector to determine whether the current request will render WooCommerce blocks/shortcodes (including resolving template-parts and synced patterns with cycle guarding).
  • Hook perfmatters_disable_woocommerce_scripts to keep WooCommerce assets when the detector reports WooCommerce content (while respecting NEWSPACK_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.

Comment thread plugins/newspack-plugin/tests/unit-tests/woocommerce-content-detector.php Outdated
Comment thread plugins/newspack-plugin/tests/unit-tests/woocommerce-content-detector.php Outdated
Comment thread plugins/newspack-plugin/tests/unit-tests/woocommerce-content-detector.php Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

@jason10lee jason10lee marked this pull request as ready for review June 1, 2026 20:56
@jason10lee jason10lee requested a review from a team as a code owner June 1, 2026 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants