js
jsβ€’7mo ago

Route redirect handlers

I tried using this code to redirect a route to another route.
export const handler: Handlers = {
GET(_req: Request, _ctx: FreshContext) {
const headers = new Headers();
headers.set('location', '/c/xyz');
return new Response(null, {
status: 302,
headers,
});
},
};
export const handler: Handlers = {
GET(_req: Request, _ctx: FreshContext) {
const headers = new Headers();
headers.set('location', '/c/xyz');
return new Response(null, {
status: 302,
headers,
});
},
};
It works-- its loading "/c/xyz" (correctly, as it should). But the URL doesnt' change. Is there a way to make this work AND change the URL?
8 Replies
Deno AI Helper
Deno AI Helperβ€’7mo ago
Based on the documentation, the code you've provided should indeed create a redirect response that, when processed by the browser, changes the URL to the location specified in the location header. If the URL in the browser's address bar is not changing, it could be due to the way the redirect response is being handled on the client side. Here are a few things to check or try: 1. Client-Side JavaScript Handling: If you have client-side JavaScript that is intercepting fetch requests and handling responses, it might not be processing the redirect correctly. Ensure that any client-side logic allows the browser to follow the redirect as it normally would. 2. Cache Headers: Make sure that the response is not being cached in a way that would prevent the browser from performing the redirect. You can set cache-control headers to ensure the response is not cached. 3. Server-Side Rendering: If you're using server-side rendering, ensure that the server is properly returning the redirect response to the client. 4. HTTP vs. HTTPS: If there is a mismatch between HTTP and HTTPS (for example, if the site is served over HTTPS but the redirect location is HTTP), the browser may block the redirect for security reasons. Ensure that the scheme of the redirect location matches the scheme of the original request. 5. Browser Behavior: Test the behavior in different browsers to rule out browser-specific issues. 6. Inspect Network Traffic: Use the browser's developer tools to inspect the network traffic and verify that the server is indeed sending a 302 status code with the correct location header. If after checking these points the issue persists, it might be helpful to look at the broader context of how the request is being made and handled. The
Deno AI Helper
Deno AI Helperβ€’7mo ago
documentation does not provide a specific solution for the case where the URL does not change despite a 302 redirect, so it's important to consider the client-side behavior and other factors in your application that might affect how redirects are processed. πŸ¦•
MitchMc
MitchMcβ€’7mo ago
It’s a known bug. Probably will be fixed with the next release. https://github.com/denoland/fresh/issues/2404
GitHub
Client Address Bar wont update after 303 redirect response. Β· Iss...
The correct Page will load but the Address Bar is still showing the last Page. At the API the status 303 is used with a new location header.
MitchMc
MitchMcβ€’7mo ago
I am using a workaround but it involves client side code. If you don’t mind it you can easily get away with it.
js
jsOPβ€’7mo ago
thank you, i added to your issue. Also, i found that technically this is a non-issue as long as the app never uses outdated links. I expanded on that on my github comment. I suspect you're doing the same.
MitchMc
MitchMcβ€’7mo ago
I use it to redirect the client after login. Not on outdated links. However there are many use cases where it might be needed to redirect from server side. For example if you created a post, you might want to redirect back to the timeline and so on. πŸ™ƒ
js
jsOPβ€’7mo ago
ah, that's interesting then. I just hope they don't make the router system/framework bloated though. πŸ™ @marvinh. please keep the framework non-bloated. I hope there won't be a secondary routing system on top of location API but this does seem like a real issue.
marvinh.
marvinh.β€’7mo ago
we'll always keep Fresh as sleek as possible. It's meant to highlight the strengths of Deno after all