-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add option to prefetch links by default #790
Comments
i do like this! i'd change the name to perhaps offer the option to opt out by page level as well - in the |
Great idea! Updated original post with that edit.
That would make sense. We could potentially use the same syntax for prerendering to keep things consistent. <script context="module">
export const prefetchLinks = false;
</script> |
Not sure about the |
I think this is a great idea, moreso for content you don't have control over (eg: links in content from a CMS) than for simply saving keystrokes. It's also the default of NextJS, which is an industry leader for a reason. I'd second that the |
I'm all for this. I'd go even further and propose to make prefetching links by default the default such that there is an option to opt-out instead of opt-in. SvelteKit should be fast by default, not make the user search around for options to tweak to make it even faster. Instead of when to opt-in, the manual should simply clarify when to opt-out. |
Also, there should be someway for the prefetched links to not call the load method. The load call should be deferred untill the user actually clicks the link. For example, if you are building a football match app, then just hovering over the match link will prefetch the js bundle including the current score by calling load. So, if the user clicks the match link after 1 hour, he will be seeing the score of 1 hour before (which is bad). Ideally we want to call load only after the user actually clicks it (I think NextJS does it this way). So, there should be following options on the achor tag: prefetch_hover_no_load And also, some options to make prefetching of links in viewport (Like in NextJS) |
Is there any movement on this / input from maintainers? I think it's a bit of a no-brainer feature and after coming back to Sveltekit after using React/Next for work for the last 6 months it jumped out as a rough edge. I'd strongly second @vwkd in that Sveltekit should be as performant as possible by default, rather than requiring this flag on every anchor in the app, especially those which cannot be controlled by the user. |
Prefetching is currently implemented by hovering on Therefore, it is a desktop only feature. How can we prefetch on mobile ? |
For mobile, SvelteKit listens to the |
@Conduitry came up with a brilliantly simple solution to this problem the other day: <nav>
<svg viewBox="0 0 2 3" aria-hidden="true">
<path d="M0,0 L1,2 C1.5,3 1.5,3 2,3 L2,0 Z" />
</svg>
- <ul>
+ <ul data-sveltekit-prefetch>
<li class:active={$page.url.pathname === '/'}>
- <a data-sveltekit-prefetch href="/">Home</a>
+ <a href="/">Home</a>
</li>
<li class:active={$page.url.pathname === '/about'}>
- <a data-sveltekit-prefetch href="/about">About</a>
+ <a href="/about">About</a>
</li>
<li class:active={$page.url.pathname === '/todos'}>
- <a data-sveltekit-prefetch href="/todos">Todos</a>
+ <a href="/todos">Todos</a>
</li>
</ul>
<svg viewBox="0 0 2 3" aria-hidden="true">
<path d="M0,0 L0,3 C0.5,3 0.5,3 1,2 L2,0 Z" />
</svg>
</nav> (Note that In other words: instead of just checking the element itself, we check its parents until we find a In that case we'd need a way to opt out for a subtree: <body data-sveltekit-prefetch>
<!-- these are all prefetched -->
<a href="/a">...</a>
<a href="/b">...</a>
<a href="/c">...</a>
<!-- but these are not -->
<div data-sveltekit-prefetch="off">
<a href="/x">...</a>
<a href="/y">...</a>
<a href="/z">...</a>
</div>
</div> (In future we could add other modes besides 'prefetch when a mouse/finger stops over a link', like For consistency, it would be nice to do the same thing for <div data-sveltekit-reload>
<!-- all links inside here will trigger a full page reload... -->
<div data-sveltekit-reload="off">
<!-- ...except these ones -->
</div>
</div>
<div data-sveltekit-noscroll="off">
<!-- all links inside here will preserve the current scroll position... -->
<div data-sveltekit-noscroll>
<!-- ...except these ones -->
</div>
</div> |
Would this mean we could just add the prop to a root layout And FWIW it's still really unfortunate that the |
Applying it to And yeah, I know :( The hope is that with the other change we'll only have to look at it rarely |
Oh yeah derp, but sweet! Sounds like a good solution, and in line with moving more stuff from config to code (ssr, prerender, hydrate, etc). I do think sveltekit should be as performant as possible by default, which means defaulting to prefetching like Next does, but I guess under this scheme that can be 'solved' with documentation and adding the attribute to |
Does that mean that I mean, this issue still not closed… Not available in versions published on npm yet? UPD: seems to be available in @sveltejs/kit@1.0.0-next.454, not sure why issue still not closed |
@jerrygreen this issue is for having |
* failing tests for #790 * implement parent data-sveltekit-foo attributes * changeset * update docs * move data-sveltekit-prefetch to <nav> in demo app * update docs * fix link * whoops, meant to uncomment * fix test * remove unused function * Update packages/kit/src/runtime/client/client.js Co-authored-by: Conduitry <git@chor.date> * Update documentation/docs/09-link-options.md Co-authored-by: Conduitry <git@chor.date> * Update .changeset/fresh-lemons-fail.md Co-authored-by: Conduitry <git@chor.date> Co-authored-by: Conduitry <git@chor.date>
Is your feature request related to a problem? Please describe.
There should be an option to prefetch links by default. In my opinion, page transitions should always be instantaneous if possible. And this is the default in other frameworks like Next.js and Gatsby.
Now you could just add
sveltekit:prefetch
to every single<a>
tag, but there are some reasons why this isn't practical/possible.And not only are faster transitions better user experience, in some industries (like eCommerce), the extra 150-500 milliseconds can mean millions of dollars of more revenue.
Describe the solution you'd like
Add field to
svelte.config.cjs
to automatically prefetch pages. Perhaps something like this?Now there are likely valid reasons to not prefetch certain links, in which case you also need a way to opt-out of prefetching. This example seems like intuitive syntax:
Describe alternatives you've considered
As far as I know, the current best solution is to add
sveltekit:prefetch
to every single<a>
tag.Alternatively, you could call
prefetchRoutes(routes)
on every page, but this is wasteful (all routes will be prefetched) and may not work with dynamic content.How important is this feature to you?
I'll keep using Svelte/kit if this doesn't get added, but I'd be ecstatic if it gets added!
The text was updated successfully, but these errors were encountered: