solidjs / solid-router

A universal router for Solid inspired by Ember and React Router
MIT License
1.14k stars 147 forks source link

feat: intent based route preloading #279

Closed marbemac closed 10 months ago

marbemac commented 1 year ago

This PR adds preload (similar to remix) and preloadDelay (similar to tanstack-router) options to the Router and Link components.

I'm slowly making my way over from the world of React, so the particular implementation I chose might not be ideal - happy to adjust as ya'll see fit. I tried to minimize the amount of change, but it felt a little awkward to get access to the relevant route data and asset preload functions in the right spots given how things are currently organized (I ended up storing branches down to the route context - I'm not sure if there are any potential side effects to doing this).

Usage

// Set global defaults via Router
<Router preload="intent" preloadDelay={500} />

// Override global default on a per Link basis
<Link preload="intent" preloadDelay={500} />

If the target route has a data function AND/OR if it has a lazy loaded component, the data func and lazy assets will be prefetched.

ryansolid commented 1 year ago

Thank you. This is interesting. Sorry it took me so long to get back here. I am not sure if you saw the work Ryan Turnquist did on https://github.com/solidjs/solid-router/compare/main...next

He was the original co-author of this library and started on a preload approach but I was never sure if this is how we wanted to do it. The problem with re-running the data functions is it does nothing unless we cache the results.. we had decided we'd leave that to the library, but we never had a blessed solution so the feature didn't really work well.

We talked about keeping the results of the data function around and then passing them into the actual render as available as that wouldn't waste work, but they would be live queries at that point so managing their lifecycle might be hard. I'm gathering you are doing the latter. Can you describe a bit more about the behavior? Am I correct in my understanding? LIke if something is preloaded at what point to do we throw it away?

marbemac commented 1 year ago

No worries! Ah no I hadn't seen Ryan's work.

Yeah... imho data caching prob should not be a concern of solid-router. However, this opt-in preload behavior gives end users the option to combine solid-router with other libs that handle data caching - for example this functionality pairs very nicely with solid-query.

ryansolid commented 10 months ago

Happy to say this will be resolved by #312