Skip to content

Commit e763dd4

Browse files
[5.x] Invalidate nav's URL cache when collection/taxonomy/etc is created (#13297)
1 parent 7e8a0a9 commit e763dd4

4 files changed

Lines changed: 33 additions & 12 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Statamic\Listeners;
4+
5+
use Statamic\Events\AssetContainerCreated;
6+
use Statamic\Events\CollectionCreated;
7+
use Statamic\Events\GlobalSetCreated;
8+
use Statamic\Events\NavCreated;
9+
use Statamic\Events\Subscriber;
10+
use Statamic\Events\TaxonomyCreated;
11+
use Statamic\Facades\CP\Nav;
12+
13+
class InvalidateNavCache extends Subscriber
14+
{
15+
protected $listeners = [
16+
CollectionCreated::class => 'invalidate',
17+
NavCreated::class => 'invalidate',
18+
TaxonomyCreated::class => 'invalidate',
19+
AssetContainerCreated::class => 'invalidate',
20+
GlobalSetCreated::class => 'invalidate',
21+
];
22+
23+
public function invalidate($event): void
24+
{
25+
Nav::clearCachedUrls();
26+
}
27+
}

src/Providers/EventServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class EventServiceProvider extends ServiceProvider
3737
\Statamic\Listeners\GeneratePresetImageManipulations::class,
3838
\Statamic\Listeners\UpdateAssetReferences::class,
3939
\Statamic\Listeners\UpdateTermReferences::class,
40+
\Statamic\Listeners\InvalidateNavCache::class,
4041
];
4142

4243
public function register()

tests/CP/Navigation/ActiveNavItemTest.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,9 @@ public function it_updates_caches_when_new_child_urls_are_detected_after_resolvi
110110
// Now let's create a new collection
111111
Facades\Collection::make('products')->title('Products')->save();
112112

113-
// Simply building the nav should change what is cached
114-
$collectionsChildrenUrls = [
115-
'http://localhost/cp/collections/articles',
116-
'http://localhost/cp/collections/pages',
117-
];
118-
$this->assertEquals($collectionsChildrenUrls, Cache::get(NavBuilder::UNRESOLVED_CHILDREN_URLS_CACHE_KEY)->get('content::collections'));
119-
$this->assertEquals($collectionsChildrenUrls, Blink::get(NavBuilder::UNRESOLVED_CHILDREN_URLS_CACHE_KEY)->get('content::collections'));
120-
collect($collectionsChildrenUrls)->each(function ($url) {
121-
$this->assertTrue(Cache::get(NavBuilder::ALL_URLS_CACHE_KEY)->contains($url));
122-
$this->assertTrue(Blink::get(NavBuilder::ALL_URLS_CACHE_KEY)->contains($url));
123-
});
113+
// The InvalidateNavCache subscriber will clear the URLs cache.
114+
$this->assertNull(Cache::get(NavBuilder::UNRESOLVED_CHILDREN_URLS_CACHE_KEY));
115+
$this->assertNull(Blink::get(NavBuilder::UNRESOLVED_CHILDREN_URLS_CACHE_KEY));
124116

125117
// But if we build the nav again by hitting collections url to resolve its' children, the caches should get updated
126118
$this

tests/TestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ protected function setUp(): void
3838

3939
if ($this->shouldPreventNavBeingBuilt) {
4040
\Statamic\Facades\CP\Nav::shouldReceive('build')->zeroOrMoreTimes()->andReturn(collect());
41-
$this->addToAssertionCount(-1); // Dont want to assert this
41+
\Statamic\Facades\CP\Nav::shouldReceive('clearCachedUrls')->zeroOrMoreTimes();
42+
$this->addToAssertionCount(-2); // Dont want to assert this
4243
}
4344

4445
$this->addGqlMacros();

0 commit comments

Comments
 (0)