Next.js Discord

Discord Forum

Is there a way to tell middleware that a page (App Router) is visited via client-side navigation?

Answered
Rough harvester ant posted this in #help-forum
Open in Discord
Avatar
Rough harvester antOP
Context:

In middleware, we’re doing auth checks, and this is an “expensive” process that ideally should only be called during a hard page visit. So we want to skip calling this middleware when a page is visited via client-side navigation. The next.config.js skipMiddlewareUrlNormalize doesn’t seem to be doing anything on app router.
Answered by Alfonsus Ardani
you could check it via the headers
View full answer

3 Replies

Avatar
Alfonsus Ardani
you could check it via the headers
Answer
Avatar
Alfonsus Ardani
using headers().get('sec-fetch-dest') === 'document' will endure that the the code runs only happens on hard reload
Avatar
Rough harvester antOP
cool, thanks I got it to work.