From faf29f1f4c07efa62efcab4d91a20c5f8e954b7d Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Fri, 26 Jan 2024 15:07:12 +0000 Subject: [PATCH 1/7] When request is missing middleware, fallback to nocache contents --- src/StaticCaching/Middleware/Cache.php | 4 ++++ src/StaticCaching/NoCache/Tags.php | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/StaticCaching/Middleware/Cache.php b/src/StaticCaching/Middleware/Cache.php index 7b889214ab0..21dac8b812e 100644 --- a/src/StaticCaching/Middleware/Cache.php +++ b/src/StaticCaching/Middleware/Cache.php @@ -19,6 +19,8 @@ class Cache { + public static bool $requestCanBeCached = false; + /** * @var Cacher */ @@ -43,6 +45,8 @@ public function __construct(Cacher $cacher, Session $nocache) */ public function handle($request, Closure $next) { + static::$requestCanBeCached = true; + $lock = $this->createLock($request); while (! $lock->acquire()) { diff --git a/src/StaticCaching/NoCache/Tags.php b/src/StaticCaching/NoCache/Tags.php index 60a08a73876..91d17e71f7f 100644 --- a/src/StaticCaching/NoCache/Tags.php +++ b/src/StaticCaching/NoCache/Tags.php @@ -3,6 +3,7 @@ namespace Statamic\StaticCaching\NoCache; use Statamic\Facades\Antlers; +use Statamic\StaticCaching\Middleware\Cache; class Tags extends \Statamic\Tags\Tags { @@ -21,6 +22,10 @@ public function __construct(Session $nocache) public function index() { + if (! Cache::$requestCanBeCached) { + return $this->parse($this->context->toArray()); + } + if ($this->params->has('select')) { $fields = $this->params->explode('select'); From ba4cdd9fd98d16abfcbf94b86553514c1e147cee Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Fri, 26 Jan 2024 15:14:33 +0000 Subject: [PATCH 2/7] When request is missing middleware, fallback to nocache view --- src/StaticCaching/NoCache/BladeDirective.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/StaticCaching/NoCache/BladeDirective.php b/src/StaticCaching/NoCache/BladeDirective.php index d6cf8cb793e..fad44219f38 100644 --- a/src/StaticCaching/NoCache/BladeDirective.php +++ b/src/StaticCaching/NoCache/BladeDirective.php @@ -2,6 +2,8 @@ namespace Statamic\StaticCaching\NoCache; +use Statamic\StaticCaching\Middleware\Cache; + class BladeDirective { /** @@ -22,9 +24,12 @@ public function handle($expression, array $params, ?array $data = null) } $view = $expression; - $context = array_merge($data, $params); + if (! Cache::$requestCanBeCached) { + return view($view, $context)->render(); + } + return $this->nocache->pushView($view, $context)->placeholder(); } } From 066213cd1e41ca1ffcf997a81d492ef14d07bdb2 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 26 Jan 2024 13:53:25 -0500 Subject: [PATCH 3/7] avoid a static variable --- src/StaticCaching/Middleware/Cache.php | 9 +++++---- src/StaticCaching/NoCache/BladeDirective.php | 2 +- src/StaticCaching/NoCache/Tags.php | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/StaticCaching/Middleware/Cache.php b/src/StaticCaching/Middleware/Cache.php index 21dac8b812e..ea9421e6836 100644 --- a/src/StaticCaching/Middleware/Cache.php +++ b/src/StaticCaching/Middleware/Cache.php @@ -19,8 +19,6 @@ class Cache { - public static bool $requestCanBeCached = false; - /** * @var Cacher */ @@ -45,8 +43,6 @@ public function __construct(Cacher $cacher, Session $nocache) */ public function handle($request, Closure $next) { - static::$requestCanBeCached = true; - $lock = $this->createLock($request); while (! $lock->acquire()) { @@ -166,4 +162,9 @@ private function createLock($request) return $locks->createLock($key, 30); } + + public static function isBeingUsedOnCurrentRoute() + { + return in_array(static::class, app('router')->gatherRouteMiddleware(request()->route())); + } } diff --git a/src/StaticCaching/NoCache/BladeDirective.php b/src/StaticCaching/NoCache/BladeDirective.php index fad44219f38..6f16fdfa868 100644 --- a/src/StaticCaching/NoCache/BladeDirective.php +++ b/src/StaticCaching/NoCache/BladeDirective.php @@ -26,7 +26,7 @@ public function handle($expression, array $params, ?array $data = null) $view = $expression; $context = array_merge($data, $params); - if (! Cache::$requestCanBeCached) { + if (! Cache::isBeingUsedOnCurrentRoute()) { return view($view, $context)->render(); } diff --git a/src/StaticCaching/NoCache/Tags.php b/src/StaticCaching/NoCache/Tags.php index 91d17e71f7f..5a6b13abe7c 100644 --- a/src/StaticCaching/NoCache/Tags.php +++ b/src/StaticCaching/NoCache/Tags.php @@ -22,7 +22,7 @@ public function __construct(Session $nocache) public function index() { - if (! Cache::$requestCanBeCached) { + if (! Cache::isBeingUsedOnCurrentRoute()) { return $this->parse($this->context->toArray()); } From bc378c81e14752fb153e81e3fb2e11fc88042869 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 26 Jan 2024 13:54:00 -0500 Subject: [PATCH 4/7] parse will get the context automatically --- src/StaticCaching/NoCache/Tags.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StaticCaching/NoCache/Tags.php b/src/StaticCaching/NoCache/Tags.php index 5a6b13abe7c..8e71ae28f9b 100644 --- a/src/StaticCaching/NoCache/Tags.php +++ b/src/StaticCaching/NoCache/Tags.php @@ -23,7 +23,7 @@ public function __construct(Session $nocache) public function index() { if (! Cache::isBeingUsedOnCurrentRoute()) { - return $this->parse($this->context->toArray()); + return $this->parse(); } if ($this->params->has('select')) { From b2da6d859b3d8237897bcc9eec6468ebb6602dc6 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 26 Jan 2024 14:16:56 -0500 Subject: [PATCH 5/7] hit a route with the cache middleware in the test --- tests/StaticCaching/NocacheTagsTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/StaticCaching/NocacheTagsTest.php b/tests/StaticCaching/NocacheTagsTest.php index 5929cb28824..0e6b741e4d1 100644 --- a/tests/StaticCaching/NocacheTagsTest.php +++ b/tests/StaticCaching/NocacheTagsTest.php @@ -106,6 +106,8 @@ public function it_can_keep_nested_nocache_tags_dynamic_inside_cache_tags() /** @test */ public function it_only_adds_appropriate_fields_of_context_to_session() { + $this->get('/'); + $expectedFields = [ 'foo', // By adding @auto it will be picked up from the template. 'baz', // Explicitly selected @@ -132,6 +134,8 @@ public function it_only_adds_appropriate_fields_of_context_to_session() /** @test */ public function it_only_adds_explicitly_defined_fields_of_context_to_session() { + $this->get('/'); + // We will not add `bar` to the session because it is not explicitly defined. // We will not add `nope` to the session because it is not in the context. $expectedFields = ['foo', 'baz']; From dd8faff9be73f3d214b50c0068e73265840ec0d3 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 1 Aug 2024 16:44:02 -0400 Subject: [PATCH 6/7] Adjust tests to better reflect reality ... Made the route test just care about replacing regions. Move the nested logic to a different test. If there was a nested nocache tag, it wouldn't be in the view. It would have been converted to a placeholder string. --- tests/StaticCaching/NocacheRouteTest.php | 20 ++--------- tests/StaticCaching/NocacheTagsTest.php | 43 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/tests/StaticCaching/NocacheRouteTest.php b/tests/StaticCaching/NocacheRouteTest.php index 7f648dd23ab..56397021093 100644 --- a/tests/StaticCaching/NocacheRouteTest.php +++ b/tests/StaticCaching/NocacheRouteTest.php @@ -37,27 +37,11 @@ public function index() $this->createPage('test', ['with' => ['title' => 'Test']]); - $secondTemplate = <<<'EOT' -Second {{ example_count }} {{ name }} {{ title }} -{{ nocache }} - Nested {{ example_count }} {{ name }} {{ title }} - {{ nocache }} - Double nested {{ example_count }} {{ name }} {{ title }} - {{ /nocache }} -{{ /nocache }} -EOT; - $session = new Session('http://localhost/test'); $regionOne = $session->pushRegion('First {{ example_count }} {{ name }} {{ title }}', ['name' => 'Dustin'], 'antlers.html'); - $regionTwo = $session->pushRegion($secondTemplate, ['name' => 'Will'], 'antlers.html'); + $regionTwo = $session->pushRegion('Second {{ example_count }} {{ name }} {{ title }}', ['name' => 'Will'], 'antlers.html'); $session->write(); - $secondExpectation = <<<'EOT' -Second 2 Will Test -Nested 3 Will Test - Double nested 4 Will Test -EOT; - $this ->postJson('/!/nocache', ['url' => 'http://localhost/test']) ->assertOk() @@ -65,7 +49,7 @@ public function index() 'csrf' => csrf_token(), 'regions' => [ $regionOne->key() => 'First 1 Dustin Test', - $regionTwo->key() => $secondExpectation, + $regionTwo->key() => 'Second 2 Will Test', ], ]); } diff --git a/tests/StaticCaching/NocacheTagsTest.php b/tests/StaticCaching/NocacheTagsTest.php index a7517c4a489..e21daa639f9 100644 --- a/tests/StaticCaching/NocacheTagsTest.php +++ b/tests/StaticCaching/NocacheTagsTest.php @@ -25,6 +25,47 @@ protected function getEnvironmentSetUp($app) $app['config']->set('statamic.static_caching.strategy', null); } + #[Test] + public function it_can_nest_nocache_tags() + { + $this->withStandardFakeViews(); + + $template = <<<'EOT' +{{ title }} +{{ nocache }} + {{ title }} + {{ nocache }} + {{ title }} + {{ nocache }}{{ title }}{{ /nocache }} + {{ /nocache }} +{{ /nocache }} +EOT; + + $this->viewShouldReturnRaw('default', $template); + + $page = $this->createPage('about', [ + 'with' => [ + 'title' => 'Existing', + ], + ]); + + $this + ->get('/about') + ->assertOk() + ->assertSeeInOrder(['Existing', 'Existing', 'Existing', 'Existing']); + + $page + ->set('title', 'Updated') + ->saveQuietly(); // Save quietly to prevent the invalidator from clearing the statically cached page. + + $this->app->make(Session::class)->reset(); + + $this + ->get('/about') + ->assertOk() + ->assertSeeInOrder(['Updated', 'Updated', 'Updated', 'Updated']); + } + #[Test] public function it_can_keep_nocache_tags_dynamic_inside_cache_tags() { @@ -107,6 +148,7 @@ public function it_can_keep_nested_nocache_tags_dynamic_inside_cache_tags() #[Test] public function it_only_adds_appropriate_fields_of_context_to_session() { + // The tag won't do anything if it's not being used on a request with the cache middleware. $this->get('/'); $expectedFields = [ @@ -135,6 +177,7 @@ public function it_only_adds_appropriate_fields_of_context_to_session() #[Test] public function it_only_adds_explicitly_defined_fields_of_context_to_session() { + // The tag won't do anything if it's not being used on a request with the cache middleware. $this->get('/'); // We will not add `bar` to the session because it is not explicitly defined. From 07b16f9f0b3db89f46b0f149e3b2c1ff34c2a091 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 1 Aug 2024 16:50:36 -0400 Subject: [PATCH 7/7] less diff, for erin --- src/StaticCaching/NoCache/BladeDirective.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/StaticCaching/NoCache/BladeDirective.php b/src/StaticCaching/NoCache/BladeDirective.php index 6f16fdfa868..fa6e3ed7ab6 100644 --- a/src/StaticCaching/NoCache/BladeDirective.php +++ b/src/StaticCaching/NoCache/BladeDirective.php @@ -24,6 +24,7 @@ public function handle($expression, array $params, ?array $data = null) } $view = $expression; + $context = array_merge($data, $params); if (! Cache::isBeingUsedOnCurrentRoute()) {