Merge branch 'master' into gh-103

This commit is contained in:
Rich Harris
2018-03-04 19:00:28 -05:00
4 changed files with 29 additions and 9 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,20 @@ 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 => {
if (!pathnames) return true;
return pathnames.some(pathname => {
return route.error
? route.error === pathname
: route.pattern.test(pathname)
});
})
.reduce((promise: Promise<any>, route) => {
return promise.then(route.load);
}, Promise.resolve());
}

View File

@@ -13,8 +13,9 @@ export interface Component {
export type Route = {
pattern: RegExp;
params: (match: RegExpExecArray) => Record<string, string>;
load: () => Promise<{ default: ComponentConstructor }>;
error?: string;
params?: (match: RegExpExecArray) => Record<string, string>;
ignore?: boolean;
};