Skip to content

Commit a2e32e3

Browse files
authored
Handle nav tag tracking edge cases (#32)
2 parents 6ac81a8 + 65aec34 commit a2e32e3

File tree

2 files changed

+150
-1
lines changed

2 files changed

+150
-1
lines changed

src/Http/Middleware/CacheTracker.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Statamic\Facades\URL;
1414
use Statamic\Forms;
1515
use Statamic\StaticCaching\Cacher;
16+
use Statamic\Structures\Nav;
1617
use Statamic\Structures\Page;
1718
use Statamic\Support\Str;
1819
use Statamic\Tags;
@@ -181,7 +182,13 @@ private function setupTagHooks()
181182
});
182183

183184
Tags\Nav::hook('init', function ($value, $next) use ($self) {
184-
$handle = $this->params->get('handle') ? 'nav:'.$this->params->get('handle') : $this->tag;
185+
$handle = $this->params->get('handle', $this->tag != 'nav:index' ? Str::after($this->tag, 'nav:') : 'collection::pages');
186+
187+
if ($handle instanceof Nav) {
188+
$handle = $handle->handle();
189+
}
190+
191+
$handle = 'nav:'.$handle;
185192
$self->addContentTag($handle);
186193

187194
return $next($value);

tests/Unit/NavTagTest.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace Thoughtco\StatamicCacheTracker\Tests\Unit;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use Statamic\Facades;
7+
use Thoughtco\StatamicCacheTracker\Facades\Tracker;
8+
use Thoughtco\StatamicCacheTracker\Tests\TestCase;
9+
10+
class NavTagTest extends TestCase
11+
{
12+
#[Test]
13+
public function it_tracks_nav_tag_with_explicit_handle()
14+
{
15+
// Create a navigation structure
16+
Facades\Nav::make('footer')
17+
->title('Footer')
18+
->expectsRoot(true)
19+
->collections(['pages'])
20+
->save();
21+
22+
$view = <<<'BLADE'
23+
{{ nav handle="footer" }}
24+
{{ title }}
25+
{{ /nav }}
26+
BLADE;
27+
28+
file_put_contents($this->viewPath('nav-test.antlers.html'), $view);
29+
30+
Facades\Entry::make()
31+
->id('nav-test-page')
32+
->slug('nav-test')
33+
->collection('pages')
34+
->data(['template' => 'nav-test'])
35+
->save();
36+
37+
$this->get('/nav-test');
38+
39+
$tags = collect(Tracker::all())->first()['tags'] ?? [];
40+
41+
$this->assertContains('nav:footer', $tags);
42+
}
43+
44+
#[Test]
45+
public function it_tracks_nav_tag_with_shorthand_handle()
46+
{
47+
// Create a navigation structure
48+
Facades\Nav::make('footer')
49+
->title('Footer')
50+
->expectsRoot(true)
51+
->collections(['pages'])
52+
->save();
53+
54+
$view = <<<'BLADE'
55+
{{ nav:footer }}
56+
{{ title }}
57+
{{ /nav:footer }}
58+
BLADE;
59+
60+
file_put_contents($this->viewPath('nav-test.antlers.html'), $view);
61+
62+
Facades\Entry::make()
63+
->id('nav-test-page')
64+
->slug('nav-test')
65+
->collection('pages')
66+
->data(['template' => 'nav-test'])
67+
->save();
68+
69+
$this->get('/nav-test');
70+
71+
$tags = collect(Tracker::all())->first()['tags'] ?? [];
72+
73+
$this->assertContains('nav:footer', $tags);
74+
}
75+
76+
#[Test]
77+
public function it_tracks_nav_tag_without_handle_using_default()
78+
{
79+
$view = <<<'BLADE'
80+
{{ nav }}
81+
{{ title }}
82+
{{ /nav }}
83+
BLADE;
84+
85+
file_put_contents($this->viewPath('nav-default.antlers.html'), $view);
86+
87+
Facades\Entry::make()
88+
->id('nav-default-page')
89+
->slug('nav-default')
90+
->collection('pages')
91+
->data(['template' => 'nav-default'])
92+
->save();
93+
94+
$this->get('/nav-default');
95+
96+
$tags = collect(Tracker::all())->first()['tags'] ?? [];
97+
98+
$this->assertContains('nav:collection::pages', $tags);
99+
}
100+
101+
#[Test]
102+
public function it_tracks_nav_tag_when_handle_is_nav_instance()
103+
{
104+
// Create a navigation structure
105+
$nav = tap(Facades\Nav::make('sidebar')
106+
->title('Sidebar')
107+
->expectsRoot(true)
108+
->collections(['pages']))
109+
->save();
110+
111+
// The template uses a blueprint field that returns a Nav instance
112+
$view = <<<'BLADE'
113+
{{ nav :handle="my_nav" }}
114+
{{ title }}
115+
{{ /nav }}
116+
BLADE;
117+
118+
file_put_contents($this->viewPath('nav-test.antlers.html'), $view);
119+
120+
// Create entry with nav field that returns Nav instance
121+
Facades\Entry::make()
122+
->id('nav-test-page')
123+
->slug('nav-test')
124+
->collection('pages')
125+
->data([
126+
'template' => 'nav-test',
127+
'my_nav' => $nav,
128+
])
129+
->save();
130+
131+
$this->get('/nav-test');
132+
133+
$tags = collect(Tracker::all())->first()['tags'] ?? [];
134+
135+
$this->assertContains('nav:sidebar', $tags);
136+
}
137+
138+
protected function viewPath($name)
139+
{
140+
return __DIR__.'/../__fixtures__/resources/views/'.$name;
141+
}
142+
}

0 commit comments

Comments
 (0)