Ability to Handle a Prefetch in Middleware #37736
-
Describe the feature you'd like to requestI would like to have some sort of way to detect if a middleware file is being called by a prefetch. Due to Next.js Links prefetching the content of the webpage when the link loads, it causes the middleware to run at undesirable times. I would like a variable to know if the request is prefetched, so I can have custom logic to prevent my code running on prefetch. Describe the solution you'd likeI am working on a custom analytics solution to fit my needs with Next.js. I am currently using a middleware file to get analytics on every page, but because Next.js prefetches any links on the page, I have extra page views in my analytics. I would like to have a way to only trigger middleware on the actual viewing of the page. Describe alternatives you've consideredI have considered disabling prefetching on all of my links, but I would lose performance and the page would still prefetch on hover. I have also considered using the built-in |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 22 replies
-
It has been about a week since I created this. I have since started using an open source solution for analytics that is entirely client side. This problem is solved for me now, but server side analytics using middleware might help if the user has client side javascript disabled. It may also help keep the javascript size down and improve performance. |
Beta Was this translation helpful? Give feedback.
-
I caught up with a similar situation, where I want a code block not to be executed for prefetch calls. Basically, I am implementing A/B experiment through GrowthBook in the middleware which means 50% of the users will be seeing a page "A" and another 50% of users will be seeing page "B". So I want the A/B experiment logic to be executed for the users who are really visiting the page, not for those who are just hovering over the links. Otherwise, the metrics for experiments would be messed up. I tried using A/B experiments on the client-side but it impacts the CLS (Cumulative Layout Shift) and user experience. |
Beta Was this translation helpful? Give feedback.
-
I ran into a similar issue. For me the fix was to check the
|
Beta Was this translation helpful? Give feedback.
-
What about this SO answer?
|
Beta Was this translation helpful? Give feedback.
-
Is it possible de avoid prefetch to trigger middleware ? |
Beta Was this translation helpful? Give feedback.
-
Ok, I i've found a solution for this. It seems the CSP docs hold a clue:
Use the export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
{
source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
],
},
],
} When added, I don't see logs in Vercel anymore of prefetches that go through the src/middleware Function. Win! 🎉 |
Beta Was this translation helpful? Give feedback.
-
By examining the presence or absence of the next-url header: Prefetching requests will have a next-url header pointing to the pathname of the current page. example:
} |
Beta Was this translation helpful? Give feedback.
-
This is an issue in how NextJS handles the order of Request headers: You can retrieve the headers such as The fix to this problem is setting these headers before the custom middleware function is run.Can this be prioritised? I see many people running on same trouble. |
Beta Was this translation helpful? Give feedback.
-
we got it by const isPrefetch = |
Beta Was this translation helpful? Give feedback.
Ok, I i've found a solution for this. It seems the CSP docs hold a clue:
https://nextjs.org/docs/app/building-your-application/configuring/content-security-policy
Use the
missing
array in the Middleware config to only run the Middleware when the given headers are missing. Example: