diff --git a/src/server/context.ts b/src/server/context.ts index 7bd3fd4a..00b22063 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -28,6 +28,7 @@ import { MiddlewareRoute, Plugin, RenderFunction, + RenderOptions, Route, RouteModule, UnknownPage, @@ -490,7 +491,7 @@ export class ServerContext { params: Record, error?: unknown, ) => { - return async (data?: Data) => { + return async (data?: Data, options?: RenderOptions) => { if (route.component === undefined) { throw new Error("This page does not have a component to render."); } @@ -538,7 +539,16 @@ export class ServerContext { headers["content-security-policy"] = directive; } } - return new Response(body, { status, headers }); + return new Response(body, { + status: options?.status ?? status, + statusText: options?.statusText, + headers: options?.headers + ? { + ...headers, + ...options.headers, + } + : headers, + }); }; }; }; diff --git a/src/server/types.ts b/src/server/types.ts index e52ab3a4..785c7341 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -72,10 +72,16 @@ export interface RouteConfig { csp?: boolean; } +// deno-lint-ignore no-empty-interface +export interface RenderOptions extends ResponseInit {} + export interface HandlerContext> extends ConnInfo { params: Record; - render: (data?: Data) => Response | Promise; + render: ( + data?: Data, + options?: RenderOptions, + ) => Response | Promise; renderNotFound: () => Response | Promise; state: State; } diff --git a/tests/fixture/fresh.gen.ts b/tests/fixture/fresh.gen.ts index 8b737694..8f7f9efc 100644 --- a/tests/fixture/fresh.gen.ts +++ b/tests/fixture/fresh.gen.ts @@ -34,7 +34,8 @@ import * as $27 from "./routes/not_found.ts"; import * as $28 from "./routes/params.tsx"; import * as $29 from "./routes/props/[id].tsx"; import * as $30 from "./routes/static.tsx"; -import * as $31 from "./routes/wildcard.tsx"; +import * as $31 from "./routes/status_overwrite.tsx"; +import * as $32 from "./routes/wildcard.tsx"; import * as $$0 from "./islands/Counter.tsx"; import * as $$1 from "./islands/ReturningNull.tsx"; import * as $$2 from "./islands/RootFragment.tsx"; @@ -76,7 +77,8 @@ const manifest = { "./routes/params.tsx": $28, "./routes/props/[id].tsx": $29, "./routes/static.tsx": $30, - "./routes/wildcard.tsx": $31, + "./routes/status_overwrite.tsx": $31, + "./routes/wildcard.tsx": $32, }, islands: { "./islands/Counter.tsx": $$0, diff --git a/tests/fixture/routes/status_overwrite.tsx b/tests/fixture/routes/status_overwrite.tsx new file mode 100644 index 00000000..83dfe8ec --- /dev/null +++ b/tests/fixture/routes/status_overwrite.tsx @@ -0,0 +1,14 @@ +import { HandlerContext } from "$fresh/server.ts"; + +export default function Page() { + return
This is HTML
; +} + +export const handler = { + GET(req: Request, { render }: HandlerContext) { + return render(undefined, { + status: 401, + headers: { "x-some-header": "foo" }, + }); + }, +}; diff --git a/tests/main_test.ts b/tests/main_test.ts index 2d65e4a5..36de5650 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -148,6 +148,18 @@ Deno.test("/intercept_args - GET html", async () => { assertStringIncludes(body, "
intercepted
"); }); +Deno.test("/status_overwrite", async () => { + const req = new Request("https://fresh.deno.dev/status_overwrite", { + headers: { "accept": "text/html" }, + }); + const resp = await router(req); + assert(resp); + assertEquals(resp.status, Status.Unauthorized); + assertEquals(resp.headers.get("x-some-header"), "foo"); + const body = await resp.text(); + assertStringIncludes(body, "
This is HTML
"); +}); + Deno.test("/api/get_only - NOTAMETHOD", async () => { const resp = await router( new Request("https://fresh.deno.dev/api/get_only", {