forked from sveltejs/sapper
/
index.ts
27 lines (24 loc) · 826 Bytes
/
index.ts
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
import { base_url, cid, history, navigate, select_target } from '../router/index.js';
import { get_base_uri, is_hash_routing_ } from '../baseuri_helper.js'
export default function goto(
href: string,
opts: { noscroll?: boolean; replaceState?: boolean } = { noscroll: false, replaceState: false }
): Promise<void> {
if (!/\:\/\//.test(href) && href[0] !== '/') {
href = `/${href}`;
}
const url =
is_hash_routing_(location)
? new URL(`${location.origin}${base_url}#${href}`)
: new URL(href, get_base_uri(document));
const target = select_target(url);
if (target) {
const res = navigate(target, null, opts.noscroll);
history[opts.replaceState ? 'replaceState' : 'pushState']({ id: cid }, '', url.toString());
return res;
}
location.href = href;
return new Promise(() => {
/* never resolves */
});
}