Symfony News

New in Symfony 3.4: HTTP Immutable Responses

Jaroslav Kuba

Contributed by
Jaroslav Kuba
in #22932.

Some of the traditional HTTP Cache mechanisms are based on the browser sending HTTP requests to ask whether some specific resource changed since it was retrieved the first time. If the resource didn't change, the server returns an empty HTTP response with 304 status (Not Modified) and the browser can reuse it.

However, in modern web applications is common to have resources that never change. For example, when using Webpack Encore to manage web assets, you can enable versioning to include in each asset URL a hash that changes whenever the contents of that asset changes. In other words, it's safe to store that asset forever and always reuse it without asking to the server.

This technique is already used by popular services such as Facebook, which helped them reduce static resource requests to their servers by 60% and improved page load times dramatically. In September 2017, IETF made this technique official thanks to the RFC 8246 proposal and Symfony 3.4 will include support for it.

In practice, immutable HTTP responses are created with the Cache-Control: immutable header, which can be combined with other cache options (e.g. make a response immutable for 1 year: Cache-Control: max-age=31536000, immutable). In Symfony 3.4, this setting is controlled with the new setImmutable(boolean $isImmutable) and isImmutable() methods of the Response class. When setting multiple cache options, use the immutable option with the setCache() method.


Be trained by Symfony experts - 2017-09-18 Paris - 2017-09-25 Cologne - 2017-09-25 Paris


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook