Intermittent problem routing in local dev

new to the channel ... I've got a full-stack HTMX JAMstack webapp running on Cloudflare Pages with functions and static, been handling auth etc. I needed to write a middleware to check the auth but I've noticed sometimes the router doesn't navigate to a sub-route and keeps navigating to the parent. It was happening with one page, and now it's happening with all of them, with no code changes. (this is on local dev) I'm logging out the request URL in the middleware and it's trying to get the parent page before I do anything, so it's definitely not my code. any ideas?
import { parseToken } from '@lib/token';

export const authentication: PagesFunction = async (context) => {
console.log('middleware', context.request.url);
if (parseToken(context.request)) {
return await context.next();
} else {
console.error('redirect');
const url = new URL(context.request.url);
return Response.redirect(url.origin, 301);
}
};

export const onRequest = [authentication];
import { parseToken } from '@lib/token';

export const authentication: PagesFunction = async (context) => {
console.log('middleware', context.request.url);
if (parseToken(context.request)) {
return await context.next();
} else {
console.error('redirect');
const url = new URL(context.request.url);
return Response.redirect(url.origin, 301);
}
};

export const onRequest = [authentication];
This is a _middleware.ts file inside my functions/dash/ folder (I had to put it in the subfolder as a _middleware in root folder also gets fire when getting static files, which is a pain) Anyway: when I try to navigate to URL http://127.0.0.1:3000/dash/products it logs out middleware http://127.0.0.1:3000/dash ... it works fine when published to Cloudflare
1 Reply
stukennedy
stukennedyOP2y ago
it may be related to the fact I forced it to use port 3000 stopped happening since I used the default port for dev
Want results from more Discord servers?
Add your server