diff --git a/src/StaticCaching/Middleware/Cache.php b/src/StaticCaching/Middleware/Cache.php index 73c8d1348cb..2abbb8f5498 100644 --- a/src/StaticCaching/Middleware/Cache.php +++ b/src/StaticCaching/Middleware/Cache.php @@ -186,4 +186,9 @@ private function createLock($request): Lock return $store->lock($key, $this->lockFor); } + + 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 d6cf8cb793e..fa6e3ed7ab6 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 { /** @@ -25,6 +27,10 @@ public function handle($expression, array $params, ?array $data = null) $context = array_merge($data, $params); + if (! Cache::isBeingUsedOnCurrentRoute()) { + return view($view, $context)->render(); + } + return $this->nocache->pushView($view, $context)->placeholder(); } } diff --git a/src/StaticCaching/NoCache/Tags.php b/src/StaticCaching/NoCache/Tags.php index 60a08a73876..8e71ae28f9b 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::isBeingUsedOnCurrentRoute()) { + return $this->parse(); + } + if ($this->params->has('select')) { $fields = $this->params->explode('select'); 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 77203a9df9f..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,9 @@ 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 = [ 'foo', // By adding @auto it will be picked up from the template. 'baz', // Explicitly selected @@ -133,6 +177,9 @@ 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. // We will not add `nope` to the session because it is not in the context. $expectedFields = ['foo', 'baz'];