Skip to content

Commit a04aba9

Browse files
committed
minor #13359 Improved the HTTPCache examples mentioning s-maxage (javiereguiluz)
This PR was merged into the 4.4 branch. Discussion ---------- Improved the HTTPCache examples mentioning s-maxage Fixes #12934. Commits ------- 71f24dd Improved the HTTPCache examples mentioning s-maxage
2 parents 1eeda33 + 71f24dd commit a04aba9

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

http_cache.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,9 @@ The *easiest* way to cache a response is by caching it for a specific amount of
249249
// somehow create a Response object, like by rendering a template
250250
$response = $this->render('blog/index.html.twig', []);
251251

252-
// cache for 3600 seconds
253-
$response->setSharedMaxAge(3600);
252+
// cache publicly for 3600 seconds
253+
$response->setPublic();
254+
$response->setMaxAge(3600);
254255

255256
// (optional) set a custom Cache-Control directive
256257
$response->headers->addCacheControlDirective('must-revalidate', true);
@@ -262,7 +263,7 @@ Thanks to this new code, your HTTP response will have the following header:
262263

263264
.. code-block:: text
264265
265-
Cache-Control: public, s-maxage=3600, must-revalidate
266+
Cache-Control: public, maxage=3600, must-revalidate
266267
267268
This tells your HTTP reverse proxy to cache this response for 3600 seconds. If *anyone*
268269
requests this URL again before 3600 seconds, your application *won't* be hit at all.

http_cache/esi.rst

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,15 @@ independently of the rest of the page::
105105
public function about()
106106
{
107107
$response = $this->render('static/about.html.twig');
108-
// sets the shared max age - which also marks the response as public
109-
$response->setSharedMaxAge(600);
108+
$response->setPublic();
109+
$response->setMaxAge(600);
110110

111111
return $response;
112112
}
113113
}
114114

115-
In this example, the full-page cache has a lifetime of ten minutes.
115+
In this example, the response is marked as public to make the full page
116+
cacheable for all requests with a lifetime of ten minutes.
116117
Next, include the news ticker in the template by embedding an action.
117118
This is done via the ``render()`` helper (for more details, see how to
118119
:ref:`embed controllers in templates <templates-embed-controllers>`).
@@ -170,14 +171,19 @@ of the master page::
170171
public function latest($maxPerPage)
171172
{
172173
// ...
173-
$response->setSharedMaxAge(60);
174+
$response->setPublic();
175+
$response->setMaxAge(60);
174176

175177
return $response;
176178
}
177179
}
178180

179-
With ESI, the full page cache will be valid for 600 seconds, but the news
180-
component cache will only last for 60 seconds.
181+
In this example, the embedded action is cached publicly too because the contents
182+
are the same for all requests. However, in other cases you may need to make this
183+
response non-public and even non-cacheable, depending on your needs.
184+
185+
Putting all the above code together, with ESI the full page cache will be valid
186+
for 600 seconds, but the news component cache will only last for 60 seconds.
181187

182188
.. _http_cache-fragments:
183189

@@ -233,14 +239,6 @@ possible.
233239
signed when using the fragment renderer and the ``render_esi`` Twig
234240
function.
235241

236-
.. note::
237-
238-
Once you start using ESI, remember to always use the ``s-maxage``
239-
directive instead of ``max-age``. As the browser only ever receives the
240-
aggregated resource, it is not aware of the sub-components, and so it will
241-
obey the ``max-age`` directive and cache the entire page. And you don't
242-
want that.
243-
244242
The ``render_esi`` helper supports two other useful options:
245243

246244
``alt``

http_cache/expiration.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,22 @@ is used to specify many different cache directives::
2626

2727
// sets the number of seconds after which the response
2828
// should no longer be considered fresh by shared caches
29-
$response->setSharedMaxAge(600);
29+
$response->setPublic();
30+
$response->setMaxAge(600);
3031

3132
The ``Cache-Control`` header would take on the following format (it may have
3233
additional directives):
3334

3435
.. code-block:: text
3536
36-
Cache-Control: public, s-maxage=600
37+
Cache-Control: public, maxage=600
38+
39+
.. note::
40+
41+
Using the ``setSharedMaxAge()`` may seem equivalent to using both ``setPublic()``
42+
and ``setMaxAge()`` methods. However, their behavior is different in a
43+
``stale-if-error`` scenario and that's why it's recommended to use both
44+
``public`` and ``max-age`` directives.
3745

3846
.. index::
3947
single: Cache; Expires header

0 commit comments

Comments
 (0)