From 2a7af9858026471ed1bc9339351630734c382b50 Mon Sep 17 00:00:00 2001 From: Jack McDade Date: Thu, 3 Sep 2026 14:53:48 -0400 Subject: [PATCH] Store nocache views in Statamic temporary storage --- src/Providers/ViewServiceProvider.php | 2 +- src/StaticCaching/NoCache/StringFragment.php | 2 +- src/View/Blade/AntlersBladePrecompiler.php | 3 +- src/View/Blade/Concerns/CompilesNocache.php | 5 +-- tests/StaticCaching/IncludeNocacheTest.php | 4 ++- tests/StaticCaching/StringFragmentTest.php | 28 +++++++++++++++++ tests/View/Blade/NocacheStorageTest.php | 33 ++++++++++++++++++++ 7 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 tests/StaticCaching/StringFragmentTest.php create mode 100644 tests/View/Blade/NocacheStorageTest.php diff --git a/src/Providers/ViewServiceProvider.php b/src/Providers/ViewServiceProvider.php index 2ebcba95062..0f6745d4644 100644 --- a/src/Providers/ViewServiceProvider.php +++ b/src/Providers/ViewServiceProvider.php @@ -425,7 +425,7 @@ public function registerBladeDirectives() public function boot() { - ViewFactory::addNamespace('compiled__views', storage_path('framework/views')); + ViewFactory::addNamespace('compiled__views', storage_path('statamic/tmp/nocache')); Region::preserveContextKeys(IncludeTag::VIEW_DATA_KEYS); diff --git a/src/StaticCaching/NoCache/StringFragment.php b/src/StaticCaching/NoCache/StringFragment.php index fd9f22eef1c..e6c1ebf7215 100644 --- a/src/StaticCaching/NoCache/StringFragment.php +++ b/src/StaticCaching/NoCache/StringFragment.php @@ -20,7 +20,7 @@ public function __construct($region, $contents, $extension, $data) $this->contents = $contents; $this->extension = $extension; $this->data = $data; - $this->directory = config('view.compiled').'/nocache'; + $this->directory = storage_path('statamic/tmp/nocache'); } public function render(): string diff --git a/src/View/Blade/AntlersBladePrecompiler.php b/src/View/Blade/AntlersBladePrecompiler.php index 1fc52c5aaa0..5ff37103afd 100644 --- a/src/View/Blade/AntlersBladePrecompiler.php +++ b/src/View/Blade/AntlersBladePrecompiler.php @@ -3,6 +3,7 @@ namespace Statamic\View\Blade; use Illuminate\Support\Str; +use Statamic\Facades\File; class AntlersBladePrecompiler { @@ -27,7 +28,7 @@ public static function compile(string $content): string $contentHash = sha1($innerContent); $fileName = 'antlers_'.$contentHash; - file_put_contents(storage_path('framework/views/'.$fileName.'.antlers.html'), $innerContent); + File::put(storage_path('statamic/tmp/nocache/'.$fileName.'.antlers.html'), $innerContent); $content = str_replace($original, '@include(\'compiled__views::'.$fileName.'\')', $content); } diff --git a/src/View/Blade/Concerns/CompilesNocache.php b/src/View/Blade/Concerns/CompilesNocache.php index 50b07ab83d9..096722c0d19 100644 --- a/src/View/Blade/Concerns/CompilesNocache.php +++ b/src/View/Blade/Concerns/CompilesNocache.php @@ -2,6 +2,7 @@ namespace Statamic\View\Blade\Concerns; +use Statamic\Facades\File; use Statamic\View\Blade\StatamicTagCompiler; use Stillat\BladeParser\Nodes\Components\ComponentNode; @@ -11,8 +12,8 @@ protected function compileNocache(ComponentNode $component): string { $compiled = (new StatamicTagCompiler())->compile($component->innerDocumentContent); $viewName = '_nocache'.sha1($compiled); - $path = storage_path('framework/views/'.$viewName.'.blade.php'); - file_put_contents($path, $compiled); + $path = storage_path('statamic/tmp/nocache/'.$viewName.'.blade.php'); + File::put($path, $compiled); return '@nocache(\'compiled__views::'.$viewName.'\')'; } diff --git a/tests/StaticCaching/IncludeNocacheTest.php b/tests/StaticCaching/IncludeNocacheTest.php index be1919b288a..9fcf0935376 100644 --- a/tests/StaticCaching/IncludeNocacheTest.php +++ b/tests/StaticCaching/IncludeNocacheTest.php @@ -39,11 +39,13 @@ private function setUpViews(string $layout, array $views) private function setUpBladeViews(array $views) { + app('files')->cleanDirectory(config('view.compiled')); + foreach ($views as $name => $contents) { $this->viewShouldReturnRaw($name, $contents, 'blade.php'); } - view()->addNamespace('compiled__views', storage_path('framework/views')); + view()->addNamespace('compiled__views', storage_path('statamic/tmp/nocache')); } private function renderTwice(string $layout, array $views): array diff --git a/tests/StaticCaching/StringFragmentTest.php b/tests/StaticCaching/StringFragmentTest.php new file mode 100644 index 00000000000..d05eeb7a323 --- /dev/null +++ b/tests/StaticCaching/StringFragmentTest.php @@ -0,0 +1,28 @@ +set('view.compiled', $compiledViewDirectory); + + try { + $fragment = new StringFragment('test-region', 'Hello {{ name }}', 'antlers.html', ['name' => 'World']); + + $this->assertSame('Hello World', $fragment->render()); + $this->assertDirectoryExists(storage_path('statamic/tmp/nocache')); + $this->assertDirectoryDoesNotExist($compiledViewDirectory.'/nocache'); + } finally { + app('files')->deleteDirectory($compiledViewDirectory); + } + } +} diff --git a/tests/View/Blade/NocacheStorageTest.php b/tests/View/Blade/NocacheStorageTest.php new file mode 100644 index 00000000000..9a631c38c2d --- /dev/null +++ b/tests/View/Blade/NocacheStorageTest.php @@ -0,0 +1,33 @@ +compile('Hello'); + $viewName = '_nocache'.sha1('Hello'); + + $this->assertSame("@nocache('compiled__views::{$viewName}')", $compiled); + $this->assertSame('Hello', file_get_contents(storage_path("statamic/tmp/nocache/{$viewName}.blade.php"))); + } + + #[Test] + public function it_stores_precompiled_antlers_views_in_statamic_storage() + { + $antlers = 'Hello {{ name }}'; + $viewName = 'antlers_'.sha1($antlers); + + $compiled = AntlersBladePrecompiler::compile("@antlers{$antlers}@endantlers"); + + $this->assertSame("@include('compiled__views::{$viewName}')", $compiled); + $this->assertSame($antlers, file_get_contents(storage_path("statamic/tmp/nocache/{$viewName}.antlers.html"))); + } +}