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

Commit 7e2f5f8

Browse files
committed
add preloadRoutes function - closes #160
1 parent acef0e8 commit 7e2f5f8

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/runtime/index.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ function render(Component: ComponentConstructor, data: any, scroll: ScrollPositi
5959
detach(start);
6060
detach(end);
6161
}
62-
63-
// preload additional routes
64-
routes.reduce((promise: Promise<any>, route) => promise.then(route.load), Promise.resolve());
6562
}
6663

6764
component = new Component({
@@ -268,3 +265,15 @@ export function goto(href: string, opts = { replaceState: false }) {
268265
window.location.href = href;
269266
}
270267
}
268+
269+
export function preloadRoutes(pathnames: string[]) {
270+
if (!routes) throw new Error(`You must call init() first`);
271+
272+
return routes
273+
.filter(route => {
274+
return !pathnames || pathnames.some(pathname => route.pattern.test(pathname));
275+
})
276+
.reduce((promise: Promise<any>, route) => {
277+
return promise.then(route.load);
278+
}, Promise.resolve());
279+
}

test/app/app/client.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { init } from '../../../runtime.js';
1+
import { init, preloadRoutes } from '../../../runtime.js';
22
import { routes } from './manifest/client.js';
33

44
window.init = () => {
55
return init(document.querySelector('#sapper'), routes);
6-
};
6+
};
7+
8+
window.preloadRoutes = preloadRoutes;

test/common/test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ Nightmare.action('init', function(done) {
2323
this.evaluate_now(() => window.init(), done);
2424
});
2525

26+
Nightmare.action('preloadRoutes', function(done) {
27+
this.evaluate_now(() => window.preloadRoutes(), done);
28+
});
29+
2630
function run(env) {
2731
describe(`env=${env}`, function () {
2832
this.timeout(30000);
@@ -155,7 +159,7 @@ function run(env) {
155159
});
156160

157161
it('navigates to a new page without reloading', () => {
158-
return capture(() => nightmare.goto(base).init().wait(400))
162+
return capture(() => nightmare.goto(base).init().preloadRoutes())
159163
.then(() => {
160164
return capture(() => nightmare.click('a[href="/about"]'));
161165
})
@@ -189,7 +193,6 @@ function run(env) {
189193
return nightmare
190194
.goto(`${base}/about`)
191195
.init()
192-
.wait(200)
193196
.then(() => {
194197
return capture(() => {
195198
return nightmare
@@ -215,7 +218,7 @@ function run(env) {
215218
it('reuses prefetch promise', () => {
216219
return nightmare
217220
.goto(`${base}/blog`)
218-
.init().wait(300)
221+
.init()
219222
.then(() => {
220223
return capture(() => {
221224
return nightmare

0 commit comments

Comments
 (0)