Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Fallback to nocache content when request is missing the Cache middleware #9406

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/StaticCaching/Middleware/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
6 changes: 6 additions & 0 deletions src/StaticCaching/NoCache/BladeDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Statamic\StaticCaching\NoCache;

use Statamic\StaticCaching\Middleware\Cache;

class BladeDirective
{
/**
Expand All @@ -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();
}
}
5 changes: 5 additions & 0 deletions src/StaticCaching/NoCache/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\StaticCaching\NoCache;

use Statamic\Facades\Antlers;
use Statamic\StaticCaching\Middleware\Cache;

class Tags extends \Statamic\Tags\Tags
{
Expand All @@ -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');

Expand Down
20 changes: 2 additions & 18 deletions tests/StaticCaching/NocacheRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,19 @@ 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()
->assertExactJson([
'csrf' => csrf_token(),
'regions' => [
$regionOne->key() => 'First 1 Dustin Test',
$regionTwo->key() => $secondExpectation,
$regionTwo->key() => 'Second 2 Will Test',
],
]);
}
Expand Down
47 changes: 47 additions & 0 deletions tests/StaticCaching/NocacheTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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
Expand All @@ -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'];
Expand Down
Loading