Skip to content

Commit 4101e1e

Browse files
committed
Add purge on events Nav and Collection Tree Saved (reordering)
1 parent 4caf239 commit 4101e1e

4 files changed

Lines changed: 32 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ return [
131131
'term_deleted' => true,
132132
'asset_saved' => true,
133133
'asset_deleted' => true,
134+
'collection_tree_saved' => true,
135+
'nav_tree_saved' => true,
134136
],
135137

136138
// Dispatch purge jobs to the queue instead of running synchronously

config/cloudflare-cache.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
'term_deleted' => true,
4444
'asset_saved' => true,
4545
'asset_deleted' => true,
46+
'collection_tree_saved' => true,
47+
'nav_tree_saved' => true,
4648
],
4749

4850
'queue_purge' => env('CLOUDFLARE_CACHE_QUEUE_PURGE', false), // Dispatch purge jobs to the queue

src/CloudflareCacheServiceProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Statamic\Events\TermDeleted;
1212
use Statamic\Events\AssetSaved;
1313
use Statamic\Events\AssetDeleted;
14+
use Statamic\Events\CollectionTreeSaved;
15+
use Statamic\Events\NavTreeSaved;
1416

1517
class CloudflareCacheServiceProvider extends AddonServiceProvider
1618
{
@@ -37,6 +39,12 @@ class CloudflareCacheServiceProvider extends AddonServiceProvider
3739
AssetDeleted::class => [
3840
PurgeCloudflareCache::class,
3941
],
42+
CollectionTreeSaved::class => [
43+
PurgeCloudflareCache::class,
44+
],
45+
NavTreeSaved::class => [
46+
PurgeCloudflareCache::class,
47+
],
4048
];
4149

4250
/**

src/Listeners/PurgeCloudflareCache.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Statamic\Events\TermDeleted;
1212
use Statamic\Events\AssetSaved;
1313
use Statamic\Events\AssetDeleted;
14+
use Statamic\Events\CollectionTreeSaved;
15+
use Statamic\Events\NavTreeSaved;
1416
use Statamic\Facades\URL;
1517
use Illuminate\Support\Facades\Log; // Already present, but good to confirm
1618

@@ -109,6 +111,8 @@ protected function shouldHandleEvent(Event $event): bool
109111
'Statamic\Events\TermDeleted' => 'term_deleted',
110112
'Statamic\Events\AssetSaved' => 'asset_saved',
111113
'Statamic\Events\AssetDeleted' => 'asset_deleted',
114+
'Statamic\Events\CollectionTreeSaved' => 'collection_tree_saved',
115+
'Statamic\Events\NavTreeSaved' => 'nav_tree_saved',
112116
];
113117

114118
$configKey = $eventMap[$eventClass] ?? null;
@@ -153,6 +157,22 @@ protected function getUrlsToPurge(Event $event): array
153157
}
154158
}
155159

160+
if ($event instanceof CollectionTreeSaved) {
161+
$tree = $event->tree;
162+
if ($tree && $tree->collection()) {
163+
$collection = $tree->collection();
164+
if ($collection->url()) {
165+
$urls[] = URL::makeAbsolute($collection->url());
166+
}
167+
}
168+
}
169+
170+
if ($event instanceof NavTreeSaved) {
171+
// Navigation tree saved - this happens when nav items are reordered
172+
// Since navigation appears on multiple pages, we purge everything by default
173+
// If purge_everything_fallback is disabled, this will do nothing (intended behavior)
174+
}
175+
156176
$urls = array_filter($urls);
157177

158178
return array_unique($urls);

0 commit comments

Comments
 (0)