FULL AI ANALYSIS

React SPA loses session cookie intermittently when routed through Cloudflare Worker edge scripts

Generated Oct 17, 2026, 9:26:12 AM

Analysis summary

Cloudflare Workers run at the edge and can modify request/response headers. Browsers send cookies based on the request URL (domain + scheme + path + SameSite rules); if the edge script issues redirects, rewrites hostnames, or constructs a new Request with different URL/host, the Cookie header forwarded to origin may differ from the browser's original header. Additionally, Set-Cookie semantics combined with proxying and redirects can cause the browser not to persist cookies when domain/secure/sameSite constraints are not met. The correlation between Worker host rewrite and missing Cookie suggests the Worker is reconstructing requests or issuing cross-host redirects that break cookie scoping.

Possible causes

Worker rewrites the request URL or Host header and creates a new Request object; this new Request lacks the original Cookie header or uses a different host, causing browser cookies not to be sent/forwarded

75%

Edge-issued redirects (302) from the Worker change the effective domain/path of the following request so the browser does not attach the cookie on the redirected request, particularly with SameSite=Lax/potential cross-site redirect semantics

50%

Cloudflare caching or custom cache logic at the Worker level returns an older response without Set-Cookie, making clients briefly appear unauthenticated until they refresh

25%

Browser-level cookie policy (SameSite, Secure) interacting with subdomain differences (www vs naked domain) — some clients treat requests as cross-site and suppress cookies

20%

Suggested fixes

1

Diagnostic step 1

Modify Worker to avoid creating a fresh Request with a new URL/Host. Instead, use event.fetch(event.request) with minimal alterations or explicitly copy event.request.headers and ensure Cookie is preserved when constructing a new Request (e.g., new Request(url, { headers: event.request.headers })).

2

Diagnostic step 2

Avoid returning edge-side redirects that change site origin during authentication flows. If redirect is required, ensure Set-Cookie attributes (Domain, SameSite) match the redirect target and use SameSite=None with Secure for third-party contexts only if acceptable.

3

Diagnostic step 3

Temporarily disable the Worker and confirm production traffic behaves correctly (you already tested this). Use that as a control while iteratively enabling minimal parts of the Worker to identify the exact instruction that strips or changes Cookie.

4

Diagnostic step 4

Add diagnostic logging in the Worker (respecting PII policies — do not log cookie values; instead log presence/absence flags and host header) to confirm when Cookie header is dropped. If this proves a Cloudflare behavior bug, collect timestamps and Worker code snippet and escalate to Cloudflare support with request IDs.

5

Diagnostic step 5

If you cannot avoid rewriting hostnames, consider passing the original host in a custom header and handling cookies at origin accordingly, or configure Cloudflare page rules to route auth endpoints unmodified. Escalate to Cloudflare only if you can demonstrate Worker code that preserves headers yet the Cookie header is still stripped at the edge.