fix(newspack-plugin): cache-bust assets by content hash#290
Open
dkoo wants to merge 15 commits into
Open
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
… hash Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves cache-busting for Newspack Plugin JS/CSS assets by using the content-hash version emitted into dist/*.asset.php (instead of relying solely on NEWSPACK_PLUGIN_VERSION), and updates enqueue call sites accordingly to prevent stale CDN-cached assets across redeploys.
Changes:
- Add
Newspack::asset_version( string $name ): stringto resolve per-asset hashed versions fromdist/<name>.asset.php, with fallback toNEWSPACK_PLUGIN_VERSION. - Convert numerous script/style enqueues across admin, frontend, wizards, and integrations to use the new helper for the
verargument. - Add PHPUnit coverage for the helper, including fixtures and “missing/malformed asset file” fallbacks.
Reviewed changes
Copilot reviewed 39 out of 39 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| plugins/newspack-plugin/includes/class-newspack.php | Adds asset_version() helper and applies it to common assets. |
| plugins/newspack-plugin/tests/unit-tests/asset-version.php | Adds unit tests covering helper behavior (valid, malformed, missing, nested paths). |
| plugins/newspack-plugin/src/blocks/reader-registration/index.php | Switch block JS/CSS versioning to asset_version(). |
| plugins/newspack-plugin/src/blocks/my-account-button/class-my-account-button-block.php | Switch blocks CSS versioning to asset_version(). |
| plugins/newspack-plugin/src/blocks/content-gate/countdown/class-content-gate-countdown-block.php | Switch countdown block script versioning to asset_version(). |
| plugins/newspack-plugin/includes/wizards/traits/trait-wizards-admin-header.php | Replace direct .asset.php include with asset_version() for admin-header assets. |
| plugins/newspack-plugin/includes/wizards/newsletters/class-newsletters-wizard.php | Switch wizard script versioning to asset_version(). |
| plugins/newspack-plugin/includes/wizards/class-wizard.php | Switch wizards bundle versioning to asset_version() and remove unused include. |
| plugins/newspack-plugin/includes/wizards/class-setup-wizard.php | Switch setup wizard JS/CSS versioning to asset_version(). |
| plugins/newspack-plugin/includes/wizards/class-components-demo.php | Switch components demo script versioning to asset_version(). |
| plugins/newspack-plugin/includes/wizards/audience/class-audience-wizard.php | Switch content-banner preview CSS versioning to asset_version(). |
| plugins/newspack-plugin/includes/wizards/audience/class-audience-content-gates.php | Switch content-banner preview CSS versioning to asset_version(). |
| plugins/newspack-plugin/includes/wizards/advertising/class-advertising-sponsors.php | Switch billboard CSS versioning to asset_version(). |
| plugins/newspack-plugin/includes/wizards/advertising/class-advertising-display-ads.php | Switch billboard JS/CSS versioning to asset_version(). |
| plugins/newspack-plugin/includes/revisions-control/class-major-revisions.php | Fix wp_enqueue_style() args and switch revisions-control assets to asset_version(). |
| plugins/newspack-plugin/includes/reader-activation/class-reader-activation.php | Switch reader-activation/auth/newsletters-signup assets to asset_version(). |
| plugins/newspack-plugin/includes/polyfills/class-amp-polyfills.php | Switch lightbox assets to asset_version() (nested dist path). |
| plugins/newspack-plugin/includes/plugins/woocommerce/my-account/class-woocommerce-my-account.php | Switch my-account script versioning to asset_version(). |
| plugins/newspack-plugin/includes/plugins/woocommerce/my-account/class-my-account-ui-v1.php | Switch v1 account assets to asset_version(). |
| plugins/newspack-plugin/includes/plugins/woocommerce/my-account/class-my-account-ui-v0.php | Switch v0 account CSS versioning to asset_version(). |
| plugins/newspack-plugin/includes/plugins/woocommerce/class-woocommerce-products.php | Switch custom product options script versioning to asset_version(). |
| plugins/newspack-plugin/includes/plugins/woocommerce/class-woocommerce-cover-fees.php | Switch cover-fees script versioning to asset_version(). |
| plugins/newspack-plugin/includes/plugins/woocommerce-subscriptions/group-subscription/class-group-subscription-settings.php | Switch group subscription admin assets to asset_version(). |
| plugins/newspack-plugin/includes/plugins/co-authors-plus/class-nicename-change-ui.php | Switch nicename-change assets to asset_version(). |
| plugins/newspack-plugin/includes/plugins/co-authors-plus/class-guest-contributor-role.php | Switch co-authors-plus admin script versioning to asset_version(). |
| plugins/newspack-plugin/includes/plugins/class-organic-profile-block.php | Switch organic profile block CSS versioning to asset_version(). |
| plugins/newspack-plugin/includes/optional-modules/class-nextdoor.php | Switch nextdoor editor assets to asset_version(). |
| plugins/newspack-plugin/includes/emails/class-emails.php | Switch emails editor assets to asset_version(). |
| plugins/newspack-plugin/includes/corrections/class-corrections.php | Switch corrections assets to asset_version(). |
| plugins/newspack-plugin/includes/content-gate/class-content-gate.php | Switch content-banner JS/CSS versioning to asset_version(). |
| plugins/newspack-plugin/includes/content-gate/class-block-patterns.php | Switch block-patterns CSS versioning to asset_version(). |
| plugins/newspack-plugin/includes/collections/class-enqueuer.php | Switch collections script/style versioning to asset_version(). |
| plugins/newspack-plugin/includes/class-salesforce.php | Switch salesforce script versioning to asset_version(). |
| plugins/newspack-plugin/includes/class-recaptcha.php | Switch reCAPTCHA assets to asset_version(). |
| plugins/newspack-plugin/includes/class-newspack-ui.php | Switch newspack-ui JS/CSS versioning to asset_version(). |
| plugins/newspack-plugin/includes/class-handoff-banner.php | Switch handoff-banner CSS versioning to asset_version(). |
| plugins/newspack-plugin/includes/class-blocks.php | Switch blocks editor/frontend assets to asset_version(). |
| plugins/newspack-plugin/includes/class-admin-plugins-screen.php | Switch plugins screen assets to asset_version(). |
| plugins/newspack-plugin/includes/bylines/class-bylines.php | Switch bylines editor assets to asset_version(). |
…sion() Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
…ueue Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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:
WP-admin (and some reader-facing) JS/CSS assets emitted by Newspack Plugin can go stale at the CDN even after a new build is deployed. Previously every enqueue passed
NEWSPACK_PLUGIN_VERSIONas theverquery string, which only bumps on release — so alpha builds, hotfixes, and same-version redeploys all reused the same URL and inherited the previous cache entry. (Atomic edges do honor?ver=; the failure mode was the version not changing, not query-string stripping.)Webpack already emits a per-bundle content hash into
dist/<name>.asset.php(via@wordpress/dependency-extraction-webpack-plugin). This PR introduces a small helper that reads it and routes every dist-asset enqueue through it, so?ver=changes whenever the bundle bytes change — independent of the plugin version.Newspack::asset_version( string $name ): string— memoized per request, readsdist/<name>.asset.php, falls back toNEWSPACK_PLUGIN_VERSIONwhen missing or malformed. 5 unit tests, including deterministic fixtures so the suite is environment-independent and CI-safe (works even whendist/is absent on the runner).includes/andsrc/blocks/. Each swap matches the asset URL on the same call and matches the file's existingNewspack::/\Newspack\Newspack::qualification style.class-major-revisions.phphad a pre-existing malformedwp_register_style()call (version constant in deps position,trueas version). Nowdeps=[], version=helper.trait-wizards-admin-header.phphad a rawincludeofadmin-header.asset.phpwith nofile_existsguard. The helper supersedes it; the include is removed.class-wizard.phphad a stale$asset_file = include …whose value was never read. Removed.newspack-blocks-frontend(dist/blocks.css) is registered from two files; both now agree on the content hash.class-handoff-banner.phpenqueues a raw/src/wizards/handoff-banner/block-editor.js(no.asset.phpexists for it) — keepsNEWSPACK_PLUGIN_VERSION. The helper's fallback handles the same case automatically everywhere else.Future improvement deferred to a follow-up: moving entries to content-hashed filenames (
wizards.[contenthash].js) so URL paths themselves change. Atomic respects?ver=, so this PR is expected to fully resolve the symptoms on our fleet; hashed filenames would only add robustness on publisher-fronted third-party CDNs configured to ignore query strings.Sibling plugins (newspack-blocks, newspack-popups, newspack-newsletters, newspack-ads, newspack-network) almost certainly have the same code shape and should get the same treatment in follow-up PRs.
Closes #
How to test the changes in this Pull Request:
n env create cachebust --worktree newspack-plugin:fix/newspack-plugin-asset-content-hash --domain cachebust.localn env up cachebust --buildn setup --env cachebust --yes(optional, for a fully bootstrapped site)commonsandwizardsmatching theversionin their respectivedist/*.asset.phpfiles; the missing asset returns the plugin version (e.g.6.42.4).https://cachebust.local/and view source. Search for anynewspack-plugin/dist/.../*.jsor.cssURL — every?ver=…should be a 20-char hex hash, not a semver string. Reader-facing assets to grep for:reader-activation.js,reader-auth.js,newsletters-signup.js,newspack-ui.js,content-banner.js,content-gate-countdown-block.js,reader-registration-block.js.https://cachebust.local/wp-admin/and load:admin.php?page=newspack-dashboard—wizards.js,commons.js,admin.css,admin-header.js/.cssall carry hashes.admin.php?page=newspack-setup-wizard—setup.js/.csshashed.admin.php?page=newspack-newsletters-wizard—newsletters.jshashed.admin.php?page=advertising-display-ads(or similar advertising page) —billboard.js/.csshashed.plugins-screen.js/.csshashed.plugins/newspack-plugin/src/wizards/, rebuild (n build newspack-pluginfrom the workspace root), reload the dashboard, and confirm the?ver=…forwizards.jschanges to a different hash. Without rebuilding, the hash should stay the same across reloads (deterministic).dist/aside inside the container, reload any admin page — every?ver=should fall back to the plugin's semver. Movedist/back.docker exec newspack_env_cachebust /var/scripts/test-php.sh newspack-plugin→ expect 1499 tests, 1 unrelated skip, no failures. Includes 5 new tests intests/unit-tests/asset-version.php.dist/is present).reader-activation,reader-auth,newsletters-signup,newspack-ui. No JS console errors related to handle/dependency mismatches.dist/*.js/.css; no console errors.Other information: