Repository navigation
feat: send the same cache headers as TYPO3 from .htaccess and fallback delivery - #477
Merged
lochmueller merged 3 commits intoOct 15, 2026
Conversation
…HeaderService Move reading config.sendCacheHeaders from the request's frontend TypoScript into a CacheHeaderService, which also returns config.sendCacheHeadersForSharedCaches. No change in behaviour; the following commits use the service to send the same cache headers as TYPO3 from both the .htaccess and the fallback middleware delivery.
With config.sendCacheHeadersForSharedCaches TYPO3 answers max-age=0, s-maxage=<lifetime>, so browsers revalidate while shared caches such as a CDN keep the page. Entries delivered from Apache via the .htaccess template only ever got mod_expires' max-age, and nothing at all when config.sendCacheHeaders was off. HtaccessGenerator now evaluates config.sendCacheHeadersForSharedCaches the way TYPO3 does - always for "force", for "auto" when the request comes through a trusted reverse proxy - and the template rewrites mod_expires' max-age into max-age=0, s-maxage=<remaining lifetime> for those entries. Like in TYPO3 this does not require config.sendCacheHeaders. Apache cannot evaluate the reverse proxy check itself, so for "auto" the request that generates the entry decides.
The fallback middleware sent no cache headers of its own, only those validFallbackHeaders let into the entry's config.json. Storing TYPO3's Cache-Control there freezes it at generation time: every delivery then restarts the full lifetime, so a shared cache can keep a copy long after the entry expired. Not storing it leaves the response without any cache header, which shared caches answer with their own default TTL. The config generator now stores config.sendCacheHeaders and config.sendCacheHeadersForSharedCaches with the entry, and the fallback computes the headers on every delivery, as TYPO3 does when it serves from its page cache: Expires at the end of the entry's lifetime and Cache-Control max-age=<remaining lifetime>, or max-age=0, s-maxage=<remaining lifetime> for shared caches - always for "force", for "auto" when this request comes through a trusted reverse proxy. They take precedence over a stored Cache-Control or Expires. Entries written before this change send no cache headers until they are regenerated, as before.
Owner
|
Hey @felixbuenemann |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Short description
Statically cached pages now answer with the same cache headers TYPO3 sends when it serves them from its page cache, on both delivery paths:
config.sendCacheHeadersForSharedCaches, the generated.htaccessturns mod_expires'max-age=<remaining lifetime>intomax-age=0, s-maxage=<remaining lifetime>, as TYPO3 does for shared caches. Before, these entries only ever gotmax-age, and no cache header at all whenconfig.sendCacheHeaderswas off.ExpiresandCache-Controlitself, computed from the entry'sinvalidAtTimestampon every delivery:max-age=<remaining lifetime>withconfig.sendCacheHeaders,max-age=0, s-maxage=<remaining lifetime>for shared caches. Before, it sent no cache headers unless they were stored throughvalidFallbackHeaders- and stored ones are frozen when the entry is written, so every delivery restarted the full lifetime.config.sendCacheHeadersForSharedCachesis evaluated as TYPO3 evaluates it:forcealways,autowhen the request comes through a trusted reverse proxy (NormalizedParams::isBehindReverseProxy()). Like in TYPO3, it takes precedence overconfig.sendCacheHeadersand does not require it.Related Issue
None. Related to #474, which is about the lifetime an entry gets; with this change the headers count down whatever lifetime that is.
More Details
Behind a CDN this decides how long the CDN keeps a page. A stored
s-maxagerestarts on every fetch, so the CDN can keep a copy up to a full lifetime after the entry itself expired;max-agealone makes browsers cache the page as well. TYPO3's own answer,max-age=0, s-maxage=<remaining>, avoids both, and with this PR the static deliveries give the same answer.The PR is split into three commits:
CacheHeaderServicereadsconfig.sendCacheHeadersandconfig.sendCacheHeadersForSharedCachesfrom the request's frontend TypoScript. No change in behaviour.HtaccessGeneratorpassessendCacheControlHeaderForSharedCachesto the template, which addsHeader edit Cache-Control "^max-age=(\d+)$" "max-age=0, s-maxage=$1"for those entries.ConfigGeneratorstores both TypoScript values in the entry'sconfig.json, andFallbackMiddlewarecomputes the headers from them on every delivery.Things to be aware of:
autothe request that generates the entry decides for the.htaccessdelivery. The fallback evaluates it per request.Cache-ControlandExpirestake precedence over ones stored throughvalidFallbackHeaders..htaccessandconfig.jsonuntil they are regenerated, so a cache flush after updating is needed to get the new headers everywhere.Testing:
25 new unit tests for
CacheHeaderService,HtaccessGenerator,ConfigGeneratorandFallbackMiddleware(the fallback throughprocess(), for every combination of the two settings and with a storedCache-Controlthat must not win). The full suite passes, php-cs-fixer and PHPStan report nothing.On TYPO3 14.3.9 with Apache,
config.sendCacheHeadersForSharedCaches = forceandconfig.cache_period = 2678400(31 days) TYPO3 rendered withmax-age=0, s-maxage=2678400, the generated.htaccesscontained theHeader edit, Apache then delivered the file withmax-age=0, s-maxage=2678398counting down (2678390, 2678385), and the fallback answered with the sameExpiresas TYPO3's entry whiles-maxagecounted down as well.Work in Progress? No.
Open Issues? None known.