| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 |
|
| 11 | import { createPath } from "../../router/history.js";
|
| 12 | import { joinPaths, matchRoutesImpl } from "../../router/utils.js";
|
| 13 | import { createClientRoutes } from "./routes.js";
|
| 14 | import * as React$1 from "react";
|
| 15 |
|
| 16 | const nextPaths = new Set();
|
| 17 | const discoveredPathsMaxSize = 1e3;
|
| 18 | const discoveredPaths = new Set();
|
| 19 | const URL_LIMIT = 7680;
|
| 20 | function getPathsWithAncestors(paths) {
|
| 21 | let result = new Set();
|
| 22 | paths.forEach((path) => {
|
| 23 | if (!path.startsWith("/")) path = `/${path}`;
|
| 24 | for (let i = 1; i < path.length; i++) if (path[i] === "/") result.add(path.slice(0, i));
|
| 25 | result.add(path);
|
| 26 | });
|
| 27 | return Array.from(result);
|
| 28 | }
|
| 29 | function isFogOfWarEnabled(routeDiscovery, ssr) {
|
| 30 | return routeDiscovery.mode === "lazy" && ssr === true;
|
| 31 | }
|
| 32 | function getPartialManifest({ sri, ...manifest }, router) {
|
| 33 | let routeIds = new Set(router.state.matches.map((m) => m.route.id));
|
| 34 | let segments = router.state.location.pathname.split("/").filter(Boolean);
|
| 35 | let paths = ["/"];
|
| 36 | segments.pop();
|
| 37 | while (segments.length > 0) {
|
| 38 | paths.push(`/${segments.join("/")}`);
|
| 39 | segments.pop();
|
| 40 | }
|
| 41 | paths.forEach((path) => {
|
| 42 | let matches = matchRoutesImpl(router.routes, path, router.basename || "/", false, router.branches);
|
| 43 | if (matches) matches.forEach((m) => routeIds.add(m.route.id));
|
| 44 | });
|
| 45 | let initialRoutes = [...routeIds].reduce((acc, id) => Object.assign(acc, { [id]: manifest.routes[id] }), {});
|
| 46 | return {
|
| 47 | ...manifest,
|
| 48 | routes: initialRoutes,
|
| 49 | sri: sri ? true : void 0
|
| 50 | };
|
| 51 | }
|
| 52 | function getPatchRoutesOnNavigationFunction(getRouter, manifest, routeModules, ssr, routeDiscovery, isSpaMode, basename) {
|
| 53 | if (!isFogOfWarEnabled(routeDiscovery, ssr)) return;
|
| 54 | return async ({ path, patch, signal, fetcherKey }) => {
|
| 55 | if (discoveredPaths.has(path)) return;
|
| 56 | let { state } = getRouter();
|
| 57 | await fetchAndApplyManifestPatches([path], fetcherKey ? window.location.href : createPath(state.navigation.location || state.location), manifest, routeModules, ssr, isSpaMode, basename, routeDiscovery.manifestPath, patch, signal);
|
| 58 | };
|
| 59 | }
|
| 60 | function useFogOFWarDiscovery(router, manifest, routeModules, ssr, routeDiscovery, isSpaMode) {
|
| 61 | React$1.useEffect(() => {
|
| 62 | if (!isFogOfWarEnabled(routeDiscovery, ssr) || window.navigator?.connection?.saveData === true) return;
|
| 63 | function registerElement(el) {
|
| 64 | let path = el.tagName === "FORM" ? el.getAttribute("action") : el.getAttribute("href");
|
| 65 | if (!path) return;
|
| 66 | let pathname = el.tagName === "A" ? el.pathname : new URL(path, window.location.origin).pathname;
|
| 67 | if (!discoveredPaths.has(pathname)) nextPaths.add(pathname);
|
| 68 | }
|
| 69 | async function fetchPatches() {
|
| 70 | document.querySelectorAll("a[data-discover], form[data-discover]").forEach(registerElement);
|
| 71 | let lazyPaths = Array.from(nextPaths.keys()).filter((path) => {
|
| 72 | if (discoveredPaths.has(path)) {
|
| 73 | nextPaths.delete(path);
|
| 74 | return false;
|
| 75 | }
|
| 76 | return true;
|
| 77 | });
|
| 78 | if (lazyPaths.length === 0) return;
|
| 79 | try {
|
| 80 | await fetchAndApplyManifestPatches(lazyPaths, null, manifest, routeModules, ssr, isSpaMode, router.basename, routeDiscovery.manifestPath, router.patchRoutes);
|
| 81 | } catch (e) {
|
| 82 | console.error("Failed to fetch manifest patches", e);
|
| 83 | }
|
| 84 | }
|
| 85 | let debouncedFetchPatches = debounce(fetchPatches, 100);
|
| 86 | fetchPatches();
|
| 87 | let observer = new MutationObserver(() => debouncedFetchPatches());
|
| 88 | observer.observe(document.documentElement, {
|
| 89 | subtree: true,
|
| 90 | childList: true,
|
| 91 | attributes: true,
|
| 92 | attributeFilter: [
|
| 93 | "data-discover",
|
| 94 | "href",
|
| 95 | "action"
|
| 96 | ]
|
| 97 | });
|
| 98 | return () => observer.disconnect();
|
| 99 | }, [
|
| 100 | ssr,
|
| 101 | isSpaMode,
|
| 102 | manifest,
|
| 103 | routeModules,
|
| 104 | router,
|
| 105 | routeDiscovery
|
| 106 | ]);
|
| 107 | }
|
| 108 | function getManifestPath(_manifestPath, basename) {
|
| 109 | let manifestPath = _manifestPath || "/__manifest";
|
| 110 | return basename == null ? manifestPath : joinPaths([basename, manifestPath]);
|
| 111 | }
|
| 112 | const MANIFEST_VERSION_STORAGE_KEY = "react-router-manifest-version";
|
| 113 | async function fetchAndApplyManifestPatches(paths, errorReloadPath, manifest, routeModules, ssr, isSpaMode, basename, manifestPath, patchRoutes, signal) {
|
| 114 | paths = getPathsWithAncestors(paths);
|
| 115 | const searchParams = new URLSearchParams();
|
| 116 | searchParams.set("paths", paths.sort().join(","));
|
| 117 | searchParams.set("version", manifest.version);
|
| 118 | let url = new URL(getManifestPath(manifestPath, basename), window.location.origin);
|
| 119 | url.search = searchParams.toString();
|
| 120 | if (url.toString().length > 7680) {
|
| 121 | nextPaths.clear();
|
| 122 | return;
|
| 123 | }
|
| 124 | let serverPatches;
|
| 125 | try {
|
| 126 | let res = await fetch(url, { signal });
|
| 127 | if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
|
| 128 | else if (res.status === 204 && res.headers.has("X-Remix-Reload-Document")) {
|
| 129 | if (!errorReloadPath) {
|
| 130 | console.warn("Detected a manifest version mismatch during eager route discovery. The next navigation/fetch to an undiscovered route will result in a new document navigation to sync up with the latest manifest.");
|
| 131 | return;
|
| 132 | }
|
| 133 | try {
|
| 134 | if (sessionStorage.getItem(MANIFEST_VERSION_STORAGE_KEY) === manifest.version) {
|
| 135 | console.error("Unable to discover routes due to manifest version mismatch.");
|
| 136 | return;
|
| 137 | }
|
| 138 | sessionStorage.setItem(MANIFEST_VERSION_STORAGE_KEY, manifest.version);
|
| 139 | } catch {}
|
| 140 | window.location.href = errorReloadPath;
|
| 141 | console.warn("Detected manifest version mismatch, reloading...");
|
| 142 | await new Promise(() => {});
|
| 143 | } else if (res.status >= 400) throw new Error(await res.text());
|
| 144 | try {
|
| 145 | sessionStorage.removeItem(MANIFEST_VERSION_STORAGE_KEY);
|
| 146 | } catch {}
|
| 147 | serverPatches = await res.json();
|
| 148 | } catch (e) {
|
| 149 | if (signal?.aborted) return;
|
| 150 | throw e;
|
| 151 | }
|
| 152 | let knownRoutes = new Set(Object.keys(manifest.routes));
|
| 153 | let patches = Object.values(serverPatches).reduce((acc, route) => {
|
| 154 | if (route && !knownRoutes.has(route.id)) acc[route.id] = route;
|
| 155 | return acc;
|
| 156 | }, {});
|
| 157 | Object.assign(manifest.routes, patches);
|
| 158 | paths.forEach((p) => addToFifoQueue(p, discoveredPaths));
|
| 159 | let parentIds = new Set();
|
| 160 | Object.values(patches).forEach((patch) => {
|
| 161 | if (patch && (!patch.parentId || !patches[patch.parentId])) parentIds.add(patch.parentId);
|
| 162 | });
|
| 163 | parentIds.forEach((parentId) => patchRoutes(parentId || null, createClientRoutes(patches, routeModules, null, ssr, isSpaMode, parentId)));
|
| 164 | }
|
| 165 | function addToFifoQueue(path, queue) {
|
| 166 | if (queue.size >= discoveredPathsMaxSize) {
|
| 167 | let first = queue.values().next().value;
|
| 168 | if (first !== void 0) queue.delete(first);
|
| 169 | }
|
| 170 | queue.add(path);
|
| 171 | }
|
| 172 | function debounce(callback, wait) {
|
| 173 | let timeoutId;
|
| 174 | return (...args) => {
|
| 175 | window.clearTimeout(timeoutId);
|
| 176 | timeoutId = window.setTimeout(() => callback(...args), wait);
|
| 177 | };
|
| 178 | }
|
| 179 |
|
| 180 | export { URL_LIMIT, getManifestPath, getPartialManifest, getPatchRoutesOnNavigationFunction, getPathsWithAncestors, isFogOfWarEnabled, useFogOFWarDiscovery };
|