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`.
This commit is contained in:
Marvin Hagemeister
2024-03-12 17:36:34 +01:00
committed by GitHub
parent 671751d572
commit e3508c3054
8 changed files with 82 additions and 20 deletions
+1 -5
View File
@@ -12,11 +12,7 @@ export async function handler(
// Redirect from old doc URLs to new ones
const redirect = REDIRECTS[ctx.url.pathname];
if (redirect) {
const url = new URL(redirect, ctx.url.origin);
return new Response("", {
status: 307,
headers: { location: url.href },
});
return ctx.redirect(redirect, 307);
}
return await ctx.next();