Infinite redirects #2333
Replies: 1 comment 9 replies
-
You're fighting against Fresh's built in trailing slash handling. By default Fresh will redirect any route with a trailing slash to the route without one. Then you detect that in your middleware and redirect to the route with the trailing slash. Then Fresh sees that the route has a trailing slash and redirects back to the route without one, etc. You basically created a loop. To tell Fresh to always use trailing slash routes set this in your import { defineConfig } from "$fresh/server.ts";
export default defineConfig({
router: {
trailingSlash: true,
},
}); Furthermore your middleware will also lowercase the search params of an URL which can break your site if you use them elsewhere. This will turn urls like Taking this into account and by reusing the already existing import { FreshContext } from "$fresh/server.ts";
export function handler(_req: Request, ctx: FreshContext) {
if (
(ctx.destination === "route" || ctx.destination === "notFound") &&
/[A-Z]/.test(ctx.url.pathname)
) {
ctx.url.pathname = ctx.url.pathname.toLowerCase();
return Response.redirect(ctx.url, 301);
}
return ctx.next();
} P.S.: By adding the language to the fenced code block you can make your code be syntax highlighted on GitHub, see https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks |
Beta Was this translation helpful? Give feedback.
-
I've attached my middleware above. I've also shown places where it works ,and where it isn't . Spent all day on trying to find out what is wrong . I can't find out . I see that
req.url
param andctx.url.hostname
is the same , as it was ( old one ) , after redirects . Can someone help to find out ? Here isfresh
version"$fresh/": "https://deno.land/x/[email protected]/",
Beta Was this translation helpful? Give feedback.
All reactions