Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 8284010

Browse files
committed
Move remaining functions and invert calls
1 parent 607dfe0 commit 8284010

File tree

5 files changed

+84
-68
lines changed

5 files changed

+84
-68
lines changed

runtime/src/app/app.ts

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { writable } from 'svelte/store';
22
import App from '@sapper/internal/App.svelte';
3-
import { Query } from '@sapper/internal/shared';
3+
import {
4+
extract_query,
5+
init,
6+
load_current_page,
7+
select_target
8+
} from './start';
49
import {
510
DOMComponentLoader,
611
DOMComponentModule,
712
ErrorComponent,
8-
ignore,
913
components,
10-
root_comp,
11-
routes
14+
root_comp
1215
} from '@sapper/internal/manifest-client';
1316
import {
1417
HydratedTarget,
@@ -70,50 +73,23 @@ export function set_target(element) {
7073
target = element;
7174
}
7275

73-
export function extract_query(search: string) {
74-
const query = Object.create(null);
75-
if (search.length > 0) {
76-
search.slice(1).split('&').forEach(searchParam => {
77-
const [, key, value = ''] = /([^=]*)(?:=(.*))?/.exec(decodeURIComponent(searchParam.replace(/\+/g, ' ')));
78-
if (typeof query[key] === 'string') query[key] = [<string>query[key]];
79-
if (typeof query[key] === 'object') (query[key] as string[]).push(value);
80-
else query[key] = value;
81-
});
82-
}
83-
return query;
84-
}
76+
export default function start(opts: {
77+
target: Node
78+
}): Promise<void> {
79+
set_target(opts.target);
8580

86-
export function select_target(url: URL): Target {
87-
if (url.origin !== location.origin) return null;
88-
if (!url.pathname.startsWith(initial_data.baseUrl)) return null;
81+
init(initial_data.baseUrl, handle_target);
8982

90-
let path = url.pathname.slice(initial_data.baseUrl.length);
91-
92-
if (path === '') {
93-
path = '/';
83+
if (initial_data.error) {
84+
return Promise.resolve().then(() => {
85+
return handle_error(new URL(location.href));
86+
});
9487
}
9588

96-
// avoid accidental clashes between server routes and page routes
97-
if (ignore.some(pattern => pattern.test(path))) return;
98-
99-
for (let i = 0; i < routes.length; i += 1) {
100-
const route = routes[i];
101-
102-
const match = route.pattern.exec(path);
103-
104-
if (match) {
105-
const query: Query = extract_query(url.search);
106-
const part = route.parts[route.parts.length - 1];
107-
const params = part.params ? part.params(match) : {};
108-
109-
const page = { host: location.host, path, query, params };
110-
111-
return { href: url.href, route, match, page };
112-
}
113-
}
89+
return load_current_page();
11490
}
11591

116-
export function handle_error(url: URL) {
92+
function handle_error(url: URL) {
11793
const { host, pathname, search } = location;
11894
const { session, preloaded, status, error } = initial_data;
11995

@@ -142,7 +118,7 @@ export function handle_error(url: URL) {
142118
render([], props, { host, path: pathname, query, params: {} });
143119
}
144120

145-
export async function handle_target(dest: Target) {
121+
async function handle_target(dest: Target): Promise<void> {
146122
if (root_component) stores.preloading.set(true);
147123

148124
const loaded = prefetching && prefetching.href === dest.href ?

runtime/src/app/goto/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { history } from '../start';
2-
import { select_target } from '../app';
3-
import { navigate, cid } from '../start';
1+
import { cid, history, navigate, select_target } from '../start';
42

53
export default function goto(
64
href: string,

runtime/src/app/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { CONTEXT_KEY } from '@sapper/internal/shared';
33

44
export const stores = () => getContext(CONTEXT_KEY);
55

6-
export { default as start } from './start/index';
6+
export { default as start } from './app';
77
export { default as goto } from './goto/index';
88
export { default as prefetch } from './prefetch/index';
99
export { default as prefetchRoutes } from './prefetchRoutes/index';

runtime/src/app/prefetch/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { select_target, prefetching, set_prefetching, hydrate_target } from '../app';
1+
import { prefetching, set_prefetching, hydrate_target } from '../app';
2+
import { select_target } from '../start';
23

34
export default function prefetch(href: string) {
45
const target = select_target(new URL(href, document.baseURI));

runtime/src/app/start/index.ts

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import {
2-
handle_target,
3-
initial_data,
4-
select_target,
5-
handle_error,
6-
set_target
7-
} from '../app';
81
import {
92
ScrollPosition,
103
Target
114
} from '../types';
125
import prefetch from '../prefetch/index';
6+
import {
7+
ignore,
8+
routes
9+
} from '@sapper/internal/manifest-client';
10+
import { Query } from '@sapper/internal/shared';
1311

1412
export let uid = 1;
1513
export function set_uid(n: number) {
@@ -30,9 +28,24 @@ export { _history as history };
3028

3129
export const scroll_history: Record<string, ScrollPosition> = {};
3230

33-
export default function start(opts: {
34-
target: Node
35-
}): Promise<void> {
31+
export function load_current_page(): Promise<void> {
32+
return Promise.resolve().then(() => {
33+
const { hash, href } = location;
34+
35+
_history.replaceState({ id: uid }, '', href);
36+
37+
const target = select_target(new URL(location.href));
38+
if (target) return navigate(target, uid, true, hash);
39+
});
40+
}
41+
42+
let base_url: string;
43+
let handle_target: (dest: Target) => Promise<void>;
44+
45+
export function init(base: string, handler: (dest: Target) => Promise<void>): void {
46+
base_url = base;
47+
handle_target = handler;
48+
3649
if ('scrollRestoration' in _history) {
3750
_history.scrollRestoration = 'manual';
3851
}
@@ -50,27 +63,55 @@ export default function start(opts: {
5063
_history.scrollRestoration = 'manual';
5164
});
5265

53-
set_target(opts.target);
54-
5566
addEventListener('click', handle_click);
5667
addEventListener('popstate', handle_popstate);
5768

5869
// prefetch
5970
addEventListener('touchstart', trigger_prefetch);
6071
addEventListener('mousemove', handle_mousemove);
72+
}
6173

62-
return Promise.resolve().then(() => {
63-
const { hash, href } = location;
74+
export function extract_query(search: string) {
75+
const query = Object.create(null);
76+
if (search.length > 0) {
77+
search.slice(1).split('&').forEach(searchParam => {
78+
const [, key, value = ''] = /([^=]*)(?:=(.*))?/.exec(decodeURIComponent(searchParam.replace(/\+/g, ' ')));
79+
if (typeof query[key] === 'string') query[key] = [<string>query[key]];
80+
if (typeof query[key] === 'object') (query[key] as string[]).push(value);
81+
else query[key] = value;
82+
});
83+
}
84+
return query;
85+
}
6486

65-
_history.replaceState({ id: uid }, '', href);
87+
export function select_target(url: URL): Target {
88+
if (url.origin !== location.origin) return null;
89+
if (!url.pathname.startsWith(base_url)) return null;
6690

67-
const url = new URL(location.href);
91+
let path = url.pathname.slice(base_url.length);
6892

69-
if (initial_data.error) return handle_error(url);
93+
if (path === '') {
94+
path = '/';
95+
}
7096

71-
const target = select_target(url);
72-
if (target) return navigate(target, uid, true, hash);
73-
});
97+
// avoid accidental clashes between server routes and page routes
98+
if (ignore.some(pattern => pattern.test(path))) return;
99+
100+
for (let i = 0; i < routes.length; i += 1) {
101+
const route = routes[i];
102+
103+
const match = route.pattern.exec(path);
104+
105+
if (match) {
106+
const query: Query = extract_query(url.search);
107+
const part = route.parts[route.parts.length - 1];
108+
const params = part.params ? part.params(match) : {};
109+
110+
const page = { host: location.host, path, query, params };
111+
112+
return { href: url.href, route, match, page };
113+
}
114+
}
74115
}
75116

76117
let mousemove_timeout: NodeJS.Timer;

0 commit comments

Comments
 (0)