1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | import { createContext, useRef, useState, useLayoutEffect, createElement, useContext, useEffect, useMemo, useCallback, Children, isValidElement, Fragment } from 'react';
|
12 | import { createMemoryHistory, Action, parsePath } from 'history';
|
13 |
|
14 | function invariant(cond, message) {
|
15 | if (!cond) throw new Error(message);
|
16 | }
|
17 |
|
18 | function warning(cond, message) {
|
19 | if (!cond) {
|
20 |
|
21 | if (typeof console !== "undefined") console.warn(message);
|
22 |
|
23 | try {
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | throw new Error(message);
|
30 | } catch (e) {}
|
31 | }
|
32 | }
|
33 |
|
34 | const alreadyWarned = {};
|
35 |
|
36 | function warningOnce(key, cond, message) {
|
37 | if (!cond && !alreadyWarned[key]) {
|
38 | alreadyWarned[key] = true;
|
39 | process.env.NODE_ENV !== "production" ? warning(false, message) : void 0;
|
40 | }
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | const NavigationContext = createContext(null);
|
57 |
|
58 | if (process.env.NODE_ENV !== "production") {
|
59 | NavigationContext.displayName = "Navigation";
|
60 | }
|
61 |
|
62 | const LocationContext = createContext(null);
|
63 |
|
64 | if (process.env.NODE_ENV !== "production") {
|
65 | LocationContext.displayName = "Location";
|
66 | }
|
67 |
|
68 | const RouteContext = createContext({
|
69 | outlet: null,
|
70 | matches: []
|
71 | });
|
72 |
|
73 | if (process.env.NODE_ENV !== "production") {
|
74 | RouteContext.displayName = "Route";
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | function MemoryRouter(_ref) {
|
86 | let {
|
87 | basename,
|
88 | children,
|
89 | initialEntries,
|
90 | initialIndex
|
91 | } = _ref;
|
92 | let historyRef = useRef();
|
93 |
|
94 | if (historyRef.current == null) {
|
95 | historyRef.current = createMemoryHistory({
|
96 | initialEntries,
|
97 | initialIndex
|
98 | });
|
99 | }
|
100 |
|
101 | let history = historyRef.current;
|
102 | let [state, setState] = useState({
|
103 | action: history.action,
|
104 | location: history.location
|
105 | });
|
106 | useLayoutEffect(() => history.listen(setState), [history]);
|
107 | return createElement(Router, {
|
108 | basename: basename,
|
109 | children: children,
|
110 | location: state.location,
|
111 | navigationType: state.action,
|
112 | navigator: history
|
113 | });
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | function Navigate(_ref2) {
|
126 | let {
|
127 | to,
|
128 | replace,
|
129 | state
|
130 | } = _ref2;
|
131 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
132 |
|
133 | "<Navigate> may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
134 | process.env.NODE_ENV !== "production" ? warning(!useContext(NavigationContext).static, "<Navigate> must not be used on the initial render in a <StaticRouter>. " + "This is a no-op, but you should modify your code so the <Navigate> is " + "only ever rendered in response to some user interaction or state change.") : void 0;
|
135 | let navigate = useNavigate();
|
136 | useEffect(() => {
|
137 | navigate(to, {
|
138 | replace,
|
139 | state
|
140 | });
|
141 | });
|
142 | return null;
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | function Outlet(props) {
|
151 | return useOutlet(props.context);
|
152 | }
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | function Route(_props) {
|
160 | process.env.NODE_ENV !== "production" ? invariant(false, "A <Route> is only ever to be used as the child of <Routes> element, " + "never rendered directly. Please wrap your <Route> in a <Routes>.") : invariant(false) ;
|
161 | }
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | function Router(_ref3) {
|
173 | let {
|
174 | basename: basenameProp = "/",
|
175 | children = null,
|
176 | location: locationProp,
|
177 | navigationType = Action.Pop,
|
178 | navigator,
|
179 | static: staticProp = false
|
180 | } = _ref3;
|
181 | !!useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false, "You cannot render a <Router> inside another <Router>." + " You should never have more than one in your app.") : invariant(false) : void 0;
|
182 | let basename = normalizePathname(basenameProp);
|
183 | let navigationContext = useMemo(() => ({
|
184 | basename,
|
185 | navigator,
|
186 | static: staticProp
|
187 | }), [basename, navigator, staticProp]);
|
188 |
|
189 | if (typeof locationProp === "string") {
|
190 | locationProp = parsePath(locationProp);
|
191 | }
|
192 |
|
193 | let {
|
194 | pathname = "/",
|
195 | search = "",
|
196 | hash = "",
|
197 | state = null,
|
198 | key = "default"
|
199 | } = locationProp;
|
200 | let location = useMemo(() => {
|
201 | let trailingPathname = stripBasename(pathname, basename);
|
202 |
|
203 | if (trailingPathname == null) {
|
204 | return null;
|
205 | }
|
206 |
|
207 | return {
|
208 | pathname: trailingPathname,
|
209 | search,
|
210 | hash,
|
211 | state,
|
212 | key
|
213 | };
|
214 | }, [basename, pathname, search, hash, state, key]);
|
215 | process.env.NODE_ENV !== "production" ? warning(location != null, "<Router basename=\"" + basename + "\"> is not able to match the URL " + ("\"" + pathname + search + hash + "\" because it does not start with the ") + "basename, so the <Router> won't render anything.") : void 0;
|
216 |
|
217 | if (location == null) {
|
218 | return null;
|
219 | }
|
220 |
|
221 | return createElement(NavigationContext.Provider, {
|
222 | value: navigationContext
|
223 | }, createElement(LocationContext.Provider, {
|
224 | children: children,
|
225 | value: {
|
226 | location,
|
227 | navigationType
|
228 | }
|
229 | }));
|
230 | }
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | function Routes(_ref4) {
|
239 | let {
|
240 | children,
|
241 | location
|
242 | } = _ref4;
|
243 | return useRoutes(createRoutesFromChildren(children), location);
|
244 | }
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | function useHref(to) {
|
256 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
257 |
|
258 | "useHref() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
259 | let {
|
260 | basename,
|
261 | navigator
|
262 | } = useContext(NavigationContext);
|
263 | let {
|
264 | hash,
|
265 | pathname,
|
266 | search
|
267 | } = useResolvedPath(to);
|
268 | let joinedPathname = pathname;
|
269 |
|
270 | if (basename !== "/") {
|
271 | let toPathname = getToPathname(to);
|
272 | let endsWithSlash = toPathname != null && toPathname.endsWith("/");
|
273 | joinedPathname = pathname === "/" ? basename + (endsWithSlash ? "/" : "") : joinPaths([basename, pathname]);
|
274 | }
|
275 |
|
276 | return navigator.createHref({
|
277 | pathname: joinedPathname,
|
278 | search,
|
279 | hash
|
280 | });
|
281 | }
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | function useInRouterContext() {
|
289 | return useContext(LocationContext) != null;
|
290 | }
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | function useLocation() {
|
303 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
304 |
|
305 | "useLocation() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
306 | return useContext(LocationContext).location;
|
307 | }
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 | function useNavigationType() {
|
316 | return useContext(LocationContext).navigationType;
|
317 | }
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | function useMatch(pattern) {
|
327 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
328 |
|
329 | "useMatch() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
330 | let {
|
331 | pathname
|
332 | } = useLocation();
|
333 | return useMemo(() => matchPath(pattern, pathname), [pathname, pattern]);
|
334 | }
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 | function useNavigate() {
|
346 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
347 |
|
348 | "useNavigate() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
349 | let {
|
350 | basename,
|
351 | navigator
|
352 | } = useContext(NavigationContext);
|
353 | let {
|
354 | matches
|
355 | } = useContext(RouteContext);
|
356 | let {
|
357 | pathname: locationPathname
|
358 | } = useLocation();
|
359 | let routePathnamesJson = JSON.stringify(matches.map(match => match.pathnameBase));
|
360 | let activeRef = useRef(false);
|
361 | useEffect(() => {
|
362 | activeRef.current = true;
|
363 | });
|
364 | let navigate = useCallback(function (to, options) {
|
365 | if (options === void 0) {
|
366 | options = {};
|
367 | }
|
368 |
|
369 | process.env.NODE_ENV !== "production" ? warning(activeRef.current, "You should call navigate() in a React.useEffect(), not when " + "your component is first rendered.") : void 0;
|
370 | if (!activeRef.current) return;
|
371 |
|
372 | if (typeof to === "number") {
|
373 | navigator.go(to);
|
374 | return;
|
375 | }
|
376 |
|
377 | let path = resolveTo(to, JSON.parse(routePathnamesJson), locationPathname);
|
378 |
|
379 | if (basename !== "/") {
|
380 | path.pathname = joinPaths([basename, path.pathname]);
|
381 | }
|
382 |
|
383 | (!!options.replace ? navigator.replace : navigator.push)(path, options.state);
|
384 | }, [basename, navigator, routePathnamesJson, locationPathname]);
|
385 | return navigate;
|
386 | }
|
387 | const OutletContext = createContext(null);
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 | function useOutletContext() {
|
395 | return useContext(OutletContext);
|
396 | }
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 | function useOutlet(context) {
|
405 | let outlet = useContext(RouteContext).outlet;
|
406 |
|
407 | if (outlet) {
|
408 | return createElement(OutletContext.Provider, {
|
409 | value: context
|
410 | }, outlet);
|
411 | }
|
412 |
|
413 | return outlet;
|
414 | }
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 | function useParams() {
|
423 | let {
|
424 | matches
|
425 | } = useContext(RouteContext);
|
426 | let routeMatch = matches[matches.length - 1];
|
427 | return routeMatch ? routeMatch.params : {};
|
428 | }
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 | function useResolvedPath(to) {
|
436 | let {
|
437 | matches
|
438 | } = useContext(RouteContext);
|
439 | let {
|
440 | pathname: locationPathname
|
441 | } = useLocation();
|
442 | let routePathnamesJson = JSON.stringify(matches.map(match => match.pathnameBase));
|
443 | return useMemo(() => resolveTo(to, JSON.parse(routePathnamesJson), locationPathname), [to, routePathnamesJson, locationPathname]);
|
444 | }
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 | function useRoutes(routes, locationArg) {
|
455 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
456 |
|
457 | "useRoutes() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
458 | let {
|
459 | matches: parentMatches
|
460 | } = useContext(RouteContext);
|
461 | let routeMatch = parentMatches[parentMatches.length - 1];
|
462 | let parentParams = routeMatch ? routeMatch.params : {};
|
463 | let parentPathname = routeMatch ? routeMatch.pathname : "/";
|
464 | let parentPathnameBase = routeMatch ? routeMatch.pathnameBase : "/";
|
465 | let parentRoute = routeMatch && routeMatch.route;
|
466 |
|
467 | if (process.env.NODE_ENV !== "production") {
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 | let parentPath = parentRoute && parentRoute.path || "";
|
489 | warningOnce(parentPathname, !parentRoute || parentPath.endsWith("*"), "You rendered descendant <Routes> (or called `useRoutes()`) at " + ("\"" + parentPathname + "\" (under <Route path=\"" + parentPath + "\">) but the ") + "parent route path has no trailing \"*\". This means if you navigate " + "deeper, the parent won't match anymore and therefore the child " + "routes will never render.\n\n" + ("Please change the parent <Route path=\"" + parentPath + "\"> to <Route ") + ("path=\"" + (parentPath === "/" ? "*" : parentPath + "/*") + "\">."));
|
490 | }
|
491 |
|
492 | let locationFromContext = useLocation();
|
493 | let location;
|
494 |
|
495 | if (locationArg) {
|
496 | var _parsedLocationArg$pa;
|
497 |
|
498 | let parsedLocationArg = typeof locationArg === "string" ? parsePath(locationArg) : locationArg;
|
499 | !(parentPathnameBase === "/" || ((_parsedLocationArg$pa = parsedLocationArg.pathname) == null ? void 0 : _parsedLocationArg$pa.startsWith(parentPathnameBase))) ? process.env.NODE_ENV !== "production" ? invariant(false, "When overriding the location using `<Routes location>` or `useRoutes(routes, location)`, " + "the location pathname must begin with the portion of the URL pathname that was " + ("matched by all parent routes. The current pathname base is \"" + parentPathnameBase + "\" ") + ("but pathname \"" + parsedLocationArg.pathname + "\" was given in the `location` prop.")) : invariant(false) : void 0;
|
500 | location = parsedLocationArg;
|
501 | } else {
|
502 | location = locationFromContext;
|
503 | }
|
504 |
|
505 | let pathname = location.pathname || "/";
|
506 | let remainingPathname = parentPathnameBase === "/" ? pathname : pathname.slice(parentPathnameBase.length) || "/";
|
507 | let matches = matchRoutes(routes, {
|
508 | pathname: remainingPathname
|
509 | });
|
510 |
|
511 | if (process.env.NODE_ENV !== "production") {
|
512 | process.env.NODE_ENV !== "production" ? warning(parentRoute || matches != null, "No routes matched location \"" + location.pathname + location.search + location.hash + "\" ") : void 0;
|
513 | process.env.NODE_ENV !== "production" ? warning(matches == null || matches[matches.length - 1].route.element !== undefined, "Matched leaf route at location \"" + location.pathname + location.search + location.hash + "\" does not have an element. " + "This means it will render an <Outlet /> with a null value by default resulting in an \"empty\" page.") : void 0;
|
514 | }
|
515 |
|
516 | return _renderMatches(matches && matches.map(match => Object.assign({}, match, {
|
517 | params: Object.assign({}, parentParams, match.params),
|
518 | pathname: joinPaths([parentPathnameBase, match.pathname]),
|
519 | pathnameBase: match.pathnameBase === "/" ? parentPathnameBase : joinPaths([parentPathnameBase, match.pathnameBase])
|
520 | })), parentMatches);
|
521 | }
|
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 | function createRoutesFromChildren(children) {
|
534 | let routes = [];
|
535 | Children.forEach(children, element => {
|
536 | if (! isValidElement(element)) {
|
537 |
|
538 |
|
539 | return;
|
540 | }
|
541 |
|
542 | if (element.type === Fragment) {
|
543 |
|
544 | routes.push.apply(routes, createRoutesFromChildren(element.props.children));
|
545 | return;
|
546 | }
|
547 |
|
548 | !(element.type === Route) ? process.env.NODE_ENV !== "production" ? invariant(false, "[" + (typeof element.type === "string" ? element.type : element.type.name) + "] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>") : invariant(false) : void 0;
|
549 | let route = {
|
550 | caseSensitive: element.props.caseSensitive,
|
551 | element: element.props.element,
|
552 | index: element.props.index,
|
553 | path: element.props.path
|
554 | };
|
555 |
|
556 | if (element.props.children) {
|
557 | route.children = createRoutesFromChildren(element.props.children);
|
558 | }
|
559 |
|
560 | routes.push(route);
|
561 | });
|
562 | return routes;
|
563 | }
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 |
|
573 | function generatePath(path, params) {
|
574 | if (params === void 0) {
|
575 | params = {};
|
576 | }
|
577 |
|
578 | return path.replace(/:(\w+)/g, (_, key) => {
|
579 | !(params[key] != null) ? process.env.NODE_ENV !== "production" ? invariant(false, "Missing \":" + key + "\" param") : invariant(false) : void 0;
|
580 | return params[key];
|
581 | }).replace(/\/*\*$/, _ => params["*"] == null ? "" : params["*"].replace(/^\/*/, "/"));
|
582 | }
|
583 |
|
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 |
|
590 |
|
591 |
|
592 | function matchRoutes(routes, locationArg, basename) {
|
593 | if (basename === void 0) {
|
594 | basename = "/";
|
595 | }
|
596 |
|
597 | let location = typeof locationArg === "string" ? parsePath(locationArg) : locationArg;
|
598 | let pathname = stripBasename(location.pathname || "/", basename);
|
599 |
|
600 | if (pathname == null) {
|
601 | return null;
|
602 | }
|
603 |
|
604 | let branches = flattenRoutes(routes);
|
605 | rankRouteBranches(branches);
|
606 | let matches = null;
|
607 |
|
608 | for (let i = 0; matches == null && i < branches.length; ++i) {
|
609 | matches = matchRouteBranch(branches[i], pathname);
|
610 | }
|
611 |
|
612 | return matches;
|
613 | }
|
614 |
|
615 | function flattenRoutes(routes, branches, parentsMeta, parentPath) {
|
616 | if (branches === void 0) {
|
617 | branches = [];
|
618 | }
|
619 |
|
620 | if (parentsMeta === void 0) {
|
621 | parentsMeta = [];
|
622 | }
|
623 |
|
624 | if (parentPath === void 0) {
|
625 | parentPath = "";
|
626 | }
|
627 |
|
628 | routes.forEach((route, index) => {
|
629 | let meta = {
|
630 | relativePath: route.path || "",
|
631 | caseSensitive: route.caseSensitive === true,
|
632 | childrenIndex: index,
|
633 | route
|
634 | };
|
635 |
|
636 | if (meta.relativePath.startsWith("/")) {
|
637 | !meta.relativePath.startsWith(parentPath) ? process.env.NODE_ENV !== "production" ? invariant(false, "Absolute route path \"" + meta.relativePath + "\" nested under path " + ("\"" + parentPath + "\" is not valid. An absolute child route path ") + "must start with the combined path of all its parent routes.") : invariant(false) : void 0;
|
638 | meta.relativePath = meta.relativePath.slice(parentPath.length);
|
639 | }
|
640 |
|
641 | let path = joinPaths([parentPath, meta.relativePath]);
|
642 | let routesMeta = parentsMeta.concat(meta);
|
643 |
|
644 |
|
645 |
|
646 | if (route.children && route.children.length > 0) {
|
647 | !(route.index !== true) ? process.env.NODE_ENV !== "production" ? invariant(false, "Index routes must not have child routes. Please remove " + ("all child routes from route path \"" + path + "\".")) : invariant(false) : void 0;
|
648 | flattenRoutes(route.children, branches, routesMeta, path);
|
649 | }
|
650 |
|
651 |
|
652 |
|
653 | if (route.path == null && !route.index) {
|
654 | return;
|
655 | }
|
656 |
|
657 | branches.push({
|
658 | path,
|
659 | score: computeScore(path, route.index),
|
660 | routesMeta
|
661 | });
|
662 | });
|
663 | return branches;
|
664 | }
|
665 |
|
666 | function rankRouteBranches(branches) {
|
667 | branches.sort((a, b) => a.score !== b.score ? b.score - a.score
|
668 | : compareIndexes(a.routesMeta.map(meta => meta.childrenIndex), b.routesMeta.map(meta => meta.childrenIndex)));
|
669 | }
|
670 |
|
671 | const paramRe = /^:\w+$/;
|
672 | const dynamicSegmentValue = 3;
|
673 | const indexRouteValue = 2;
|
674 | const emptySegmentValue = 1;
|
675 | const staticSegmentValue = 10;
|
676 | const splatPenalty = -2;
|
677 |
|
678 | const isSplat = s => s === "*";
|
679 |
|
680 | function computeScore(path, index) {
|
681 | let segments = path.split("/");
|
682 | let initialScore = segments.length;
|
683 |
|
684 | if (segments.some(isSplat)) {
|
685 | initialScore += splatPenalty;
|
686 | }
|
687 |
|
688 | if (index) {
|
689 | initialScore += indexRouteValue;
|
690 | }
|
691 |
|
692 | return segments.filter(s => !isSplat(s)).reduce((score, segment) => score + (paramRe.test(segment) ? dynamicSegmentValue : segment === "" ? emptySegmentValue : staticSegmentValue), initialScore);
|
693 | }
|
694 |
|
695 | function compareIndexes(a, b) {
|
696 | let siblings = a.length === b.length && a.slice(0, -1).every((n, i) => n === b[i]);
|
697 | return siblings ?
|
698 |
|
699 |
|
700 |
|
701 | a[a.length - 1] - b[b.length - 1] :
|
702 |
|
703 | 0;
|
704 | }
|
705 |
|
706 | function matchRouteBranch(branch, pathname) {
|
707 | let {
|
708 | routesMeta
|
709 | } = branch;
|
710 | let matchedParams = {};
|
711 | let matchedPathname = "/";
|
712 | let matches = [];
|
713 |
|
714 | for (let i = 0; i < routesMeta.length; ++i) {
|
715 | let meta = routesMeta[i];
|
716 | let end = i === routesMeta.length - 1;
|
717 | let remainingPathname = matchedPathname === "/" ? pathname : pathname.slice(matchedPathname.length) || "/";
|
718 | let match = matchPath({
|
719 | path: meta.relativePath,
|
720 | caseSensitive: meta.caseSensitive,
|
721 | end
|
722 | }, remainingPathname);
|
723 | if (!match) return null;
|
724 | Object.assign(matchedParams, match.params);
|
725 | let route = meta.route;
|
726 | matches.push({
|
727 | params: matchedParams,
|
728 | pathname: joinPaths([matchedPathname, match.pathname]),
|
729 | pathnameBase: joinPaths([matchedPathname, match.pathnameBase]),
|
730 | route
|
731 | });
|
732 |
|
733 | if (match.pathnameBase !== "/") {
|
734 | matchedPathname = joinPaths([matchedPathname, match.pathnameBase]);
|
735 | }
|
736 | }
|
737 |
|
738 | return matches;
|
739 | }
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 | function renderMatches(matches) {
|
746 | return _renderMatches(matches);
|
747 | }
|
748 |
|
749 | function _renderMatches(matches, parentMatches) {
|
750 | if (parentMatches === void 0) {
|
751 | parentMatches = [];
|
752 | }
|
753 |
|
754 | if (matches == null) return null;
|
755 | return matches.reduceRight((outlet, match, index) => {
|
756 | return createElement(RouteContext.Provider, {
|
757 | children: match.route.element !== undefined ? match.route.element : createElement(Outlet, null),
|
758 | value: {
|
759 | outlet,
|
760 | matches: parentMatches.concat(matches.slice(0, index + 1))
|
761 | }
|
762 | });
|
763 | }, null);
|
764 | }
|
765 |
|
766 |
|
767 |
|
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 |
|
774 |
|
775 |
|
776 | function matchPath(pattern, pathname) {
|
777 | if (typeof pattern === "string") {
|
778 | pattern = {
|
779 | path: pattern,
|
780 | caseSensitive: false,
|
781 | end: true
|
782 | };
|
783 | }
|
784 |
|
785 | let [matcher, paramNames] = compilePath(pattern.path, pattern.caseSensitive, pattern.end);
|
786 | let match = pathname.match(matcher);
|
787 | if (!match) return null;
|
788 | let matchedPathname = match[0];
|
789 | let pathnameBase = matchedPathname.replace(/(.)\/+$/, "$1");
|
790 | let captureGroups = match.slice(1);
|
791 | let params = paramNames.reduce((memo, paramName, index) => {
|
792 |
|
793 |
|
794 | if (paramName === "*") {
|
795 | let splatValue = captureGroups[index] || "";
|
796 | pathnameBase = matchedPathname.slice(0, matchedPathname.length - splatValue.length).replace(/(.)\/+$/, "$1");
|
797 | }
|
798 |
|
799 | memo[paramName] = safelyDecodeURIComponent(captureGroups[index] || "", paramName);
|
800 | return memo;
|
801 | }, {});
|
802 | return {
|
803 | params,
|
804 | pathname: matchedPathname,
|
805 | pathnameBase,
|
806 | pattern
|
807 | };
|
808 | }
|
809 |
|
810 | function compilePath(path, caseSensitive, end) {
|
811 | if (caseSensitive === void 0) {
|
812 | caseSensitive = false;
|
813 | }
|
814 |
|
815 | if (end === void 0) {
|
816 | end = true;
|
817 | }
|
818 |
|
819 | process.env.NODE_ENV !== "production" ? warning(path === "*" || !path.endsWith("*") || path.endsWith("/*"), "Route path \"" + path + "\" will be treated as if it were " + ("\"" + path.replace(/\*$/, "/*") + "\" because the `*` character must ") + "always follow a `/` in the pattern. To get rid of this warning, " + ("please change the route path to \"" + path.replace(/\*$/, "/*") + "\".")) : void 0;
|
820 | let paramNames = [];
|
821 | let regexpSource = "^" + path.replace(/\/*\*?$/, "")
|
822 | .replace(/^\/*/, "/")
|
823 | .replace(/[\\.*+^$?{}|()[\]]/g, "\\$&")
|
824 | .replace(/:(\w+)/g, (_, paramName) => {
|
825 | paramNames.push(paramName);
|
826 | return "([^\\/]+)";
|
827 | });
|
828 |
|
829 | if (path.endsWith("*")) {
|
830 | paramNames.push("*");
|
831 | regexpSource += path === "*" || path === "/*" ? "(.*)$"
|
832 | : "(?:\\/(.+)|\\/*)$";
|
833 | } else {
|
834 | regexpSource += end ? "\\/*$"
|
835 | :
|
836 |
|
837 |
|
838 | "(?:\\b|\\/|$)";
|
839 | }
|
840 |
|
841 | let matcher = new RegExp(regexpSource, caseSensitive ? undefined : "i");
|
842 | return [matcher, paramNames];
|
843 | }
|
844 |
|
845 | function safelyDecodeURIComponent(value, paramName) {
|
846 | try {
|
847 | return decodeURIComponent(value);
|
848 | } catch (error) {
|
849 | process.env.NODE_ENV !== "production" ? warning(false, "The value for the URL param \"" + paramName + "\" will not be decoded because" + (" the string \"" + value + "\" is a malformed URL segment. This is probably") + (" due to a bad percent encoding (" + error + ").")) : void 0;
|
850 | return value;
|
851 | }
|
852 | }
|
853 |
|
854 |
|
855 |
|
856 |
|
857 |
|
858 |
|
859 |
|
860 | function resolvePath(to, fromPathname) {
|
861 | if (fromPathname === void 0) {
|
862 | fromPathname = "/";
|
863 | }
|
864 |
|
865 | let {
|
866 | pathname: toPathname,
|
867 | search = "",
|
868 | hash = ""
|
869 | } = typeof to === "string" ? parsePath(to) : to;
|
870 | let pathname = toPathname ? toPathname.startsWith("/") ? toPathname : resolvePathname(toPathname, fromPathname) : fromPathname;
|
871 | return {
|
872 | pathname,
|
873 | search: normalizeSearch(search),
|
874 | hash: normalizeHash(hash)
|
875 | };
|
876 | }
|
877 |
|
878 | function resolvePathname(relativePath, fromPathname) {
|
879 | let segments = fromPathname.replace(/\/+$/, "").split("/");
|
880 | let relativeSegments = relativePath.split("/");
|
881 | relativeSegments.forEach(segment => {
|
882 | if (segment === "..") {
|
883 |
|
884 | if (segments.length > 1) segments.pop();
|
885 | } else if (segment !== ".") {
|
886 | segments.push(segment);
|
887 | }
|
888 | });
|
889 | return segments.length > 1 ? segments.join("/") : "/";
|
890 | }
|
891 |
|
892 | function resolveTo(toArg, routePathnames, locationPathname) {
|
893 | let to = typeof toArg === "string" ? parsePath(toArg) : toArg;
|
894 | let toPathname = toArg === "" || to.pathname === "" ? "/" : to.pathname;
|
895 |
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 | let from;
|
903 |
|
904 | if (toPathname == null) {
|
905 | from = locationPathname;
|
906 | } else {
|
907 | let routePathnameIndex = routePathnames.length - 1;
|
908 |
|
909 | if (toPathname.startsWith("..")) {
|
910 | let toSegments = toPathname.split("/");
|
911 |
|
912 |
|
913 |
|
914 | while (toSegments[0] === "..") {
|
915 | toSegments.shift();
|
916 | routePathnameIndex -= 1;
|
917 | }
|
918 |
|
919 | to.pathname = toSegments.join("/");
|
920 | }
|
921 |
|
922 |
|
923 |
|
924 | from = routePathnameIndex >= 0 ? routePathnames[routePathnameIndex] : "/";
|
925 | }
|
926 |
|
927 | let path = resolvePath(to, from);
|
928 |
|
929 | if (toPathname && toPathname !== "/" && toPathname.endsWith("/") && !path.pathname.endsWith("/")) {
|
930 | path.pathname += "/";
|
931 | }
|
932 |
|
933 | return path;
|
934 | }
|
935 |
|
936 | function getToPathname(to) {
|
937 |
|
938 | return to === "" || to.pathname === "" ? "/" : typeof to === "string" ? parsePath(to).pathname : to.pathname;
|
939 | }
|
940 |
|
941 | function stripBasename(pathname, basename) {
|
942 | if (basename === "/") return pathname;
|
943 |
|
944 | if (!pathname.toLowerCase().startsWith(basename.toLowerCase())) {
|
945 | return null;
|
946 | }
|
947 |
|
948 | let nextChar = pathname.charAt(basename.length);
|
949 |
|
950 | if (nextChar && nextChar !== "/") {
|
951 |
|
952 | return null;
|
953 | }
|
954 |
|
955 | return pathname.slice(basename.length) || "/";
|
956 | }
|
957 |
|
958 | const joinPaths = paths => paths.join("/").replace(/\/\/+/g, "/");
|
959 |
|
960 | const normalizePathname = pathname => pathname.replace(/\/+$/, "").replace(/^\/*/, "/");
|
961 |
|
962 | const normalizeSearch = search => !search || search === "?" ? "" : search.startsWith("?") ? search : "?" + search;
|
963 |
|
964 | const normalizeHash = hash => !hash || hash === "#" ? "" : hash.startsWith("#") ? hash : "#" + hash;
|
965 |
|
966 | export { MemoryRouter, Navigate, Outlet, Route, Router, Routes, LocationContext as UNSAFE_LocationContext, NavigationContext as UNSAFE_NavigationContext, RouteContext as UNSAFE_RouteContext, createRoutesFromChildren, generatePath, matchPath, matchRoutes, renderMatches, resolvePath, useHref, useInRouterContext, useLocation, useMatch, useNavigate, useNavigationType, useOutlet, useOutletContext, useParams, useResolvedPath, useRoutes };
|
967 |
|