Skip to content

feat: send the same cache headers as TYPO3 from .htaccess and fallback delivery - #477

Merged
lochmueller merged 3 commits into
lochmueller:mainfrom
felixbuenemann:feat/cache-headers-like-typo3
Oct 15, 2026
Merged

lochmueller merged 3 commits into
lochmueller:mainfrom
felixbuenemann:feat/cache-headers-like-typo3

Conversation

@felixbuenemann

Copy link
Copy Markdown
Contributor

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:

  • .htaccess (Apache): with config.sendCacheHeadersForSharedCaches, the generated .htaccess turns mod_expires' max-age=<remaining lifetime> into max-age=0, s-maxage=<remaining lifetime>, as TYPO3 does for shared caches. Before, these entries only ever got max-age, and no cache header at all when config.sendCacheHeaders was off.
  • Fallback middleware: the fallback now sends Expires and Cache-Control itself, computed from the entry's invalidAtTimestamp on every delivery: max-age=<remaining lifetime> with config.sendCacheHeaders, max-age=0, s-maxage=<remaining lifetime> for shared caches. Before, it sent no cache headers unless they were stored through validFallbackHeaders - and stored ones are frozen when the entry is written, so every delivery restarted the full lifetime.

config.sendCacheHeadersForSharedCaches is evaluated as TYPO3 evaluates it: force always, auto when the request comes through a trusted reverse proxy (NormalizedParams::isBehindReverseProxy()). Like in TYPO3, it takes precedence over config.sendCacheHeaders and 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-maxage restarts on every fetch, so the CDN can keep a copy up to a full lifetime after the entry itself expired; max-age alone 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:

  1. refactor(htaccess): a CacheHeaderService reads config.sendCacheHeaders and config.sendCacheHeadersForSharedCaches from the request's frontend TypoScript. No change in behaviour.
  2. feat(htaccess): HtaccessGenerator passes sendCacheControlHeaderForSharedCaches to the template, which adds Header edit Cache-Control "^max-age=(\d+)$" "max-age=0, s-maxage=$1" for those entries.
  3. feat(fallback): ConfigGenerator stores both TypoScript values in the entry's config.json, and FallbackMiddleware computes the headers from them on every delivery.

Things to be aware of:

  • Apache cannot evaluate the trusted reverse proxy check per request, so for auto the request that generates the entry decides for the .htaccess delivery. The fallback evaluates it per request.
  • The fallback's computed Cache-Control and Expires take precedence over ones stored through validFallbackHeaders.
  • Entries written before the update keep their old .htaccess and config.json until 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, ConfigGenerator and FallbackMiddleware (the fallback through process(), for every combination of the two settings and with a stored Cache-Control that must not win). The full suite passes, php-cs-fixer and PHPStan report nothing.

  • On TYPO3 14.3.9 with Apache, config.sendCacheHeadersForSharedCaches = force and config.cache_period = 2678400 (31 days) TYPO3 rendered with max-age=0, s-maxage=2678400, the generated .htaccess contained the Header edit, Apache then delivered the file with max-age=0, s-maxage=2678398 counting down (2678390, 2678385), and the fallback answered with the same Expires as TYPO3's entry while s-maxage counted down as well.

  • Work in Progress? No.

  • Open Issues? None known.

…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.
@lochmueller

Copy link
Copy Markdown
Owner

Hey @felixbuenemann
Thanks for the PR and detailed explanation.
Merged!
Regards,
Tim

@lochmueller
lochmueller merged commit 3827587 into lochmueller:main Oct 15, 2026
3 checks passed
@felixbuenemann
felixbuenemann deleted the feat/cache-headers-like-typo3 branch October 16, 2026 00:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants