add preloadRoutes function - closes #160

This commit is contained in:
Rich Harris
2018-03-04 18:05:33 -05:00
parent acef0e808f
commit 7e2f5f8fb6
3 changed files with 22 additions and 8 deletions

View File

@@ -59,9 +59,6 @@ function render(Component: ComponentConstructor, data: any, scroll: ScrollPositi
detach(start);
detach(end);
}
// preload additional routes
routes.reduce((promise: Promise<any>, route) => promise.then(route.load), Promise.resolve());
}
component = new Component({
@@ -268,3 +265,15 @@ export function goto(href: string, opts = { replaceState: false }) {
window.location.href = href;
}
}
export function preloadRoutes(pathnames: string[]) {
if (!routes) throw new Error(`You must call init() first`);
return routes
.filter(route => {
return !pathnames || pathnames.some(pathname => route.pattern.test(pathname));
})
.reduce((promise: Promise<any>, route) => {
return promise.then(route.load);
}, Promise.resolve());
}