Middleware not always working?
Answered
StilauGamer posted this in #help-forum
Hey!
So currently I am trying to use the middleware file to run a check to the backend on every request done so I can check if he has access to the page or not...
Sometimes if I click the same route twice it will not check after that, meaning if I click a button that I don't have access to twice and then give myself access.. It will still not work?
Why so?
So currently I am trying to use the middleware file to run a check to the backend on every request done so I can check if he has access to the page or not...
Sometimes if I click the same route twice it will not check after that, meaning if I click a button that I don't have access to twice and then give myself access.. It will still not work?
Why so?
14 Replies
@StilauGamer Hey!
So currently I am trying to use the middleware file to run a check to the backend on every request done so I can check if he has access to the page or not...
Sometimes if I click the same route twice it will not check after that, meaning if I click a button that I don't have access to twice and then give myself access.. It will still not work?
Why so?
sound like that route hit the router cache
Answer
@Ray sound like that route hit the router cache
Router cache?
@StilauGamer Router cache?
It's when nextjs caches the page in the client side, lemme send docs
30 sec for dynamic route and 5min for static route
Go read it, router cache is imo the most weird part of nextjs caching :)
you could adjust it in next.config.js with next >= 14.2.0
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
staleTimes: {
dynamic: 30,
static: 180,
},
},
}
module.exports = nextConfigoooooh...
So basically when I click on the page it will cache that this went through, and now it will be not checked in the middleware till the stale time is over?
So basically when I click on the page it will cache that this went through, and now it will be not checked in the middleware till the stale time is over?
it will only make a new request after the stale time
Oooooh okay, makes sense
try to click that button after 30 sec if its a dynamic route, you should see the middlware run
Yeah, I can tell..
This makes a lot more sense now, thanks 😅
This makes a lot more sense now, thanks 😅