[6.x] Allow static cache JS to be externalized - #15342
Open
ryanmitchell wants to merge 1 commit into
Open
Conversation
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.
Fixes #8802.
Background
Full-measure (file) static caching injects two small inline
<script>blocks into every cached page — one to swap the placeholder CSRF token, one to hydratenocacheregions. A site sendingContent-Security-Policy: script-src 'self'blocks inline scripts, so full static caching can't be used with that policy.What this does
Adds a
script_deliveryoption toconfig/static_caching.php:The two routes (statamic.nocache.js, statamic.csrf.js, under the action-route prefix) are only registered when script_delivery is external. They respond with application/javascript and an ETag.
The script tag is a publishable view
The JS snippets moved out of heredocs in FileCacher into Blade views, and the <script> tag itself is now a view too:
static-caching/csrf-js.blade.php: CSRF snippet body
static-caching/nocache-js.blade.php: nocache snippet body
static-caching/script.blade.php: the <script> tag wrapper (shared by both)
publishable using:
php artisan vendor:publish --tag=statamic-static-cachingscript.blade.php is where you'd add a nonce or integrity attribute for a stricter policy, for example:
The tag is rendered during the cache-miss request, so any per-request/per-cache value your middleware has set is available — no new Statamic API needed. The existing StaticCache::nocacheJs() / csrfTokenJs() overrides still work.
Backwards compatibility
Default is inline; existing sites see identical output. No breaking changes.