How to make an redirect? #511
-
Hello, I have to make redirect for auth process. Best, |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 8 replies
-
You can return a Response with a Location header in your handler functions: export function handler(req: Request): Response {
// ...
const headers = new Headers({
location: new URL(req.url).origin,
});
return new Response(null, {
status: 302,
headers,
});
} Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
If you have a fully qualified URL, you can use export function handler(req: Request): Response {
return Response.redirect("https://example.com", 307);
} If you want to redirect to a relative path on the current domain: export function handler(req: Request): Response {
return new Response("", {
status: 307,
headers: { Location: "/my/new/relative/path" },
});
} |
Beta Was this translation helpful? Give feedback.
-
@lucacasonato hello, can you tell me how to get the user back. |
Beta Was this translation helpful? Give feedback.
-
I am having some issues with this, I have a route: import { HandlerContext, Handlers } from "$fresh/server.ts";
import { MiddlewareState } from "./_middleware.ts";
export const handler: Handlers = {
GET(_, ctx: HandlerContext<unknown, MiddlewareState>) {
const pb = ctx.state.pb;
pb.authStore.clear();
return new Response(undefined, {
status: 307,
headers: { Location: "/" },
});
},
}; But when I navigate to I have also tried the technique recommended in this thread: const url = new URL(req.url);
url.pathname ="/";
return Response.redirect(url, 307); But got the same result. Code is all opensource and can be viewed here. |
Beta Was this translation helpful? Give feedback.
-
I have this code and I see a blank page and it does not redirect.
I do not have any middleware logic. |
Beta Was this translation helpful? Give feedback.
-
Why would this line error out
with the error |
Beta Was this translation helpful? Give feedback.
If you have a fully qualified URL, you can use
Response.redirect
.If you want to redirect to a relative path on the current domain: