mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-12 19:25:10 +00:00
add preloadRoutes function - closes #160
This commit is contained in:
@@ -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());
|
||||
}
|
||||
Reference in New Issue
Block a user