UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

70 lines (69 loc) 2.66 kB
const MAX_MODULES = 500; const TIMEOUT_MS = 5e3; function collectImports(source, base) { const out = []; const add = spec => { if (!/^[./]|^https?:/.test(spec)) return; try { out.push(new URL(spec, base).href); } catch {} }; for (const m of source.matchAll(/(?:^|[\s;}])(?:import|export)[^'"]{0,300}?from\s*["']([^"']+)["']/g)) add(m[1]); for (const m of source.matchAll(/\bimport\s*\(\s*["']([^"']+)["']\s*\)/g)) add(m[1]); for (const m of source.matchAll(/(?:^|[\s;}])import\s*["']([^"']+)["']/g)) add(m[1]); return out; } async function findBlockedModule(entryUrl) { const deadline = Date.now() + TIMEOUT_MS; const seen = /* @__PURE__ */new Set(); let frontier = [new URL(entryUrl, window.location.href).href]; while (frontier.length && seen.size < MAX_MODULES && Date.now() < deadline) { const batch = frontier.filter(url => !seen.has(url)).slice(0, 32); if (!batch.length) break; frontier = frontier.slice(batch.length); const results = await Promise.all(batch.map(async url => { seen.add(url); let res; try { res = await fetch(url); } catch { return { url, blocked: true, imports: [] }; } if (!res.ok) return { url, blocked: false, imports: [] }; const type = res.headers.get("content-type") ?? ""; if (!type.includes("javascript")) return { url, blocked: false, imports: [] }; return { url, blocked: false, imports: collectImports(await res.text(), url) }; })); for (const result of results) { if (result.blocked) return result.url; for (const next of result.imports) { if (!seen.has(next)) frontier.push(next); } } } return null; } async function diagnoseRouteLoadFailure(routeId, routeUrl) { const blocked = await findBlockedModule(routeUrl); if (!blocked) return null; const path = blocked.replace(window.location.origin, ""); return [`Route "${routeId}" failed to load because this browser refused to fetch ${path}`, ``, `That request never reached the dev server, so a content blocker or privacy`, `extension is blocking it. In dev the module URL is the source path, and a`, `path containing a word like "analytics", "pageview" or "track" matches the`, `tracking rules those blockers ship.`, ``, `Rename the file or the directory so its path no longer matches, or allow`, `${window.location.origin} in the blocker.`].join("\n"); } export { diagnoseRouteLoadFailure, findBlockedModule }; //# sourceMappingURL=diagnoseRouteLoadFailure.mjs.map