Anchor options
Edit this page on GitHubdata-sveltekit-prefetchpermalink
SvelteKit uses code splitting to break your app into small chunks (one per route), ensuring fast startup times.
For dynamic routes, such as our src/routes/blog/[slug]/+page.svelte
example, that's not enough. In order to render the blog post, we need to fetch the data for it, and we can't do that until we know what slug
is. In the worst case, that could cause lag as the browser waits for the data to come back from the server.
We can mitigate that by prefetching the data. Adding a data-sveltekit-prefetch
attribute to a link...
<a data-sveltekit-prefetch href="blog/what-is-sveltekit">What is SvelteKit?</a>
...will cause SvelteKit to run the page's load
function as soon as the user hovers over the link (on a desktop) or touches it (on mobile), rather than waiting for the click
event to trigger navigation. Typically, this buys us an extra couple of hundred milliseconds, which is the difference between a user interface that feels laggy, and one that feels snappy.
Note that prefetching will not work if the router
setting is false
.
You can also programmatically invoke prefetch
from $app/navigation
.
data-sveltekit-reloadpermalink
By default, the SvelteKit runtime intercepts clicks on <a>
elements and bypasses the normal browser navigation for relative (same-origin) URLs that match one of your page routes. We sometimes need to tell SvelteKit that certain links need to be handled by normal browser navigation. Examples of this might be linking to another page on your domain that's not part of your SvelteKit app or linking to an endpoint.
Adding a data-sveltekit-reload
attribute to a link...
<a data-sveltekit-reload href="path">Path</a>
...will cause browser to navigate via a full page reload when the link is clicked.
Links with a rel="external"
attribute will receive the same treatment. In addition, they will be ignored during prerendering.
data-sveltekit-noscrollpermalink
When navigating to internal links, SvelteKit mirrors the browser's default navigation behaviour: it will change the scroll position to 0,0 so that the user is at the very top left of the page (unless the link includes a #hash
, in which case it will scroll to the element with a matching ID).
In certain cases, you may wish to disable this behaviour. Adding a data-sveltekit-noscroll
attribute to a link...
<a href="path" data-sveltekit-noscroll>Path</a>
...will prevent scrolling after the link is clicked.