Hey!
I have an Astro Website running BetterAuth, but with this setup I presume this is an issue with all auth Libaries.
Using simply trailingSlash: 'always', simply breaks everything, and this makes sense. If the auth libary needs a /api/ endpoint to not do that it will break it.
Gemini came up with a middleware redirect solution, which while I guess works, feels a little out of place for the standardised nature of these packages.
I'm looking for clarification on the best fixes for this.
Here is what was suggested:
Middleware.ts with trailingSlash: 'ignore', in Astro Config.
export const onRequest = defineMiddleware(async (context, next) => {
const { pathname } = context.url;
// Skip for API routes, files with extensions, or if already has trailing slash
if (
pathname.startsWith("/api/") ||
pathname.includes(".") ||
pathname.endsWith("/")
) {
return next();
}
// Redirect to URL with trailing slash
return context.redirect(`${pathname}/`, 301);
});```