Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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`.
- Loading branch information
1 parent
671751d
commit e3508c3
Showing
8 changed files
with
82 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
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); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters