-
Notifications
You must be signed in to change notification settings - Fork 0
/
routerino.jsx
418 lines (373 loc) · 13.3 KB
/
routerino.jsx
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import { cloneElement, useEffect, useState } from "react";
import PropTypes from "prop-types";
/**
* update a head tag
*
* @typedef {Object} HeadTag
* @property {string} [tag] - The tag name to update (default: meta)
* @property {boolean} [soft] - Whether to skip the update of an existing tag if already exists (default: false)
* @property {string} [name] - The name attribute of the tag
* @property {string} [property] - The property attribute of the tag
* @property {string} [content] - The content attribute of the tag
* @property {string} [charset] - The charset attribute of the tag
* @property {string} [httpEquiv] - The http-equiv attribute of the tag
* @property {string} [itemProp] - The itemProp attribute of the tag
* @property {string} [rel] - The rel attribute of the tag
* @property {string} [href] - The href attribute of the tag
* @property {string} [src] - The src attribute of the tag
* @property {string} [sizes] - The sizes attribute of the tag
* @property {string} [type] - The type attribute of the tag
* @property {string} [media] - The media attribute of the tag
* @property {string} [hrefLang] - The hrefLang attribute of the tag
* @property {string} [target] - The target attribute of the tag
*
*/
function updateHeadTag({ tag = "meta", soft = false, ...attrs }) {
// first, get an array of the attribute names
const attrKeys = Object.keys(attrs);
// do a quick input check
if (attrKeys.length < 1) {
return console.error(
`updateHeadTag() received no attributes to set for ${tag} tag`
);
}
// tag search/instantiate (3 steps)
// we want to use existing tag if available (to prevent duplicate tags), or create it otherwise.
// ------------------------------------------
// 1. instantiate the variable here
let tagToUpdate = null;
// 2. iterate to find the first match (while excluding the content attribute)
for (let i = 0; i < attrKeys.length; i++) {
if (attrKeys[i] !== "content") {
tagToUpdate = document.querySelector(
`${tag}[${attrKeys[i]}='${attrs[attrKeys[i]]}']`
);
}
if (tagToUpdate) break;
}
// 3. if no matching tag is found, create a new one
// make sure we don't want a "soft" update (doesn't overwrite the value if set)
if (!tagToUpdate && !soft) {
tagToUpdate = document.createElement(tag);
}
// next, set the tag attributes
attrKeys.forEach((attr) => tagToUpdate.setAttribute(attr, attrs[attr]));
// finally, append the tag
document.querySelector("head").appendChild(tagToUpdate);
}
function extractParams({ routePattern, currentRoute }) {
// For simplicity, we just split by '/' and match :paramName
let params = {};
let routeSegments = routePattern.split("/");
let currentRouteSegments = currentRoute.split("/");
routeSegments.forEach((segment, index) => {
if (segment.startsWith(":")) {
params[segment.slice(1)] = currentRouteSegments[index];
}
});
return params;
}
// Routerino Component
export default function Routerino({
routes = [
{
path: "/",
element: (
<p>
This is the default route. Pass an array of routes to the Routerino
component in order to configure your own pages. Each route is a
dictionary with at least `path` and `element` defined.
</p>
),
title: "Routerino default route example",
description: "The default route example description.",
tags: [{ property: "og:locale", content: "en_US" }],
},
],
notFoundTemplate = (
<>
<p>No page found for this URL. [404]</p>
<p>
<a href="/">Home</a>
</p>
</>
),
notFoundTitle = "Page not found [404]",
errorTemplate = (
<>
<p>Page failed to load. [500]</p>
<p>
<a href="/">Home</a>
</p>
</>
),
errorTitle = "Page error [500]",
useTrailingSlash = true,
usePrerenderTags = true,
title = "",
separator = " | ",
titlePrefix = "",
titlePostfix = "",
imageUrl = null,
touchIconUrl = null,
debug = false,
}) {
try {
// we use this state to track the URL internally and control React updates
const [href, setHref] = useState(window.location.href);
// this useEffect manages reload-free page transitions via
// the browser's history.pushState and window.scrollTo APIs
useEffect(() => {
const handleClick = (event) => {
if (debug) {
console.debug("click occurred");
}
let target = event.target;
while (target.tagName !== "A" && target.parentElement) {
target = target.parentElement;
}
if (target.tagName !== "A") {
// no anchor tag, stop checking anything
if (debug) {
console.debug("no achor tag found during click");
}
return;
}
if (debug) {
console.debug(`click target ${target}`);
}
let targetUrl = new URL(target);
if (debug) {
console.debug(`targetUrl: ${targetUrl}, current: ${window.location}`);
}
// check for links to be updated without reloading (same origin)
if (window.location.origin === targetUrl.origin) {
if (debug) {
console.debug(
"target link is same origin, push-state transitioning"
);
}
event.preventDefault();
// update the history (for new locations)
if (target.href !== window.location.href) {
setHref(target.href);
window.history.pushState({}, "", target.href);
}
// replicate a browser reload (by scrolling to top)
window.scrollTo({
top: 0,
behavior: "auto",
});
} else if (debug) {
console.debug(
"target link does not share an origin, standard link handling applies"
);
}
};
document.addEventListener("click", handleClick);
// handle browser back button state changes
const handlePopState = () => {
setHref(window.location.href);
};
window.addEventListener("popstate", handlePopState);
// clean up the event listeners
return () => {
document.removeEventListener("click", handleClick);
window.removeEventListener("popstate", handlePopState);
};
}, [href]);
// START LOCATING MATCH
let currentRoute = window.location?.pathname ?? "/";
// use the root route for index.html requests
if (currentRoute === "/index.html" || currentRoute === "")
currentRoute = "/";
// console.debug({ msg: "router called", currentRoute });
// locate the route if it matches exactly
const exactMatch = routes.find((route) => route.path === currentRoute);
// locate the route if either part is missing a trailing slash
const addSlashMatch = routes.find(
(route) =>
`${route.path}/` === currentRoute || route.path === `${currentRoute}/`
);
// locate the route if using params matching
const paramsMatch = routes.find((route) => {
// Normalize the paths by removing trailing slashes
const normalizedRoutePath = route.path.endsWith("/")
? route.path.slice(0, -1)
: route.path;
const normalizedCurrentRoute = currentRoute.endsWith("/")
? currentRoute.slice(0, -1)
: currentRoute;
// Split the paths into segments
const routeSegments = normalizedRoutePath.split("/").filter(Boolean);
const currentRouteSegments = normalizedCurrentRoute
.split("/")
.filter(Boolean);
// Check if segments length match
if (routeSegments.length !== currentRouteSegments.length) {
return false;
}
// Compare each segment
return routeSegments.every((segment, index) => {
// If the segment starts with ':', it's a parameter, so it should match anything
if (segment.startsWith(":")) {
return true;
}
// If it's not a parameter, it should match the segment in the current route exactly
return segment === currentRouteSegments[index];
});
});
const match = exactMatch ?? addSlashMatch ?? paramsMatch;
if (debug) {
console.debug({ match, exactMatch, addSlashMatch, paramsMatch });
}
// START 404 HANDLING
if (!match) {
console.error(`No matching route found for ${currentRoute}`);
document.title = `${titlePrefix}${notFoundTitle}${
titlePostfix ?? `${separator}${title}`
}`;
if (usePrerenderTags) {
updateHeadTag({ name: "prerender-status-code", content: "404" });
}
return notFoundTemplate;
}
// START MATCH HANDLING
// set the title, og:title
if (match.title) {
// calculate the title
const fullTitle = `${match.titlePrefix ?? titlePrefix}${match.title}${
match.titlePostfix ?? titlePostfix ?? `${separator}${title}`
}`;
// set the doc title
document.title = fullTitle;
// create the og:title IFF user didn't supply their own
if (!match.tags?.find(({ property }) => property === "og:title")) {
updateHeadTag({
property: "og:title",
content: fullTitle,
});
}
}
// set the description
if (match.description) {
updateHeadTag({ name: "description", content: match.description });
// create the og:description IFF user didn't supply their own
if (!match.tags?.find(({ property }) => property === "og:description")) {
updateHeadTag({
property: "og:description",
content: match.description,
});
}
}
// set the og:image
if (Boolean(imageUrl) || Boolean(match.imageUrl)) {
// set the og:image tag
updateHeadTag({
property: "og:image",
content: match.imageUrl ?? imageUrl,
});
}
if (touchIconUrl) {
updateHeadTag({
tag: "link",
rel: "apple-touch-icon",
href: touchIconUrl,
});
}
// check if we need to provide prerender redirects (while skipping redirects for the root route: `/` because that makes no sense)
if (usePrerenderTags && currentRoute !== "/") {
// check if we need to redirect to a trailing slash
if (useTrailingSlash && !currentRoute.endsWith("/")) {
updateHeadTag({ name: "prerender-status-code", content: "301" });
updateHeadTag({
name: "prerender-header",
content: `Location: ${window.location.href}/`,
});
// check if we need to redirect to a non-trailing slash
} else if (!useTrailingSlash && currentRoute.endsWith("/")) {
updateHeadTag({ name: "prerender-status-code", content: "301" });
updateHeadTag({
name: "prerender-header",
content: `Location: ${window.location.href.slice(0, -1)}`,
});
}
}
// add any supplied meta tags (& default the og:type to website)
if (Boolean(match.tags) && Boolean(match.tags.length)) {
if (!match.tags.find(({ property }) => property === "og:type")) {
updateHeadTag({ property: "og:type", content: "website" });
}
match.tags.forEach((tag) => updateHeadTag(tag));
} else {
updateHeadTag({ property: "og:type", content: "website" });
}
// check & return the element to render
if (match.element) {
// we need to extract the path parameters to give to components that need them
const params = extractParams({
routePattern: match.path,
currentRoute,
});
const routerinoProps = {
currentRoute,
params,
routePattern: match.path,
updateHeadTag,
};
// add the route params to the React component
// nb: cloneElement won't re-trigger componentDidMount lifecycle
const elementWithProps = cloneElement(match.element, {
// we allow access via both uppercase and lowercase
routerino: routerinoProps,
Routerino: routerinoProps,
});
return elementWithProps;
}
// no search result
console.error(`No route found for ${currentRoute}`);
document.title = `${titlePrefix}${notFoundTitle}${
titlePostfix ?? `${separator}${title}`
}`;
if (usePrerenderTags) {
updateHeadTag({ name: "prerender-status-code", content: "404" });
}
return notFoundTemplate;
} catch (e) {
// router threw up (ಥ﹏ಥ)
console.error(`Routerino error: ${e}`);
if (usePrerenderTags) {
updateHeadTag({ name: "prerender-status-code", content: "500" });
}
document.title = `${titlePrefix}${errorTitle}${
titlePostfix ?? `${separator}${title}`
}`;
return errorTemplate;
}
}
const RouteProps = PropTypes.exact({
path: PropTypes.string.isRequired,
element: PropTypes.element.isRequired,
title: PropTypes.string,
description: PropTypes.string,
tags: PropTypes.arrayOf(PropTypes.object),
titlePrefix: PropTypes.string,
titlePostfix: PropTypes.string,
imageUrl: PropTypes.string,
});
Routerino.propTypes = {
routes: PropTypes.arrayOf(RouteProps),
title: PropTypes.string,
separator: PropTypes.string,
notFoundTemplate: PropTypes.element,
notFoundTitle: PropTypes.string,
errorTemplate: PropTypes.element,
errorTitle: PropTypes.string,
useTrailingSlash: PropTypes.bool,
usePrerenderTags: PropTypes.bool,
titlePrefix: PropTypes.string,
titlePostfix: PropTypes.string,
imageUrl: PropTypes.string,
touchIconUrl: PropTypes.string,
debug: PropTypes.bool,
};