forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigation.js
77 lines (66 loc) · 2.02 KB
/
navigation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { router, renderer } from '../client/singletons.js';
import { get_base_uri } from '../client/utils.js';
/**
* @param {string} name
*/
function guard(name) {
return () => {
throw new Error(`Cannot call ${name}(...) on the server`);
};
}
export const disableScrollHandling = import.meta.env.SSR
? guard('disableScrollHandling')
: disableScrollHandling_;
export const goto = import.meta.env.SSR ? guard('goto') : goto_;
export const invalidate = import.meta.env.SSR ? guard('invalidate') : invalidate_;
export const prefetch = import.meta.env.SSR ? guard('prefetch') : prefetch_;
export const prefetchRoutes = import.meta.env.SSR ? guard('prefetchRoutes') : prefetchRoutes_;
export const beforeNavigate = import.meta.env.SSR ? () => {} : beforeNavigate_;
export const afterNavigate = import.meta.env.SSR ? () => {} : afterNavigate_;
/**
* @type {import('$app/navigation').goto}
*/
async function disableScrollHandling_() {
renderer.disable_scroll_handling();
}
/**
* @type {import('$app/navigation').goto}
*/
async function goto_(href, opts) {
return router.goto(href, opts, []);
}
/**
* @type {import('$app/navigation').invalidate}
*/
async function invalidate_(resource) {
const { href } = new URL(resource, location.href);
return router.renderer.invalidate(href);
}
/**
* @type {import('$app/navigation').prefetch}
*/
function prefetch_(href) {
return router.prefetch(new URL(href, get_base_uri(document)));
}
/**
* @type {import('$app/navigation').prefetchRoutes}
*/
async function prefetchRoutes_(pathnames) {
const matching = pathnames
? router.routes.filter((route) => pathnames.some((pathname) => route[0].test(pathname)))
: router.routes;
const promises = matching.map((r) => Promise.all(r[1].map((load) => load())));
await Promise.all(promises);
}
/**
* @type {import('$app/navigation').beforeNavigate}
*/
function beforeNavigate_(fn) {
if (router) router.before_navigate(fn);
}
/**
* @type {import('$app/navigation').afterNavigate}
*/
function afterNavigate_(fn) {
if (router) router.after_navigate(fn);
}