diff --git a/config/static_caching.php b/config/static_caching.php index 46766739505..2af89331ea4 100644 --- a/config/static_caching.php +++ b/config/static_caching.php @@ -142,6 +142,23 @@ \Statamic\StaticCaching\Replacers\NoCacheReplacer::class, ], + /* + |-------------------------------------------------------------------------- + | Script Delivery + |-------------------------------------------------------------------------- + | + | Full measure static caching injects small ' + : ''; + } + public function shouldOutputJs(): bool { return $this->shouldOutputJs; diff --git a/src/StaticCaching/NoCache/ScriptController.php b/src/StaticCaching/NoCache/ScriptController.php new file mode 100644 index 00000000000..e3ec0a111b7 --- /dev/null +++ b/src/StaticCaching/NoCache/ScriptController.php @@ -0,0 +1,37 @@ +response($this->cacher()->getNocacheJs()); + } + + public function csrf(): Response + { + return $this->response($this->cacher()->getCsrfTokenJs()); + } + + private function cacher(): FileCacher + { + $cacher = app(Cacher::class); + + abort_unless($cacher instanceof FileCacher, 404); + + return $cacher; + } + + private function response(string $js): Response + { + return response($js) + ->header('Content-Type', 'application/javascript') + ->header('Cache-Control', 'public, max-age=3600') + ->setEtag(md5($js)); + } +} diff --git a/src/StaticCaching/Replacers/CsrfTokenReplacer.php b/src/StaticCaching/Replacers/CsrfTokenReplacer.php index 485b14564f1..27cb0f8cc0d 100644 --- a/src/StaticCaching/Replacers/CsrfTokenReplacer.php +++ b/src/StaticCaching/Replacers/CsrfTokenReplacer.php @@ -81,9 +81,7 @@ private function modifyFullMeasureResponse(Response $response) Str::position($contents, ''), ])->filter()->min(); - $js = ""; - - $contents = Str::substrReplace($contents, $js, $insertBefore, 0); + $contents = Str::substrReplace($contents, $cacher->getCsrfScript(), $insertBefore, 0); $response->setContent($contents); } diff --git a/src/StaticCaching/Replacers/NoCacheReplacer.php b/src/StaticCaching/Replacers/NoCacheReplacer.php index f7ca32fd97f..3d67e3711e2 100644 --- a/src/StaticCaching/Replacers/NoCacheReplacer.php +++ b/src/StaticCaching/Replacers/NoCacheReplacer.php @@ -94,8 +94,7 @@ private function modifyFullMeasureResponse(Response $response) $contents = $response->getContent(); if ($cacher->shouldOutputJs()) { - $js = $cacher->getNocacheJs(); - $contents = str_replace('
{{ template_content }}', '', $contents); + $contents = str_replace('', $cacher->getNocacheScript().'', $contents); } $contents = str_replace('NOCACHE_PLACEHOLDER', $cacher->getNocachePlaceholder(), $contents); diff --git a/tests/StaticCaching/ExternalScriptDeliveryTest.php b/tests/StaticCaching/ExternalScriptDeliveryTest.php new file mode 100644 index 00000000000..6884c2205ae --- /dev/null +++ b/tests/StaticCaching/ExternalScriptDeliveryTest.php @@ -0,0 +1,76 @@ +set('statamic.static_caching.strategy', 'full'); + $app['config']->set('statamic.static_caching.strategies.full.path', $this->dir = __DIR__.'/static'); + $app['config']->set('statamic.static_caching.script_delivery', 'external'); + + File::delete($this->dir); + } + + public function tearDown(): void + { + File::delete($this->dir); + parent::tearDown(); + } + + #[Test] + public function it_references_the_csrf_and_nocache_scripts_instead_of_inlining_them() + { + $this->withFakeViews(); + $this->viewShouldReturnRaw('layout', '