Skip to content

Commit 3cc0b1d

Browse files
authored
Update TwigExtension.php
1 parent 4df08f2 commit 3cc0b1d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/Turbo/src/Twig/TwigExtension.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public function getFunctions(): array
3838
{
3939
return [
4040
new TwigFunction('turbo_stream_listen', $this->turboStreamListen(...), ['needs_environment' => true, 'is_safe' => ['html']]),
41+
new TwigFunction('turbo_exempts_page_from_cache', $this->turboExemptsPageFromCache(...), ['is_safe' => ['html']]),
42+
new TwigFunction('turbo_exempts_page_from_preview', $this->turboExemptsPageFromPreview(...), ['is_safe' => ['html']]),
43+
new TwigFunction('turbo_page_requires_reload', $this->turboPageRequiresReload(...), ['is_safe' => ['html']]),
4144
new TwigFunction('turbo_refreshes_with', $this->turboRefreshesWith(...), ['is_safe' => ['html']]),
4245
new TwigFunction('turbo_refresh_method', $this->turboRefreshMethod(...), ['is_safe' => ['html']]),
4346
new TwigFunction('turbo_refresh_scroll', $this->turboRefreshScroll(...), ['is_safe' => ['html']]),
@@ -62,6 +65,45 @@ public function turboStreamListen(Environment $env, $topic, ?string $transport =
6265
return $this->turboStreamListenRenderers->get($transport)->renderTurboStreamListen($env, $topic);
6366
}
6467

68+
/**
69+
* Generates a <meta> tag to disable caching of a page.
70+
*
71+
* Inspired by Turbo Rails
72+
* ({@see https://github.com/hotwired/turbo-rails/blob/main/app/helpers/turbo/drive_helper.rb}).
73+
*
74+
* @return string
75+
*/
76+
public function turboExemptsPageFromCache(): string
77+
{
78+
return '<meta name="turbo-cache-control" content="no-cache">';
79+
}
80+
81+
/**
82+
* Generates a <meta> tag to specify cached version of the page should not be shown as a preview on regular navigation visits.
83+
*
84+
* Inspired by Turbo Rails
85+
* ({@see https://github.com/hotwired/turbo-rails/blob/main/app/helpers/turbo/drive_helper.rb}).
86+
*
87+
* @return string
88+
*/
89+
public function turboExemptsPageFromPreview(): string
90+
{
91+
return '<meta name="turbo-cache-control" content="no-preview">';
92+
}
93+
94+
/**
95+
* Generates a <meta> tag to force a full page reload.
96+
*
97+
* Inspired by Turbo Rails
98+
* ({@see https://github.com/hotwired/turbo-rails/blob/main/app/helpers/turbo/drive_helper.rb}).
99+
*
100+
* @return string
101+
*/
102+
public function turboPageRequiresReload(): string
103+
{
104+
return '<meta name="turbo-visit-control" content="reload">';
105+
}
106+
65107
/**
66108
* Generates <meta> tags to configure both the refresh method and scroll behavior for page refreshes.
67109
*

0 commit comments

Comments
 (0)