mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-19 13:55:21 +00:00
Export prefetch
This commit is contained in:
@@ -164,20 +164,24 @@ let prefetching: {
|
|||||||
promise: Promise<{ Component: ComponentConstructor, data: any }>;
|
promise: Promise<{ Component: ComponentConstructor, data: any }>;
|
||||||
} = null;
|
} = null;
|
||||||
|
|
||||||
function prefetch(event: MouseEvent | TouchEvent) {
|
export function prefetch(href: string) {
|
||||||
const a: HTMLAnchorElement = <HTMLAnchorElement>findAnchor(<Node>event.target);
|
const selected = select_route(new URL(href));
|
||||||
if (!a || a.rel !== 'prefetch') return;
|
|
||||||
|
|
||||||
const selected = select_route(new URL(a.href));
|
|
||||||
|
|
||||||
if (selected) {
|
if (selected) {
|
||||||
prefetching = {
|
prefetching = {
|
||||||
href: a.href,
|
href,
|
||||||
promise: selected.route.load().then(mod => prepare_route(mod.default, selected.data))
|
promise: selected.route.load().then(mod => prepare_route(mod.default, selected.data))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handle_touchstart_mouseover(event: MouseEvent | TouchEvent) {
|
||||||
|
const a: HTMLAnchorElement = <HTMLAnchorElement>findAnchor(<Node>event.target);
|
||||||
|
if (!a || a.rel !== 'prefetch') return;
|
||||||
|
|
||||||
|
prefetch(a.href);
|
||||||
|
}
|
||||||
|
|
||||||
let inited: boolean;
|
let inited: boolean;
|
||||||
|
|
||||||
export function init(_target: Node, _routes: Route[]) {
|
export function init(_target: Node, _routes: Route[]) {
|
||||||
@@ -189,8 +193,8 @@ export function init(_target: Node, _routes: Route[]) {
|
|||||||
window.addEventListener('popstate', handle_popstate);
|
window.addEventListener('popstate', handle_popstate);
|
||||||
|
|
||||||
// prefetch
|
// prefetch
|
||||||
window.addEventListener('touchstart', prefetch);
|
window.addEventListener('touchstart', handle_touchstart_mouseover);
|
||||||
window.addEventListener('mouseover', prefetch);
|
window.addEventListener('mouseover', handle_touchstart_mouseover);
|
||||||
|
|
||||||
inited = true;
|
inited = true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user