Next.js Discord

Discord Forum

Middleware next auth

Unanswered
Maltese posted this in #help-forum
Open in Discord
MalteseOP
When this middleware function is triggered?
export default withAuth(
  function middleware(req) {

27 Replies

By exporting a matcher
MalteseOP
yes
I did that
but somethins its working, sometimes not
I mean, if I refresh my page now it will run, but if I refresh it again and again ( without cache ) it wont run
@Maltese but somethins its working, sometimes not
What do you mean, whenever there is a non cached request from client side it’ll work
Search docs for disabling cache
MalteseOP
so because my route is static it will run the middleware only once?
isnt middleware for every request?
@Maltese isnt middleware for every request?
Unique request
MalteseOP
what unique request means
I put export const dynamic = 'force-dynamic' and no result
@Maltese what unique request means
IIRC, if the cookies or headers are different
Or Params
@Maltese I put `export const dynamic = 'force-dynamic'` and no result
After putting, restart app once.
@"use php" After putting, restart app once.
MalteseOP
still no result
Try deleting .next folder
If still doesn’t work, I’ll check once I reach home
MalteseOP
here is my repo, if you can please give it a try, because I can't figure out why the middleware is not running on every request - https://github.com/bockster6669/blog-app
MalteseOP
if I use
export default function middleware() {
  console.log('middleware ran')
}
instead of
export default withAuth(
  function middleware(req) {
    console.log('middleware ran')
  },
);
then everything works as expected, the middleware is ran for every request, but if I use withAuth middleware than its not running for every request
MalteseOP
interesting but if I remove the regex and instead I put hard coded paths, then it works fine
export const config = {
matcher: ['/create-post', '/', '/about'],
};
but the regex I am using its from Clerk and it should cover all routes
umm can you send the regex used by clerk