This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
router.js
401 lines (337 loc) · 10.3 KB
/
router.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import React from 'react';
import isNode from './isNode';
import {setQueryParams} from './queryParams';
import {interceptRoute} from './interceptor';
let preparedRoutes = {};
let stack = {};
let componentId = 1;
let currentPath = isNode ? '' : location.pathname;
let basePath = '';
let basePathRegEx = null;
const pathUpdaters = [];
/**
* Will define a base path that will be utilized in your routing and navigation.
* To be called _before_ any routing or navigation happens.
* @param {string} inBasepath
*/
export const setBasepath = (inBasepath) => {
basePath = inBasepath;
basePathRegEx = new RegExp('^' + basePath);
};
/**
* Returns the currently used base path.
* @returns {string}
*/
export const getBasepath = () => basePath;
const resolvePath = (inPath) => {
if (isNode) {
const url = require('url');
return url.resolve(currentPath, inPath);
}
const current = new URL(currentPath, location.href);
const resolved = new URL(inPath, current);
return resolved.pathname;
};
export const ParentContext = React.createContext(null);
/**
* Pass a route string to this function to receive a regular expression.
* The transformation will be cached and if you pass the same route a second
* time, the cached regex will be returned.
* @param {string} inRoute
* @returns {Array} [RegExp, propList]
*/
const prepareRoute = (inRoute) => {
if (preparedRoutes[inRoute]) {
return preparedRoutes[inRoute];
}
const preparedRoute = [
new RegExp(`${inRoute.substr(0, 1) === '*' ? '' : '^'}${inRoute.replace(/:[a-zA-Z]+/g, '([^/]+)').replace(/\*/g, '')}${inRoute.substr(-1,) === '*' ? '' : '$'}`)
];
const propList = inRoute.match(/:[a-zA-Z]+/g);
preparedRoute.push(
propList
? propList.map(paramName => paramName.substr(1))
: []
);
preparedRoutes[inRoute] = preparedRoute;
return preparedRoute;
};
/**
* Virtually navigates the browser to the given URL and re-processes all routers.
* @param {string} url The URL to navigate to. Do not mix adding GET params here and using the `getParams` argument.
* @param {boolean} [replace=false] Should the navigation be done with a history replace to prevent back navigation by the user
* @param {object} [queryParams] Key/Value pairs to convert into get parameters to be appended to the URL.
* @param {boolean} [replaceQueryParams=true] Should existing query parameters be carried over, or dropped (replaced)?
*/
export const navigate = (url, replace = false, queryParams = null, replaceQueryParams = true) => {
url = interceptRoute(currentPath, resolvePath(url));
if (!url || url === currentPath) {
return;
}
currentPath = url;
if (isNode) {
setPath(url);
processStack();
updatePathHooks();
return;
}
const finalURL = basePathRegEx
? url.match(basePathRegEx)
? url
: basePath + url
:
url;
window.history[`${replace ? 'replace' : 'push'}State`](null, null, finalURL);
processStack();
updatePathHooks();
if (queryParams) {
setQueryParams(queryParams, replaceQueryParams);
}
};
let customPath = '/';
/**
* Enables you to manually set the path from outside in a nodeJS environment, where window.history is not available.
* @param {string} inPath
*/
export const setPath = (inPath) => {
const url = require('url');
customPath = url.resolve(customPath, inPath);
};
/**
* Returns the current path of the router.
* @returns {string}
*/
export const getPath = () => customPath;
/**
* This hook returns the currently used URI.
* Works in a browser context as well as for SSR.
*
* _Heads up:_ This will make your component render on every navigation unless you set this hook to passive!
* @param {boolean} [active=true] Will update the component upon path changes. Set to false to only retrieve the path, once.
* @param {boolean} [withBasepath=false] Should the base path be left at the beginning of the URI?
* @returns {string}
*/
export const usePath = (active = true, withBasepath = false) => {
const [, setUpdate] = React.useState(0);
React.useEffect(() => {
if (!active) {
return;
}
pathUpdaters.push(setUpdate);
return () => {
const index = pathUpdaters.indexOf(setUpdate);
if (index !== -1) {
pathUpdaters.splice(index, 1);
}
};
}, [setUpdate]);
return withBasepath ? currentPath : currentPath.replace(basePathRegEx, '');
};
/**
* Render all components that use path hooks.
*/
const updatePathHooks = () => {
const now = Date.now();
pathUpdaters.forEach(cb => cb(now));
};
/**
* Called from within the router. This returns either the current windows url path
* or a already reduced path, if a parent router has already matched with a finishing
* wildcard before.
* @param {string} [parentRouterId]
* @returns {string}
*/
export const getWorkingPath = (parentRouterId) => {
if (!parentRouterId) {
return isNode ? customPath : window.location.pathname.replace(basePathRegEx, '') || '/';
}
const stackEntry = stack[parentRouterId];
if (!stackEntry) {
throw 'wth';
}
return stackEntry.reducedPath !== null ? stackEntry.reducedPath || '/' : window.location.pathname;
};
const processStack = () => Object.values(stack).forEach(process);
/**
* This function takes two objects and compares if they have the same
* keys and their keys have the same values assigned, so the objects are
* basically the same.
* @param {object} objA
* @param {object} objB
* @return {boolean}
*/
const objectsEqual = (objA, objB) => {
const objAKeys = Object.keys(objA);
const objBKeys = Object.keys(objB);
const valueIsEqual = key => objB.hasOwnProperty(key) && objA[key] === objB[key];
return (
objAKeys.length === objBKeys.length
&& objAKeys.every(valueIsEqual)
);
};
if (!isNode) {
window.addEventListener('popstate', (e) => {
const nextPath = interceptRoute(currentPath, location.pathname);
if (!nextPath || nextPath === currentPath) {
e.preventDefault();
e.stopPropagation();
history.pushState(null, null, currentPath);
return;
}
currentPath = nextPath;
if (nextPath !== location.pathname) {
history.replaceState(null, null, nextPath);
}
processStack();
updatePathHooks();
});
}
const emptyFunc = () => null;
/**
* This will calculate the match of a given router.
* @param {object} stackObj
* @param {boolean} [directCall] If its not a direct call, the process function might trigger a component render.
*/
const process = (stackObj, directCall) => {
const {
routerId,
parentRouterId,
routes,
setUpdate,
resultFunc,
resultProps,
reducedPath: previousReducedPath
} = stackObj;
const currentPath = getWorkingPath(parentRouterId);
let route = null;
let targetFunction = null;
let targetProps = null;
let reducedPath = null;
let anyMatched = false;
for (let i = 0; i < routes.length; i++) {
[route, targetFunction] = routes[i];
const [regex, groupNames] = preparedRoutes[route]
? preparedRoutes[route]
: prepareRoute(route);
const result = currentPath.match(regex);
if (!result) {
targetFunction = emptyFunc;
continue;
}
if (groupNames.length) {
targetProps = {};
for (let j = 0; j < groupNames.length; j++) {
targetProps[groupNames[j]] = result[j + 1];
}
}
reducedPath = currentPath.replace(result[0], '');
anyMatched = true;
break;
}
if (!stack[routerId]) {
return;
}
if (!anyMatched) {
route = null;
targetFunction = null;
targetProps = null;
reducedPath = null;
}
const funcsDiffer = resultFunc !== targetFunction;
const pathDiffer = reducedPath !== previousReducedPath;
let propsDiffer = true;
if (!funcsDiffer) {
if (!resultProps && !targetProps) {
propsDiffer = false;
} else {
propsDiffer = !(resultProps && targetProps && objectsEqual(resultProps, targetProps) === true);
}
if (!propsDiffer) {
if (!pathDiffer) {
return;
}
}
}
const result = funcsDiffer || propsDiffer
? targetFunction
? targetFunction(targetProps)
: null
: stackObj.result;
Object.assign(stack[routerId], {
result,
reducedPath,
matchedRoute: route,
passContext: route ? route.substr(-1) === '*' : false
});
if (!directCall && (funcsDiffer || propsDiffer || route === null)) {
setUpdate(Date.now());
}
};
/**
* If a route returns a function, instead of a react element, we need to wrap this function
* to eventually wrap a context object around its result.
* @param RouteContext
* @param originalResult
* @returns {function(): *}
*/
const wrapperFunction = (RouteContext, originalResult) => function (){
return (
<RouteContext>{originalResult.apply(originalResult, arguments)}</RouteContext>
);
};
/**
* Pass an object to this function where the keys are routes and the values
* are functions to be executed when a route matches. Whatever your function returns
* will be returned from the hook as well into your react component. Ideally you would
* return components to be rendered when certain routes match, but you are not limited
* to that.
* @param {object} routeObj {"/someRoute": () => <Example />}
*/
export const useRoutes = (routeObj) => {
// Each router gets an internal id to look them up again.
const [routerId] = React.useState(componentId);
const setUpdate = React.useState(0)[1];
// Needed to create nested routers which use only a subset of the URL.
const parentRouterId = React.useContext(ParentContext);
// If we just took the last ID, increase it for the next hook.
if (routerId === componentId) {
componentId += 1;
}
// Removes the router from the stack after component unmount - it won't be processed anymore.
React.useEffect(() => () => delete stack[routerId], [routerId]);
let stackObj = stack[routerId];
if (stackObj && stackObj.originalRouteObj !== routeObj) {
stackObj = null;
}
if (!stackObj) {
stackObj = {
routerId,
originalRouteObj: routeObj,
routes: Object.entries(routeObj),
setUpdate,
parentRouterId,
matchedRoute: null,
reducedPath: null,
passContext: false,
result: null
};
stack[routerId] = stackObj;
process(stackObj, true);
}
React.useDebugValue(stackObj.matchedRoute);
if (!stackObj.matchedRoute) {
return null;
}
let result = stackObj.result;
if (!stackObj.passContext) {
return result;
} else {
const RouteContext = ({children}) => <ParentContext.Provider value={routerId}>{children}</ParentContext.Provider>;
if (typeof result === 'function') {
return wrapperFunction(RouteContext, result);
}
return React.isValidElement(result) && result.type !== RouteContext
? <RouteContext>{result}</RouteContext>
: result;
}
};