Commit 78648f0
committed
This PR was squashed before being merged into the 7.3 branch.
Discussion
----------
[Cache][HttpKernel] Add a `noStore` argument to the `#` attribute
| Q | A
| ------------- | ---
| Branch? | 7.3
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Issues | Fix #...
| License | MIT
This PR introduces a `noStore` argument to the `#[Cache]` attribute, allowing controllers to easily set the [no-store](https://datatracker.ietf.org/doc/html/rfc7234#section-5.2.2.3) directive.
```php
use Symfony\Component\HttpKernel\Attribute\Cache;
#[Cache(noStore: true)]
final class MyController
{
public function __invoke(): Response
{
// This response will NOT be stored in ANY cache
```
When set to `true`, it also supersedes the `public` / `private` value.
---
I recently encountered issues with the back-forward cache ([bfcache](https://web.dev/articles/bfcache)), a browser feature that stores entire pages in memory to make navigating back and forth faster. It can cause problems when pages rely on JavaScript initialization, dynamic content, or state-changing resources. For example, an edit form might reappear after submission just by hitting “Back” (even after a redirection), with no HTTP request being triggered—leading to unexpected behavior and frustrating the user.
Standard cache headers like `Cache-Control: no-cache` don’t stop this behavior. The _**only**_ reliable way to disable the bfcache across all major browsers is by using the `no-store` directive.
```php
use Symfony\Component\HttpKernel\Attribute\Cache;
final class MyController
{
#[Cache(public: false)]
public function private(): Response
{
// ❌ This page can (and probably will) be cached in the browser bfc
}
#[Cache(noStore: true)]
public function notStored(): Response
{
// ✅ This page will NOT be cached -- not even in the browser bfc
}
}
```
The [HTTP cache documentation](https://symfony.com/doc/current/http_cache.html) states that all options available for the `Response::setCache()` method can also be used with the `#[Cache]` attribute. However, the `no-store` option is currently missing.
> [!NOTE]
> This is a very "raw" implementation.. not sure about it or potential consequences I might not have considered... but I wanted to start the discussion :)
---
**Resources:**
* [Understanding bfcache (web.dev)](https://web.dev/articles/bfcache)
* [Minimizing the impact of no-store on bfcache (web.dev)](https://web.dev/articles/bfcache#minimize-no-store)
* [MDN Glossary: bfcache](https://developer.mozilla.org/en-US/docs/Glossary/bfcache)
* [RFC7234 - Storing Responses in Caches](https://datatracker.ietf.org/doc/html/rfc7234#section-3)
* [RFC7234 - no-store](https://datatracker.ietf.org/doc/html/rfc7234#section-5.2.2.3)
Commits
-------
ecc8c33 [Cache][HttpKernel] Add a `noStore` argument to the `#` attribute
File tree
3 files changed
+68
-0
lines changed- src/Symfony/Component/HttpKernel
- Attribute
- EventListener
- Tests/EventListener
3 files changed
+68
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
105 | 117 | | |
106 | 118 | | |
107 | 119 | | |
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
166 | 175 | | |
167 | 176 | | |
168 | 177 | | |
| |||
Lines changed: 47 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
94 | 138 | | |
95 | 139 | | |
96 | 140 | | |
| |||
132 | 176 | | |
133 | 177 | | |
134 | 178 | | |
| 179 | + | |
135 | 180 | | |
136 | 181 | | |
137 | 182 | | |
| |||
140 | 185 | | |
141 | 186 | | |
142 | 187 | | |
| 188 | + | |
143 | 189 | | |
144 | 190 | | |
145 | 191 | | |
| |||
149 | 195 | | |
150 | 196 | | |
151 | 197 | | |
| 198 | + | |
152 | 199 | | |
153 | 200 | | |
154 | 201 | | |
| |||
0 commit comments