Please add an option to disabled completely prefetch on hover on links #24437
Replies: 11 comments 13 replies
-
I second this, especially as NextJS is being used more for e-commerce applications. I'm currently working on a large ecomm app (half a million products) with category links in the sidebar. Even with prefetch disabled, when a user moves their mouse into that region they inevitably hover over several category links before clicking the one they want. This then results in a long delay before the category page the user clicked on is loaded while the app is still prefetching the ones they unintentionally hovered over. An optional hover delay for prefetching would be great for cases where there are lots of links on a page. Unfortunately at the moment, the user experience is degraded in these cases and lots of unnecessary server requests are being sent. |
Beta Was this translation helpful? Give feedback.
-
@meotimdihia while it would be nice to have this as an option on the core next/link component, it has occurred to me that we can probably get around this by creating a CustomLink component which returns an tag using router.push() as an onClick. This gives us client side routing but without prefetching. I'm just trying it on my app now and it seems to be working ok. Update: Upon testing this in a production build I've found that router.push() doesn't work quite the way I expected on an tag. It seems to trigger a client side route change so I can show a progress loader, but it also trigger an http request at the same time. This might still be a better solution than having prefetching on a large number of links. Hopefully the next/link component might get some more configuration options. |
Beta Was this translation helpful? Give feedback.
-
I don't think that it should be changed globally. For your use case it seems like you just need to make |
Beta Was this translation helpful? Give feedback.
-
Just an additional use case: I don't have many elements for prefetching, but the pages have a bigger size of data. This means that I can't use Building your own link to opt out of prefetching probably works, but I would really like to stick to Next.js' |
Beta Was this translation helpful? Give feedback.
-
I really don't understand the choice here. Even the Setting |
Beta Was this translation helpful? Give feedback.
-
In my opinion, the current behaviour is misleading. I think the cleanest solution would be something like: prefetch={'viewport'|'hover'|'off'} I also agree, that setting prefetch to false should disable all prefetching. To fix the expected behavior, I use the following patch with patch-package. diff --git a/node_modules/next/dist/client/link.js b/node_modules/next/dist/client/link.js
index 3264681..0c44959 100644
--- a/node_modules/next/dist/client/link.js
+++ b/node_modules/next/dist/client/link.js
@@ -287,7 +287,7 @@ const Link = /*#__PURE__*/ _react.default.forwardRef((props, forwardedRef)=>{
if (legacyBehavior && child.props && typeof child.props.onMouseEnter === 'function') {
child.props.onMouseEnter(e);
}
- if ((0, _router).isLocalURL(href)) {
+ if ((0, _router).isLocalURL(href) && p) {
prefetch(router, href, as, {
priority: true
}); |
Beta Was this translation helpful? Give feedback.
-
This seems to have been fixed with #39866 Setting |
Beta Was this translation helpful? Give feedback.
-
in your _app.tsx const router = useRouter()
useMemo(() => {
router.prefetch = async () => { }
}, [router]) |
Beta Was this translation helpful? Give feedback.
-
I had to use @hazae41 monkey patch to disable pre-fetching. In my case the combination of prefetch with next-auth middleware and a secure page using "getServerSideProps" meant the prefetch call was responding with a 200 with an empty body and lacking any specific cache control headers my CDN was caching the empty response which then got served for the "real" call. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Describe the feature you'd like to request
On my websites, a lot of links were stacked. Once the visitor wants to click a link, his mouse will be hover over 10 links+ which degrade performance on the visitor's device and make too many requests server.
This problem is very critical on my website.
And I search people met this problem too:
Describe the solution you'd like
Add delay to mouse hover on prefetch request
Describe alternatives you've considered
Add delay to mouse hover on prefetch request
Beta Was this translation helpful? Give feedback.
All reactions