Next.js Discord

Discord Forum

Middleware not always working?

Answered
StilauGamer posted this in #help-forum
Open in Discord
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?
Answered by Ray
sound like that route hit the router cache
View full answer

14 Replies

Answer
@StilauGamer Router cache?
yes
@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 = nextConfig
oooooh...
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?
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 😅