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:
@@ -1,13 +1,7 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers<unknown, { data: string }> = {
|
||||
GET(req) {
|
||||
const url = new URL(req.url);
|
||||
return new Response(null, {
|
||||
status: 302,
|
||||
headers: {
|
||||
"Location": url.origin,
|
||||
},
|
||||
});
|
||||
GET(_req, ctx) {
|
||||
return ctx.redirect(ctx.url.origin, 302);
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user