Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Programmatically preload secondary routes #160

Closed
Rich-Harris opened this issue Mar 4, 2018 · 2 comments · Fixed by #161
Closed

Programmatically preload secondary routes #160

Rich-Harris opened this issue Mar 4, 2018 · 2 comments · Fixed by #161

Comments

@Rich-Harris
Copy link
Member

Rich-Harris commented Mar 4, 2018

At present, as soon as the first route is rendered, Sapper preloads the modules for all your secondary routes. In a lot of cases that makes sense, but not always.

Perhaps it should be a function that the user can choose whether or not to run:

import { init, preloadRoutes } from 'sapper/runtime.js';
import { routes } from './manifest/client.js';

init(document.querySelector('#sapper'), routes).then(() => {
  preloadRoutes();
});

With no arguments, it would preload all routes. Or, you could provide an array of routes:

// could replace '[slug]' with '*' or 'whatever', it'd still match
preloadRoutes(['/about', '/blog/[slug]']);

Not sure how you'd specify error pages in the second case. Maybe '4xx' and '5xx' get special treatment.

@mrkishi
Copy link
Member

mrkishi commented Mar 4, 2018

(I was actually about to open a similar issue..)

That looks good!

And perhaps by default Sapper could wait a bit before preloading all other routes?

I saw definitive Lighthouse improvement (of seconds) by not preloading everything right away when moving from Sapper to Svelte in my app. Granted, there were quite a few other differences, but this was the most glaring one (the architecture was largely the same).

@Rich-Harris
Copy link
Member Author

Interesting, I hadn't considered that it would actually harm Lighthouse scores. In #161, secondary routes aren't loaded by default — the expectation is that you'd do something like this...

init(target, routes).then(() => preloadRoutes());

...and if you wanted to wait a beat, you could do that like so:

init(target, routes).then(() => {
  setTimeout(() => preloadRoutes(), 2000);
});

Rich-Harris added a commit that referenced this issue Mar 5, 2018
add preloadRoutes function
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants