-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathrouter.js
More file actions
222 lines (184 loc) · 6.8 KB
/
router.js
File metadata and controls
222 lines (184 loc) · 6.8 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { h, createContext, cloneElement, toChildArray } from 'preact';
import { useContext, useMemo, useReducer, useLayoutEffect, useRef } from 'preact/hooks';
let push;
const UPDATE = (state, url) => {
push = undefined;
if (url && url.type === 'click') {
// ignore events the browser takes care of already:
if (url.ctrlKey || url.metaKey || url.altKey || url.shiftKey || url.button !== 0) {
return state;
}
const link = url.target.closest('a[href]');
if (
!link ||
link.origin != location.origin ||
/^#/.test(link.getAttribute('href')) ||
!/^(_?self)?$/i.test(link.target)
) {
return state;
}
push = true;
url.preventDefault();
url = link.href.replace(location.origin, '');
} else if (typeof url === 'string') {
push = true;
} else {
url = location.pathname + location.search;
}
if (push === true) history.pushState(null, '', url);
else if (push === false) history.replaceState(null, '', url);
return url;
};
export const exec = (url, route, matches) => {
url = url.split('/').filter(Boolean);
route = (route || '').split('/').filter(Boolean);
for (let i = 0, val, rest; i < Math.max(url.length, route.length); i++) {
let [, m, param, flag] = (route[i] || '').match(/^(:?)(.*?)([+*?]?)$/);
val = url[i];
// segment match:
if (!m && param == val) continue;
// /foo/* match
if (!m && val && flag == '*') {
matches.rest = '/' + url.slice(i).map(decodeURIComponent).join('/');
break;
}
// segment mismatch / missing required field:
if (!m || (!val && flag != '?' && flag != '*')) return;
rest = flag == '+' || flag == '*';
// rest (+/*) match:
if (rest) val = url.slice(i).map(decodeURIComponent).join('/');
// normal/optional field:
else if (val) val = decodeURIComponent(val);
matches.params[param] = val;
if (!(param in matches)) matches[param] = val;
if (rest) break;
}
return matches;
};
export function LocationProvider(props) {
const [url, route] = useReducer(UPDATE, props.url || location.pathname + location.search);
const wasPush = push === true;
const value = useMemo(() => {
const u = new URL(url, location.origin);
const path = u.pathname.replace(/(.)\/$/g, '$1');
// @ts-ignore-next
return { url, path, query: Object.fromEntries(u.searchParams), route, wasPush };
}, [url]);
useLayoutEffect(() => {
addEventListener('click', route);
addEventListener('popstate', route);
return () => {
removeEventListener('click', route);
removeEventListener('popstate', route);
};
}, []);
// @ts-ignore
return h(LocationProvider.ctx.Provider, { value }, props.children);
}
const RESOLVED = Promise.resolve();
export function Router(props) {
const [c, update] = useReducer(c => c + 1, 0);
const { url, query, wasPush, path } = useLocation();
const { rest = path, params = {} } = useContext(RouteContext);
const isLoading = useRef(false);
const prevRoute = useRef(path);
// Monotonic counter used to check if an un-suspending route is still the current route:
const count = useRef(0);
// The current route:
const cur = useRef();
// Previous route (if current route is suspended):
const prev = useRef();
// A not-yet-hydrated DOM root to remove once we commit:
const pendingBase = useRef();
// has this component ever successfully rendered without suspending:
const hasEverCommitted = useRef(false);
// was the most recent render successful (did not suspend):
const didSuspend = useRef();
didSuspend.current = false;
// current return value of onBeforeRoute() prop
const onBeforeRoute = useRef();
cur.current = useMemo(() => {
// This hack prevents Preact from diffing when we swap `cur` to `prev`:
if (this.__v && this.__v.__k) this.__v.__k.reverse();
count.current++;
prev.current = cur.current;
let obr = props.onBeforeRoute && props.onBeforeRoute(url);
if (obr && obr.then) {
obr = obr.then(() => {
if (onBeforeRoute.current === obr) {
onBeforeRoute.current = null;
}
});
}
onBeforeRoute.current = obr;
let p, d, m;
toChildArray(props.children).some(vnode => {
const matches = exec(rest, vnode.props.path, (m = { path: rest, query, params, rest: '' }));
if (matches) return (p = cloneElement(vnode, m));
if (vnode.props.default) d = cloneElement(vnode, m);
});
return h(RouteContext.Provider, { value: m }, p || d);
}, [url]);
// Reset previous children - if rendering succeeds synchronously, we shouldn't render the previous children.
const p = prev.current;
prev.current = null;
// This borrows the _childDidSuspend() solution from compat.
this.__c = e => {
// Mark the current render as having suspended:
didSuspend.current = true;
// The new route suspended, so keep the previous route around while it loads:
prev.current = p;
// Fire an event saying we're waiting for the route:
if (props.onLoadStart) props.onLoadStart(url);
isLoading.current = true;
// Re-render on unsuspend:
let c = count.current;
e.then(() => {
// Ignore this update if it isn't the most recently suspended update:
if (c !== count.current) return;
// Successful route transition: un-suspend after a tick and stop rendering the old route:
prev.current = null;
RESOLVED.then(update);
});
};
useLayoutEffect(() => {
const currentDom = this.__v && this.__v.__e;
// Ignore suspended renders (failed commits):
if (didSuspend.current) {
// If we've never committed, mark any hydration DOM for removal on the next commit:
if (!hasEverCommitted.current && !pendingBase.current) {
pendingBase.current = currentDom;
}
return;
}
// If this is the first ever successful commit and we didn't use the hydration DOM, remove it:
if (!hasEverCommitted.current && pendingBase.current) {
if (pendingBase.current !== currentDom) pendingBase.current.remove();
pendingBase.current = null;
}
// Mark the component has having committed:
hasEverCommitted.current = true;
// The route is loaded and rendered.
if (prevRoute.current !== path) {
if (wasPush) scrollTo(0, 0);
if (props.onLoadEnd && isLoading.current) props.onLoadEnd(url);
if (props.onRouteChange) props.onRouteChange(url);
isLoading.current = false;
prevRoute.current = path;
}
}, [path, wasPush, c]);
// Note: curChildren MUST render first in order to set didSuspend & prev.
return [h(RenderRef, { p: onBeforeRoute.current, r: cur }), h(RenderRef, { r: prev })];
}
// Lazily render a ref's current value:
const RenderRef = ({ p, r }) => {
if (p && p.then) throw p;
return r.current;
};
Router.Provider = LocationProvider;
/** @typedef {{ url: string, path: string, query: object, route, wasPush: boolean }} RouteInfo */
LocationProvider.ctx = createContext(/** @type {RouteInfo} */ ({}));
const RouteContext = createContext({});
export const Route = props => h(props.component, props);
export const useLocation = () => useContext(LocationProvider.ctx);
export const useRoute = () => useContext(RouteContext);