[HttpFoundation] 308 responses do not get the 301 Cache-Control treatment
Description
RedirectResponse::__construct() drops the default no-cache, private Cache-Control header for status 301 only:
301 and 308 are both permanent redirects. The only difference is that 308 keeps the request method. Yet:
(new RedirectResponse('/target', 301))->headers->get('Cache-Control'); // null
(new RedirectResponse('/target', 302))->headers->get('Cache-Control'); // "no-cache, private"
(new RedirectResponse('/target', 307))->headers->get('Cache-Control'); // "no-cache, private"
(new RedirectResponse('/target', 308))->headers->get('Cache-Control'); // "no-cache, private"This is reachable straight from the framework. RedirectController emits 308 whenever permanent: true meets keepRequestMethod: true:
Same shape in urlRedirectAction() at L112-L116, and in FrameworkBundle's RedirectController on 6.4 (L84-L86 and L117-L119). So a route redirect declared permanent is cacheable when it does not keep the method and uncacheable when it does, which is not a distinction anyone configured.
Two ways to resolve the asymmetry, and I do not have a strong preference:
- Extend the 301 carve-out to 308, so both permanent codes behave the same.
- Leave it as is, on the grounds that the 301 carve-out is old behaviour we do not want to grow.
#59346 asked for the opposite change, removing the carve-out from 301 so browsers stop caching permanent redirects indefinitely, and was closed as by design: a permanent redirect is cacheable, that is what permanent means. That reasoning applies to 308 just as well, which argues for option 1. But it changes what anyone serving a 308 gets today, so it wants a deliberate call rather than a drive-by patch.
Which one do we want?
Source: symfony/symfony