Skip to content

fix(api): restrict Souin HTTP cache to API responses - #718

Merged
vincentchalamon merged 1 commit into
mainfrom
fix/souin-exclude-next-assets
Dec 1, 2026
Merged

vincentchalamon merged 1 commit into
mainfrom
fix/souin-exclude-next-assets

Conversation

@vincentchalamon

Copy link
Copy Markdown
Contributor

Problem

Production is broken although CI and CD pass: /admin stays blank and /books never shows the book covers.

The Turbopack runtime chunk /_next/static/chunks/turbopack-077hmtqrhcoji.js is served truncated by Cloudflare in every encoding (gzip fails the CRC check, br is 1 byte, zstd and identity are empty). The browser reports ERR_CONTENT_DECODING_FAILED, so no client JS runs: React Admin never mounts, and covers, which load client-side through useOpenLibraryBook, stay on Loading.

Root cause

GKE logs (namespace prod) show the following:

  • On 2026-11-21 at 15:26:33 UTC, Cloudflare fetched the chunk from the origin. Souin answered hit; detail=REDIS with 2306 bytes instead of 4354. Next.js sends Cache-Control: public, max-age=31536000, immutable, so Cloudflare has kept this truncated copy since then.

  • This is not a one-off. Since 2026-11-15, Souin cache hits on proxied Next.js responses regularly come back with an empty body and a 200 status, mostly from Otter:

    • for /_next/static/*: every chunk, CSS and font file;
    • for HTML pages: / (160 occurrences), /admin (19), /login.

    Over the last 24h, 30 of the 160 hits outside /_next were empty. Every empty hit was a PWA response; API responses were fine.

Souin does not check the stored body against Content-Length, so broken copies of these proxied responses are stored and served.

Fix

Move {$CADDY_SERVER_CACHE} after the Keycloak and Next.js reverse_proxy directives. Souin now only handles API responses, which is what it is used for: Hydra resources invalidated through surrogate keys. PWA assets keep being cached by Cloudflare straight from Next.js. ADR 0005 is updated accordingly.

Tested locally with the FrankenPHP + Souin binary, Redis and the prod cache options from compose.prod.yaml:

Request Before After
/_next/static/... cached by Souin not handled by Souin
/ (text/html) cached by Souin not handled by Souin
/oidc/* cached by Souin not handled by Souin
/books.jsonld, / (application/ld+json) cached cached (hit; detail=OTTER)

Required after deploy

Cloudflare will keep serving the broken chunk until it is purged. Purge https://demo.api-platform.com/_next/static/chunks/turbopack-077hmtqrhcoji.js, or purge everything. The Redis and Otter entries become unreachable once this is deployed.

Souin served proxied Next.js responses from Otter and Redis with empty or truncated bodies. A truncated Turbopack runtime chunk, marked immutable for a year, was then cached by Cloudflare and broke the whole PWA (admin, book covers).

Move the cache handler after the Keycloak and Next.js reverse proxies so it only handles API responses.
@vincentchalamon
vincentchalamon merged commit 2069d29 into main Dec 1, 2026
4 checks passed
@vincentchalamon
vincentchalamon deleted the fix/souin-exclude-next-assets branch December 1, 2026 20:52
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.

1 participant