Files
sweet/tests/fixture/routes/redirect.tsx
T
Marvin Hagemeister e3508c3054 feat: add ctx.redirect() helper (#2358)
This PR adds a `ctx.redirect(path, status)` helper that aims to reduce
the verbosity of having to manually create a `Response` obejct for a
mere redirect.

```ts
// Before
new Response(null, {
  status: 307,
  headers: {
    Location: "/foo/bar"
  },
})

// After
ctx.redirect("/foo/bar", 307)
```

If the second parameter is not passed, it will default to `302`.
2024-03-12 17:36:34 +01:00

11 lines
359 B
TypeScript

import { FreshContext } from "$fresh/server.ts";
export const handler = {
GET(_req: Request, ctx: FreshContext) {
const rawStatus = ctx.url.searchParams.get("status");
const status = rawStatus !== null ? Number(rawStatus) : undefined;
const location = ctx.url.searchParams.get("path") ?? "/";
return ctx.redirect(location, status);
},
};