I need clarification caching for static pages
Answered
Catla posted this in #help-forum
CatlaOP
I am currently experimenting with the new caching on Nextjs v15.0.2. I read in the documentation that pages are no longer cached by default.
So, I set up a /events/create route with simple form to create an event, which is a static page, but I want it to be accessible only to logged in users. I set up a middleware to match all routes, but whenever I visit the create event page, the middleware doesn’t trigger, which means that the page is still being cached on the client side as no new request was made to the server when I checked the network tab in the chrome dev tool, I tried to use the experimental staleTimes and set both static and dynamic to 0, but it made no difference.
Seems the middleware only gets triggered the first time I access the page. The middleware works well for dynamic pages.
Is there a way to implement this if I want to keep the page static? Please let me know if you need more info.
So, I set up a /events/create route with simple form to create an event, which is a static page, but I want it to be accessible only to logged in users. I set up a middleware to match all routes, but whenever I visit the create event page, the middleware doesn’t trigger, which means that the page is still being cached on the client side as no new request was made to the server when I checked the network tab in the chrome dev tool, I tried to use the experimental staleTimes and set both static and dynamic to 0, but it made no difference.
Seems the middleware only gets triggered the first time I access the page. The middleware works well for dynamic pages.
Is there a way to implement this if I want to keep the page static? Please let me know if you need more info.
Answered by Anay-208
What are the chances of that actually?
If it happens, the chances are that either use cleared their cookie.
Or if it expires in a very rare case, they'll be unauthorised when they attempt to create the event
If it happens, the chances are that either use cleared their cookie.
Or if it expires in a very rare case, they'll be unauthorised when they attempt to create the event
20 Replies
does it trigger when you refresh the paeg
CatlaOP
Yes it does
That is called client side caching
CatlaOP
Previously, when I did something similar on Nextjs v15 canary 112, it worked perfectly
It is needed because if a user spam clicks back and forth to different pages, your app/db won't get requests(it'll be cached)
You can check out invalidation on the above page
CatlaOP
I went through the docs, it says pages are not cached by default, also tried setting the staleTimes to 0
pages are not, but router is cached by default
You can visit the above link to learn more
on how to disable it
CatlaOP
I went over it several times, it doesn’t clearly state how to disable it completely, except making it dynamic or using router.refresh
But why do you need to run a middleware everytime a user goes back to the path?
CatlaOP
I’m not trying to run it on backwards navigation, I want it to run when the user goes to homepage and later clicks the create event page
When the user first time goes to create event, it will work
CatlaOP
So when their session expires, going to the page still works. With the way it works currently, seems like we can’t protect statically rendered pages using middleware
What are the chances of that actually?
If it happens, the chances are that either use cleared their cookie.
Or if it expires in a very rare case, they'll be unauthorised when they attempt to create the event
If it happens, the chances are that either use cleared their cookie.
Or if it expires in a very rare case, they'll be unauthorised when they attempt to create the event
Answer
CatlaOP
Well, I guess I’m overthinking it. They won’t be able to create the event either way
Thanks for your help
Can you mark solution?