Prefetch on hover #187
dillonkearns
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In
elm-pages
1.0, there was some code to automatically prefetch the data for a page when you hover over a link.That ended up needing the client bundle to load up the list of every static route, and then checking if a route is a known static route before prefetching it:
#60
#37
This is clunky and not great for performance. It means that we need to:
In
elm-pages
2.0, I'm going for a leaner approach. Since there is now aRoute
type, we can make some nice helpers for alink
helper function that takes a route and adds a special HTML attribute to tellelm-pages
that it's a prefetchable link.Here's a working example in the current beta:
elm-pages/examples/docs/src/Page/Blog.elm
Lines 196 to 217 in 33a10ed
It successfully prefetches on hover.
Looking for feedback
I took inspiration from the way SvelteKit is doing this with using a specific HTML attribute for pages to prefetch.
https://github.com/sveltejs/kit/blob/dad93fc39721b711f06a316b0842d04f024ba69e/packages/kit/src/runtime/client/router.js#L75-L94
I'd be interested to hear any feedback on this new approach in general.
I'm also trying to find a nice abstraction for this. Since people will be using different drop-in replacement HTML libraries, it's hard to have one specific library.
But we can provide a helper in the generated
Route.elm
module that provides vanillaelm/html
Attribute
s since almost all Elm view libraries will have a way to pull in vanilla attributes or tags.In the current beta branch, I'm defining these helpers in
Route.elm
:I'm also considering adding a helper function that allows you to programmatically prefetch.
That would allow you to do custom prefetching if you wanted to do some low-level optimizations to prefetch the data for a page based on loading a page, scroll position, etc.
Beta Was this translation helpful? Give feedback.
All reactions