diff --git a/src/Http/Middleware/CacheTracker.php b/src/Http/Middleware/CacheTracker.php index dd377e9..b1a0cdf 100644 --- a/src/Http/Middleware/CacheTracker.php +++ b/src/Http/Middleware/CacheTracker.php @@ -13,6 +13,7 @@ use Statamic\Facades\URL; use Statamic\Forms; use Statamic\StaticCaching\Cacher; +use Statamic\Structures\Nav; use Statamic\Structures\Page; use Statamic\Support\Str; use Statamic\Tags; @@ -181,7 +182,13 @@ private function setupTagHooks() }); Tags\Nav::hook('init', function ($value, $next) use ($self) { - $handle = $this->params->get('handle') ? 'nav:'.$this->params->get('handle') : $this->tag; + $handle = $this->params->get('handle', $this->tag != 'nav:index' ? Str::after($this->tag, 'nav:') : 'collection::pages'); + + if ($handle instanceof Nav) { + $handle = $handle->handle(); + } + + $handle = 'nav:'.$handle; $self->addContentTag($handle); return $next($value); diff --git a/tests/Unit/NavTagTest.php b/tests/Unit/NavTagTest.php new file mode 100644 index 0000000..51e09c9 --- /dev/null +++ b/tests/Unit/NavTagTest.php @@ -0,0 +1,142 @@ +title('Footer') + ->expectsRoot(true) + ->collections(['pages']) + ->save(); + + $view = <<<'BLADE' +{{ nav handle="footer" }} + {{ title }} +{{ /nav }} +BLADE; + + file_put_contents($this->viewPath('nav-test.antlers.html'), $view); + + Facades\Entry::make() + ->id('nav-test-page') + ->slug('nav-test') + ->collection('pages') + ->data(['template' => 'nav-test']) + ->save(); + + $this->get('/nav-test'); + + $tags = collect(Tracker::all())->first()['tags'] ?? []; + + $this->assertContains('nav:footer', $tags); + } + + #[Test] + public function it_tracks_nav_tag_with_shorthand_handle() + { + // Create a navigation structure + Facades\Nav::make('footer') + ->title('Footer') + ->expectsRoot(true) + ->collections(['pages']) + ->save(); + + $view = <<<'BLADE' +{{ nav:footer }} + {{ title }} +{{ /nav:footer }} +BLADE; + + file_put_contents($this->viewPath('nav-test.antlers.html'), $view); + + Facades\Entry::make() + ->id('nav-test-page') + ->slug('nav-test') + ->collection('pages') + ->data(['template' => 'nav-test']) + ->save(); + + $this->get('/nav-test'); + + $tags = collect(Tracker::all())->first()['tags'] ?? []; + + $this->assertContains('nav:footer', $tags); + } + + #[Test] + public function it_tracks_nav_tag_without_handle_using_default() + { + $view = <<<'BLADE' +{{ nav }} + {{ title }} +{{ /nav }} +BLADE; + + file_put_contents($this->viewPath('nav-default.antlers.html'), $view); + + Facades\Entry::make() + ->id('nav-default-page') + ->slug('nav-default') + ->collection('pages') + ->data(['template' => 'nav-default']) + ->save(); + + $this->get('/nav-default'); + + $tags = collect(Tracker::all())->first()['tags'] ?? []; + + $this->assertContains('nav:collection::pages', $tags); + } + + #[Test] + public function it_tracks_nav_tag_when_handle_is_nav_instance() + { + // Create a navigation structure + $nav = tap(Facades\Nav::make('sidebar') + ->title('Sidebar') + ->expectsRoot(true) + ->collections(['pages'])) + ->save(); + + // The template uses a blueprint field that returns a Nav instance + $view = <<<'BLADE' +{{ nav :handle="my_nav" }} + {{ title }} +{{ /nav }} +BLADE; + + file_put_contents($this->viewPath('nav-test.antlers.html'), $view); + + // Create entry with nav field that returns Nav instance + Facades\Entry::make() + ->id('nav-test-page') + ->slug('nav-test') + ->collection('pages') + ->data([ + 'template' => 'nav-test', + 'my_nav' => $nav, + ]) + ->save(); + + $this->get('/nav-test'); + + $tags = collect(Tracker::all())->first()['tags'] ?? []; + + $this->assertContains('nav:sidebar', $tags); + } + + protected function viewPath($name) + { + return __DIR__.'/../__fixtures__/resources/views/'.$name; + } +}