Skip to content

fix(cloudflare): return redirects from cache-facing fetches - #3242

Open
JamesbbBriz wants to merge 1 commit into
cloudflare:mainfrom
JamesbbBriz:fix/cached-stage-redirect-loop
Open

fix(cloudflare): return redirects from cache-facing fetches#3242
JamesbbBriz wants to merge 1 commit into
cloudflare:mainfrom
JamesbbBriz:fix/cached-stage-redirect-loop

Conversation

@JamesbbBriz

@JamesbbBriz JamesbbBriz commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Problem

Since #3168 (35af791), any route whose shared-cache render responds with a redirect returns 500 TypeError: Too many redirects to the client.

Reproducer: a Payload CMS app whose /admin route redirects unauthenticated visitors to /admin/login (a stock Payload setup with zero users). On current main, GET /admin 500s. On 80c5f59 (the commit before 35af791) it returns 307 -> /admin/login and the login page renders.

Root cause

A fetch through the cache-enabled response entrypoint follows redirects by default. When the render responds 307:

  1. The runtime follows the redirect and re-enters the entrypoint for the redirect target (/admin/login).
  2. The response-stage invocation still describes the original URL (/admin) — observed via instrumentation: every hop rendered with the original requestUrl while the entrypoint received the login URL.
  3. The redirect source re-renders and redirects again, until the runtime's redirect budget is exhausted (TypeError: Too many redirects, chain of 20 identical hops in the worker logs).

Bisected with repeated single-change deploys of a Payload app: first bad commit is 35af791 (#3168), which routes public traffic through the cache-fronted entrypoint.

Fix

Set redirect: "manual" on the cache-facing request in createCacheFacingRequest, so redirect responses are returned to the caller — matching the pre-#3168 gateway behavior.

Verified on a deployed Payload CMS site (D1/KV/R2, workers cache cdnAdapter):

  • GET /admin307 to /admin/login (was 500)
  • GET /admin/login → 200
  • ISR pages still serve CF-Cache-Status: HIT with the staged pipeline intact

Test

Adds a regression test in cloudflare-cdn-worker.test.ts asserting the cache-facing request carries `redirect: "manual"" and the redirect status passes through the gateway.

Fixes #3243

A fetch through the cache-enabled response entrypoint follows redirects
by default. When a shared-cache render responds with a redirect, the
runtime follows it and re-enters the entrypoint for the redirect target
while the response-stage invocation still describes the original URL —
so the redirect source re-renders and redirects again, until the
runtime's redirect budget is exhausted (TypeError: Too many redirects).

Observed on a Payload CMS app whose /admin route redirects
unauthenticated visitors to /admin/login: every hop logged another
/admin/login render performed with the original /admin invocation, and
the page 500ed. Bisected to 35af791 (cloudflare#3168), which routes public
traffic through the cache-fronted entrypoint.

Set redirect: manual on the cache-facing request so redirect responses
are returned to the caller, matching the pre-cloudflare#3168 gateway behavior.
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@pkg-pr-new

pkg-pr-new Bot commented Sep 11, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@vinext/cloudflare@3242
npm i https://pkg.pr.new/create-vinext-app@3242
npm i https://pkg.pr.new/@vinext/types@3242
npm i https://pkg.pr.new/vinext@3242

commit: 8b18835

JamesbbBriz added a commit to JamesbbBriz/vinext that referenced this pull request Sep 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Performance benchmarks

Compared 8b18835 against base 13e7d9c using alternating same-runner rounds. Next.js was unchanged and skipped.

0 improved · 0 regressed · 6 within ±1.5%

Scenario Framework Baseline Current Change
Client bundle size (gzip) vinext 142.4 KB 142.4 KB ⚫ +0.0%
Client entry size (gzip) vinext 129.7 KB 129.7 KB ⚫ +0.0%
Dev server cold start vinext 3.38 s 3.36 s ⚫ -0.6%
Production build time vinext 3.52 s 3.54 s ⚫ +0.6%
RSC entry closure size (gzip) vinext 125.5 KB 125.4 KB ⚫ -0.0%
Server bundle size (gzip) vinext 216.1 KB 216.1 KB ⚫ -0.0%

View detailed results and traces

🟢 improvement · 🔴 regression · ⚫ change below 1.5% · paired base/head

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.

Shared-cache renders that redirect return 500 "Too many redirects" since #3168

1 participant