-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to disable prefetching for certain links. #13759
Comments
@gc if you don't want some of the functionality (rather, the core functionality) of gatsby-link, perhaps just use a regular That seems to be a much better use case for this functionality, rather than extending gatsby-link in perhaps fringe-y ways. Every additional modification to core components introduces some small measure of complexity that we then have to maintain. Rather, I would recommend a custom link helper, e.g. import React from 'react'
import { Link } from 'gatsby'
function MyLink({ children, to, prefetch, ...rest }) {
if (!prefetch) {
return <a href={to} {...rest}>{children}</a>
}
return <Link to={to} {...rest}>{children}</Link>
}
MyLink.defaultProps = {
prefetch: true
}
export default MyLink Going to close this as answered! Thanks for the question! |
Sounds good. Thanks |
@DSchau but with your suggestion, you lose the beauties of having some persistent data going on through (related to #23165) |
Sorry -- what do you mean here? |
If you rely on Link, you can have a persistent e.g. header that doesn't get reloaded between page changes But if you substitute the links for a elements to prevent prefetching, then you lose that persistency (for example with authentication within wrapRootElement, which now will be executed in every page as everything gets reloaded) |
Perhaps use reach router link? |
I worried about this while trying to optimize lightspeed scores, and at the end I've concluded that this Still, I think would be nice to have a gatsby-option to just disable prefetching, or delay it, or anything, but as you say perhaps react link could work (don't know) |
I have a use case for selective disabling preloading. I have a link in a menu that goes to a page that requires basic auth. If you mouse over the link a basic auth prompt pops up as gatsby tries to pre-load it. Seems like a perfect case for being able to turn pre-loading off on a specific link. For now I'll just make those links a tags though. |
Yeah, I don't think changing your markup to anchors is the right choice since it breaks you out of your SPA. Link lets you stay in your SPA and so an option to stunt prefetching is a great idea. Most of the links in our nav are our bigger pages with lots of content which effects our homepage loads since the data needs to load in for these other pages which is not ideal. |
I also think it would be a good idea to have such a thing! |
Summary
The ability to tell a Link component to not pre-fetch.
Basic example
It could possibly be named
prefetch
instead ofnoprefetch
and just have it set to true by default, either way:Motivation
To give developers finer control over their links, I personally would like to disable it for some certain links to reduce network requests/bandwidth usage and for debugging purposes.
In a closed issue where a user asks how to do this, a work-around for noprefetch is given and it has some upvotes which shows to some level that people want it: #2384 (comment) - as this comment suggests, an ability to globally disable prefetching would be good too, it being just a prop that you can opt-out of with a falsey value seems to be good for all.
This could also allow full control of when (as opposed to if), in a way like this:
this.state.shouldPrefetch
will be false by default, so it wont be prefetched when the page is loaded, however based on some event or logic, e.g. X component has completed Y action, the user has interacted with the page, or depending on the browser/network, and any other logic.The text was updated successfully, but these errors were encountered: