From 4e0331b5c90aff35f04d0bfe6b8da29098ae64b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sun, 29 Mar 2026 11:23:54 +0000 Subject: [PATCH] fix: preserve partial search param through redirects (#3715) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary - Fixes #2610 — "Partial + redirect breaks islands" - When a partial request (`?fresh-partial=true`) hits a server-side redirect via `ctx.redirect()`, the `fresh-partial` search param is now preserved in the redirect `Location` header - Previously the param was lost, so the redirect target rendered as a full page instead of a partial response, breaking island hydration The fix is in `ctx.redirect()` in `packages/fresh/src/context.ts` — when `this.isPartial` is true, the partial param is appended to the redirect location (handling existing query params and hash fragments correctly). Co-authored-by: Claude Opus 4.6 (1M context) --- packages/fresh/src/context.ts | 10 ++++++++ packages/fresh/src/context_test.tsx | 38 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/packages/fresh/src/context.ts b/packages/fresh/src/context.ts index abc94082..f9bba4eb 100644 --- a/packages/fresh/src/context.ts +++ b/packages/fresh/src/context.ts @@ -182,6 +182,16 @@ export class Context { location = `${pathname.replaceAll(/\/+/g, "/")}${search}`; } + // Preserve the partial search param through redirects so that the + // redirected page is still rendered in partial mode. + if (this.isPartial) { + const hashIdx = location.indexOf("#"); + const base = hashIdx > -1 ? location.slice(0, hashIdx) : location; + const hash = hashIdx > -1 ? location.slice(hashIdx) : ""; + const separator = base.includes("?") ? "&" : "?"; + location = `${base}${separator}${PARTIAL_SEARCH_PARAM}=true${hash}`; + } + return new Response(null, { status, headers: { diff --git a/packages/fresh/src/context_test.tsx b/packages/fresh/src/context_test.tsx index b39dac1e..9f695994 100644 --- a/packages/fresh/src/context_test.tsx +++ b/packages/fresh/src/context_test.tsx @@ -70,6 +70,44 @@ Deno.test("ctx.render - throw with invalid first arg", async () => { expect(res.status).toEqual(500); }); +Deno.test("ctx.redirect - preserves partial search param through redirects", async () => { + const app = new App() + .get("/old", (ctx) => ctx.redirect("/new")); + const server = new FakeServer(app.handler()); + + // Normal redirect should not have partial param + let res = await server.get("/old"); + expect(res.status).toEqual(302); + expect(res.headers.get("Location")).toEqual("/new"); + + // Partial redirect should preserve the param + res = await server.get("/old?fresh-partial=true"); + expect(res.status).toEqual(302); + expect(res.headers.get("Location")).toEqual("/new?fresh-partial=true"); + + // Partial redirect with existing query params + const app2 = new App() + .get("/old", (ctx) => ctx.redirect("/new?foo=bar")); + const server2 = new FakeServer(app2.handler()); + + res = await server2.get("/old?fresh-partial=true"); + expect(res.status).toEqual(302); + expect(res.headers.get("Location")).toEqual( + "/new?foo=bar&fresh-partial=true", + ); + + // Partial redirect with hash fragment — param must come before the hash + const app3 = new App() + .get("/old", (ctx) => ctx.redirect("/new#section")); + const server3 = new FakeServer(app3.handler()); + + res = await server3.get("/old?fresh-partial=true"); + expect(res.status).toEqual(302); + expect(res.headers.get("Location")).toEqual( + "/new?fresh-partial=true#section", + ); +}); + Deno.test("ctx.isPartial - should indicate whether request is partial or not", async () => { const isPartials: boolean[] = []; const app = new App()