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`.
20 lines
441 B
TypeScript
20 lines
441 B
TypeScript
import { FreshContext } from "$fresh/server.ts";
|
|
|
|
const REDIRECTS: Record<string, string> = {
|
|
"/docs/getting-started/fetching-data":
|
|
"/docs/getting-started/custom-handlers",
|
|
};
|
|
|
|
export async function handler(
|
|
_req: Request,
|
|
ctx: FreshContext,
|
|
) {
|
|
// Redirect from old doc URLs to new ones
|
|
const redirect = REDIRECTS[ctx.url.pathname];
|
|
if (redirect) {
|
|
return ctx.redirect(redirect, 307);
|
|
}
|
|
|
|
return await ctx.next();
|
|
}
|