From d601f4cef43eb0a7f199a11be5f222e843eede51 Mon Sep 17 00:00:00 2001 From: Furkan Demir <48316033+fdemir@users.noreply.github.com> Date: Thu, 10 Aug 2023 22:26:00 +0300 Subject: [PATCH] www: add middlware redirect section (#1615) --- docs/latest/concepts/middleware.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/latest/concepts/middleware.md b/docs/latest/concepts/middleware.md index 5f272f10..f611d5ff 100644 --- a/docs/latest/concepts/middleware.md +++ b/docs/latest/concepts/middleware.md @@ -197,3 +197,25 @@ That first `route` request is for when `Fresh` responds with the root level `index.tsx` route. The rest, as you can see, are either `internal` or `static` requests. You can use `ctx.destination` to filter these out if your middleware is only supposed to deal with routes. + +## Middleware Redirects + +If you want to redirect a request from a middleware, you can do so by returning: + +```ts +export function handler(req: Request): Response { + return Response.redirect("https://example.com", 307); +} +``` + +`307` stands for temporary redirect. You can also use `301` for permanent +redirect. You can also redirect to a relative path by doing: + +```ts +export function handler(req: Request): Response { + return new Response("", { + status: 307, + headers: { Location: "/my/new/relative/path" }, + }); +} +```