@renr/parcel-rsc-router 1.0.0-beta.10 → 1.0.0-beta.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/Link.d.mts +1 -1
- package/dist/esm/Link.d.mts.map +1 -1
- package/dist/esm/Link.mjs +44 -2
- package/dist/esm/Link.mjs.map +1 -1
- package/dist/esm/history.mjs +18 -8
- package/dist/esm/history.mjs.map +1 -1
- package/dist/esm/rsc.mjs +14 -12
- package/dist/esm/rsc.mjs.map +1 -1
- package/package.json +1 -1
package/dist/esm/Link.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RoutePath } from "./types.mjs";
|
|
2
2
|
import React, { HTMLAttributeAnchorTarget } from "react";
|
|
3
3
|
//#region src/Link.d.ts
|
|
4
|
-
export declare function Link({ to, children, hash, target, search, onClick, onMouseEnter, onFocus, prefetch, ...props }: {
|
|
4
|
+
export declare function Link({ to, children, hash, target, search, onClick, onMouseEnter, onMouseLeave, onFocus, onBlur, onPointerDown, onTouchStart, prefetch, ...props }: {
|
|
5
5
|
to: RoutePath;
|
|
6
6
|
hash?: string;
|
|
7
7
|
search?: Record<string, string>;
|
package/dist/esm/Link.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Link.d.mts","names":[],"sources":["../../src/Link.tsx"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"Link.d.mts","names":[],"sources":["../../src/Link.tsx"],"mappings":";;;wBAQgB,OACd,IACA,UACA,MACA,QACA,QACA,SACA,cACA,cACA,SACA,QACA,eACA,cACA,aACG;EAEH,IAAI;EACJ;EACA,SAAS;EACT,UAAU,MAAM;EAChB,SAAS;EACT;IACE,MAAM,qBAAqB,qBAAkB,MAAA,IAAA"}
|
package/dist/esm/Link.mjs
CHANGED
|
@@ -3,25 +3,67 @@ import { useRouter } from "./Router.mjs";
|
|
|
3
3
|
import React from "react";
|
|
4
4
|
import { jsx } from "react/jsx-runtime";
|
|
5
5
|
//#region src/Link.tsx
|
|
6
|
-
|
|
6
|
+
const PREFETCH_DELAY_MS = 50;
|
|
7
|
+
function Link({ to, children, hash, target, search, onClick, onMouseEnter, onMouseLeave, onFocus, onBlur, onPointerDown, onTouchStart, prefetch = true, ...props }) {
|
|
7
8
|
const { navigate, prefetch: prefetchRoute } = useRouter();
|
|
8
9
|
const href = `${to}${hash ? `#${hash}` : ""}${search ? `?${new URLSearchParams(search)}` : ""}`;
|
|
10
|
+
const hoverTimeoutRef = React.useRef(null);
|
|
11
|
+
const cancelPrefetch = React.useCallback(() => {
|
|
12
|
+
if (hoverTimeoutRef.current !== null) {
|
|
13
|
+
clearTimeout(hoverTimeoutRef.current);
|
|
14
|
+
hoverTimeoutRef.current = null;
|
|
15
|
+
}
|
|
16
|
+
}, []);
|
|
9
17
|
const handlePrefetch = React.useCallback(() => {
|
|
18
|
+
cancelPrefetch();
|
|
10
19
|
if (prefetch && (!target || target === "_self") && prefetchRoute) prefetchRoute(to);
|
|
11
20
|
}, [
|
|
21
|
+
cancelPrefetch,
|
|
12
22
|
prefetch,
|
|
13
23
|
target,
|
|
14
24
|
prefetchRoute,
|
|
15
25
|
to
|
|
16
26
|
]);
|
|
27
|
+
const schedulePrefetch = React.useCallback(() => {
|
|
28
|
+
cancelPrefetch();
|
|
29
|
+
if (prefetch && (!target || target === "_self") && prefetchRoute) hoverTimeoutRef.current = setTimeout(() => {
|
|
30
|
+
hoverTimeoutRef.current = null;
|
|
31
|
+
prefetchRoute(to);
|
|
32
|
+
}, PREFETCH_DELAY_MS);
|
|
33
|
+
}, [
|
|
34
|
+
cancelPrefetch,
|
|
35
|
+
prefetch,
|
|
36
|
+
target,
|
|
37
|
+
prefetchRoute,
|
|
38
|
+
to
|
|
39
|
+
]);
|
|
40
|
+
React.useEffect(() => {
|
|
41
|
+
return cancelPrefetch;
|
|
42
|
+
}, [cancelPrefetch]);
|
|
17
43
|
return /* @__PURE__ */ jsx("a", {
|
|
18
44
|
href,
|
|
19
45
|
onMouseEnter: (e) => {
|
|
20
46
|
if (onMouseEnter) onMouseEnter(e);
|
|
21
|
-
|
|
47
|
+
schedulePrefetch();
|
|
48
|
+
},
|
|
49
|
+
onMouseLeave: (e) => {
|
|
50
|
+
if (onMouseLeave) onMouseLeave(e);
|
|
51
|
+
cancelPrefetch();
|
|
22
52
|
},
|
|
23
53
|
onFocus: (e) => {
|
|
24
54
|
if (onFocus) onFocus(e);
|
|
55
|
+
schedulePrefetch();
|
|
56
|
+
},
|
|
57
|
+
onBlur: (e) => {
|
|
58
|
+
if (onBlur) onBlur(e);
|
|
59
|
+
cancelPrefetch();
|
|
60
|
+
},
|
|
61
|
+
onPointerDown: (e) => {
|
|
62
|
+
if (onPointerDown) onPointerDown(e);
|
|
63
|
+
handlePrefetch();
|
|
64
|
+
},
|
|
65
|
+
onTouchStart: (e) => {
|
|
66
|
+
if (onTouchStart) onTouchStart(e);
|
|
25
67
|
handlePrefetch();
|
|
26
68
|
},
|
|
27
69
|
onClick: (e) => {
|
package/dist/esm/Link.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Link.mjs","names":[],"sources":["../../src/Link.tsx"],"sourcesContent":["\"use client\";\n\nimport React, { type HTMLAttributeAnchorTarget } from \"react\";\nimport { RoutePath } from \"./types.js\";\nimport { useRouter } from \"./Router.js\";\n\nexport function Link({\n to,\n children,\n hash,\n target,\n search,\n onClick,\n onMouseEnter,\n onFocus,\n prefetch = true,\n ...props\n}: {\n to: RoutePath;\n hash?: string;\n search?: Record<string, string>;\n children: React.ReactNode;\n target?: HTMLAttributeAnchorTarget;\n prefetch?: boolean;\n} & React.AnchorHTMLAttributes<HTMLAnchorElement>) {\n const { navigate, prefetch: prefetchRoute } = useRouter();\n const href = `${to}${hash ? `#${hash}` : \"\"}${search ? `?${new URLSearchParams(search)}` : \"\"}`;\n\n const handlePrefetch = React.useCallback(() => {\n if (prefetch && (!target || target === \"_self\") && prefetchRoute) {\n void prefetchRoute(to);\n }\n }, [prefetch, target, prefetchRoute, to]);\n\n return (\n <a\n href={href}\n onMouseEnter={(e) => {\n if (onMouseEnter) {\n onMouseEnter(e);\n }\n
|
|
1
|
+
{"version":3,"file":"Link.mjs","names":[],"sources":["../../src/Link.tsx"],"sourcesContent":["\"use client\";\n\nimport React, { type HTMLAttributeAnchorTarget } from \"react\";\nimport { RoutePath } from \"./types.js\";\nimport { useRouter } from \"./Router.js\";\n\nconst PREFETCH_DELAY_MS = 50;\n\nexport function Link({\n to,\n children,\n hash,\n target,\n search,\n onClick,\n onMouseEnter,\n onMouseLeave,\n onFocus,\n onBlur,\n onPointerDown,\n onTouchStart,\n prefetch = true,\n ...props\n}: {\n to: RoutePath;\n hash?: string;\n search?: Record<string, string>;\n children: React.ReactNode;\n target?: HTMLAttributeAnchorTarget;\n prefetch?: boolean;\n} & React.AnchorHTMLAttributes<HTMLAnchorElement>) {\n const { navigate, prefetch: prefetchRoute } = useRouter();\n const href = `${to}${hash ? `#${hash}` : \"\"}${search ? `?${new URLSearchParams(search)}` : \"\"}`;\n const hoverTimeoutRef = React.useRef<ReturnType<typeof setTimeout> | null>(\n null\n );\n\n const cancelPrefetch = React.useCallback(() => {\n if (hoverTimeoutRef.current !== null) {\n clearTimeout(hoverTimeoutRef.current);\n hoverTimeoutRef.current = null;\n }\n }, []);\n\n const handlePrefetch = React.useCallback(() => {\n cancelPrefetch();\n if (prefetch && (!target || target === \"_self\") && prefetchRoute) {\n void prefetchRoute(to);\n }\n }, [cancelPrefetch, prefetch, target, prefetchRoute, to]);\n\n const schedulePrefetch = React.useCallback(() => {\n cancelPrefetch();\n if (prefetch && (!target || target === \"_self\") && prefetchRoute) {\n hoverTimeoutRef.current = setTimeout(() => {\n hoverTimeoutRef.current = null;\n void prefetchRoute(to);\n }, PREFETCH_DELAY_MS);\n }\n }, [cancelPrefetch, prefetch, target, prefetchRoute, to]);\n\n React.useEffect(() => {\n return cancelPrefetch;\n }, [cancelPrefetch]);\n\n return (\n <a\n href={href}\n onMouseEnter={(e) => {\n if (onMouseEnter) {\n onMouseEnter(e);\n }\n schedulePrefetch();\n }}\n onMouseLeave={(e) => {\n if (onMouseLeave) {\n onMouseLeave(e);\n }\n cancelPrefetch();\n }}\n onFocus={(e) => {\n if (onFocus) {\n onFocus(e);\n }\n schedulePrefetch();\n }}\n onBlur={(e) => {\n if (onBlur) {\n onBlur(e);\n }\n cancelPrefetch();\n }}\n onPointerDown={(e) => {\n if (onPointerDown) {\n onPointerDown(e);\n }\n handlePrefetch();\n }}\n onTouchStart={(e) => {\n if (onTouchStart) {\n onTouchStart(e);\n }\n handlePrefetch();\n }}\n onClick={(e) => {\n if (onClick) {\n onClick(e);\n }\n\n if (\n (!target || target === \"_self\") &&\n e.button === 0 && // left clicks only\n !e.metaKey && // open in new tab (mac)\n !e.ctrlKey && // open in new tab (windows)\n !e.altKey && // download\n !e.shiftKey &&\n !e.defaultPrevented\n ) {\n // Intercept link clicks to perform RSC navigation.\n e.preventDefault();\n navigate(href);\n }\n }}\n {...props}\n >\n {children}\n </a>\n );\n}\n"],"mappings":";;;;;AAMA,MAAM,oBAAoB;AAE1B,SAAgB,KAAK,EACnB,IACA,UACA,MACA,QACA,QACA,SACA,cACA,cACA,SACA,QACA,eACA,cACA,WAAW,MACX,GAAG,SAQ8C;CACjD,MAAM,EAAE,UAAU,UAAU,kBAAkB,UAAU;CACxD,MAAM,OAAO,GAAG,KAAK,OAAO,IAAI,SAAS,KAAK,SAAS,IAAI,IAAI,gBAAgB,MAAM,MAAM;CAC3F,MAAM,kBAAkB,MAAM,OAC5B,IACF;CAEA,MAAM,iBAAiB,MAAM,kBAAkB;EAC7C,IAAI,gBAAgB,YAAY,MAAM;GACpC,aAAa,gBAAgB,OAAO;GACpC,gBAAgB,UAAU;EAC5B;CACF,GAAG,CAAC,CAAC;CAEL,MAAM,iBAAiB,MAAM,kBAAkB;EAC7C,eAAe;EACf,IAAI,aAAa,CAAC,UAAU,WAAW,YAAY,eACjD,cAAmB,EAAE;CAEzB,GAAG;EAAC;EAAgB;EAAU;EAAQ;EAAe;CAAE,CAAC;CAExD,MAAM,mBAAmB,MAAM,kBAAkB;EAC/C,eAAe;EACf,IAAI,aAAa,CAAC,UAAU,WAAW,YAAY,eACjD,gBAAgB,UAAU,iBAAiB;GACzC,gBAAgB,UAAU;GAC1B,cAAmB,EAAE;EACvB,GAAG,iBAAiB;CAExB,GAAG;EAAC;EAAgB;EAAU;EAAQ;EAAe;CAAE,CAAC;CAExD,MAAM,gBAAgB;EACpB,OAAO;CACT,GAAG,CAAC,cAAc,CAAC;CAEnB,OACE,oBAAC,KAAD;EACQ;EACN,eAAe,MAAM;GACnB,IAAI,cACF,aAAa,CAAC;GAEhB,iBAAiB;EACnB;EACA,eAAe,MAAM;GACnB,IAAI,cACF,aAAa,CAAC;GAEhB,eAAe;EACjB;EACA,UAAU,MAAM;GACd,IAAI,SACF,QAAQ,CAAC;GAEX,iBAAiB;EACnB;EACA,SAAS,MAAM;GACb,IAAI,QACF,OAAO,CAAC;GAEV,eAAe;EACjB;EACA,gBAAgB,MAAM;GACpB,IAAI,eACF,cAAc,CAAC;GAEjB,eAAe;EACjB;EACA,eAAe,MAAM;GACnB,IAAI,cACF,aAAa,CAAC;GAEhB,eAAe;EACjB;EACA,UAAU,MAAM;GACd,IAAI,SACF,QAAQ,CAAC;GAGX,KACG,CAAC,UAAU,WAAW,YACvB,EAAE,WAAW,KACb,CAAC,EAAE,WACH,CAAC,EAAE,WACH,CAAC,EAAE,UACH,CAAC,EAAE,YACH,CAAC,EAAE,kBACH;IAEA,EAAE,eAAe;IACjB,SAAS,IAAI;GACf;EACF;EACA,GAAI;EAEH;CACA,CAAA;AAEP"}
|
package/dist/esm/history.mjs
CHANGED
|
@@ -2,22 +2,32 @@
|
|
|
2
2
|
//#region src/history.ts
|
|
3
3
|
function createHistory() {
|
|
4
4
|
const listeners = /* @__PURE__ */ new Set();
|
|
5
|
+
let lastHref = null;
|
|
6
|
+
let lastState = null;
|
|
5
7
|
let currentSnapshot;
|
|
6
8
|
const getSnapshot = () => {
|
|
7
9
|
if (typeof window === "undefined") return {
|
|
8
10
|
location: null,
|
|
9
11
|
state: null
|
|
10
12
|
};
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
const currentHref = window.location.href;
|
|
14
|
+
const currentState = window.history.state;
|
|
15
|
+
if (!currentSnapshot || currentHref !== lastHref || currentState !== lastState) {
|
|
16
|
+
lastHref = currentHref;
|
|
17
|
+
lastState = currentState;
|
|
18
|
+
currentSnapshot = {
|
|
19
|
+
location: window.location,
|
|
20
|
+
state: currentState
|
|
21
|
+
};
|
|
22
|
+
}
|
|
16
23
|
return currentSnapshot;
|
|
17
24
|
};
|
|
18
|
-
if (typeof window !== "undefined")
|
|
19
|
-
|
|
20
|
-
|
|
25
|
+
if (typeof window !== "undefined") {
|
|
26
|
+
if ("scrollRestoration" in window.history) window.history.scrollRestoration = "manual";
|
|
27
|
+
window.addEventListener("popstate", () => {
|
|
28
|
+
queueMicrotask(() => listeners.forEach((listener) => listener()));
|
|
29
|
+
});
|
|
30
|
+
}
|
|
21
31
|
return {
|
|
22
32
|
get snapshot() {
|
|
23
33
|
return getSnapshot();
|
package/dist/esm/history.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"history.mjs","names":[],"sources":["../../src/history.ts"],"sourcesContent":["\"use client\";\n\n// basic history management\n\nexport type HistoryState = {\n key: string;\n scrollX: number;\n scrollY: number;\n};\n\nexport function createHistory() {\n const listeners = new Set<() => void>();\n let currentSnapshot: {\n location: Location | null;\n state: HistoryState | null;\n };\n\n const getSnapshot = () => {\n if (typeof window === \"undefined\") {\n return {\n location: null,\n state: null,\n };\n }\n\n const
|
|
1
|
+
{"version":3,"file":"history.mjs","names":[],"sources":["../../src/history.ts"],"sourcesContent":["\"use client\";\n\n// basic history management\n\nexport type HistoryState = {\n key: string;\n scrollX: number;\n scrollY: number;\n};\n\nexport function createHistory() {\n const listeners = new Set<() => void>();\n let lastHref: string | null = null;\n let lastState: HistoryState | null = null;\n let currentSnapshot: {\n location: Location | null;\n state: HistoryState | null;\n };\n\n const getSnapshot = () => {\n if (typeof window === \"undefined\") {\n return {\n location: null,\n state: null,\n };\n }\n\n const currentHref = window.location.href;\n const currentState = window.history.state as HistoryState | null;\n\n // Only update currentSnapshot if something actually changed to prevent infinite loops\n if (\n !currentSnapshot ||\n currentHref !== lastHref ||\n currentState !== lastState\n ) {\n lastHref = currentHref;\n lastState = currentState;\n currentSnapshot = {\n location: window.location,\n state: currentState,\n };\n }\n return currentSnapshot;\n };\n\n if (typeof window !== \"undefined\") {\n if (\"scrollRestoration\" in window.history) {\n window.history.scrollRestoration = \"manual\";\n }\n\n window.addEventListener(\"popstate\", () => {\n // notify listeners - do it in a microtask to avoid scheduling react updates\n queueMicrotask(() => listeners.forEach((listener) => listener()));\n });\n }\n\n return {\n get snapshot() {\n return getSnapshot();\n },\n\n getSnapshot,\n\n subscribe(this: void, listener: () => void) {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n\n push(url: string, state: HistoryState) {\n if (typeof window !== \"undefined\") {\n window.history.pushState(state, \"\", url);\n }\n // notify listeners - do it in a microtask to avoid scheduling react updates\n queueMicrotask(() => listeners.forEach((listener) => listener()));\n },\n\n replace(url: string, state: HistoryState) {\n if (typeof window !== \"undefined\") {\n window.history.replaceState(state, \"\", url);\n }\n // don't notify listeners on replace, as it's for non-navigation changes\n },\n };\n}\n\nexport type BrowserHistory = ReturnType<typeof createHistory>;\n"],"mappings":";;AAUA,SAAgB,gBAAgB;CAC9B,MAAM,4BAAY,IAAI,IAAgB;CACtC,IAAI,WAA0B;CAC9B,IAAI,YAAiC;CACrC,IAAI;CAKJ,MAAM,oBAAoB;EACxB,IAAI,OAAO,WAAW,aACpB,OAAO;GACL,UAAU;GACV,OAAO;EACT;EAGF,MAAM,cAAc,OAAO,SAAS;EACpC,MAAM,eAAe,OAAO,QAAQ;EAGpC,IACE,CAAC,mBACD,gBAAgB,YAChB,iBAAiB,WACjB;GACA,WAAW;GACX,YAAY;GACZ,kBAAkB;IAChB,UAAU,OAAO;IACjB,OAAO;GACT;EACF;EACA,OAAO;CACT;CAEA,IAAI,OAAO,WAAW,aAAa;EACjC,IAAI,uBAAuB,OAAO,SAChC,OAAO,QAAQ,oBAAoB;EAGrC,OAAO,iBAAiB,kBAAkB;GAExC,qBAAqB,UAAU,SAAS,aAAa,SAAS,CAAC,CAAC;EAClE,CAAC;CACH;CAEA,OAAO;EACL,IAAI,WAAW;GACb,OAAO,YAAY;EACrB;EAEA;EAEA,UAAsB,UAAsB;GAC1C,UAAU,IAAI,QAAQ;GACtB,aAAa;IACX,UAAU,OAAO,QAAQ;GAC3B;EACF;EAEA,KAAK,KAAa,OAAqB;GACrC,IAAI,OAAO,WAAW,aACpB,OAAO,QAAQ,UAAU,OAAO,IAAI,GAAG;GAGzC,qBAAqB,UAAU,SAAS,aAAa,SAAS,CAAC,CAAC;EAClE;EAEA,QAAQ,KAAa,OAAqB;GACxC,IAAI,OAAO,WAAW,aACpB,OAAO,QAAQ,aAAa,OAAO,IAAI,GAAG;EAG9C;CACF;AACF"}
|
package/dist/esm/rsc.mjs
CHANGED
|
@@ -16,28 +16,30 @@ async function navigate(pathname, routes, onPush, { push = true } = {}) {
|
|
|
16
16
|
if (path.url === location.pathname) shouldPush = false;
|
|
17
17
|
try {
|
|
18
18
|
const root = await fetchRSCWithPreloadCache(path.rsc);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
});
|
|
19
|
+
if (shouldPush) onPush(path.url);
|
|
20
|
+
updateRoot(root);
|
|
22
21
|
} catch (e) {
|
|
23
22
|
console.error(`Error fetching ${path.path} RSC`, e);
|
|
24
23
|
}
|
|
25
24
|
}
|
|
26
25
|
async function fetchRSCWithPreloadCache(url, options) {
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
const existing = fetchCache.get(url);
|
|
27
|
+
if (existing) {
|
|
28
|
+
fetchCache.delete(url);
|
|
29
|
+
fetchCache.set(url, existing);
|
|
30
|
+
return existing;
|
|
31
|
+
}
|
|
32
|
+
const promise = fetchRSC(url, options).catch((error) => {
|
|
33
|
+
fetchCache.delete(url);
|
|
34
|
+
throw error;
|
|
35
|
+
});
|
|
29
36
|
fetchCache.set(url, promise);
|
|
30
|
-
while (fetchCache.size >
|
|
37
|
+
while (fetchCache.size > 50) {
|
|
31
38
|
const firstKey = fetchCache.keys().next().value;
|
|
32
39
|
if (!firstKey) break;
|
|
33
40
|
fetchCache.delete(firstKey);
|
|
34
41
|
}
|
|
35
|
-
return promise
|
|
36
|
-
return data;
|
|
37
|
-
}).catch((error) => {
|
|
38
|
-
fetchCache.delete(url);
|
|
39
|
-
throw error;
|
|
40
|
-
});
|
|
42
|
+
return promise;
|
|
41
43
|
}
|
|
42
44
|
const fetchCache = /* @__PURE__ */ new Map();
|
|
43
45
|
async function prefetch(pathname, routes, signal) {
|
package/dist/esm/rsc.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rsc.mjs","names":[],"sources":["../../src/rsc.ts"],"sourcesContent":["\"use client\";\n\nimport { type RSCRequestInit, fetchRSC } from \"@parcel/rsc/client\";\nimport { type ReactNode } from \"react\";\nimport { updateRoot } from \"./client-entry.js\";\nimport { RouteData } from \"./types.js\";\nimport { getPathData } from \"./util.js\";\n\n// fetch and push an rsc route\nexport async function navigate(\n pathname: string,\n routes: RouteData[],\n /** callback for a successful navigation (use this to update the history) */\n onPush: (url: string) => void,\n { push = true }: { push?: boolean } = {}\n) {\n let shouldPush = push;\n\n const path = getPathData(pathname, routes);\n\n if (!path) {\n console.warn(`No route found for ${pathname}`);\n\n if (routes.some((r) => r.path === \"/404\")) {\n // we have a 404 page, navigate there\n void navigate(\"/404\", routes, () => {\n // show the original not found path (rather than 404) in the URL\n onPush(pathname);\n });\n }\n\n return;\n }\n\n // Don't add to history if we're already on this page\n if (path.url === location.pathname) {\n shouldPush = false;\n }\n\n try {\n const root = await fetchRSCWithPreloadCache<ReactNode>(path.rsc);\n\n
|
|
1
|
+
{"version":3,"file":"rsc.mjs","names":[],"sources":["../../src/rsc.ts"],"sourcesContent":["\"use client\";\n\nimport { type RSCRequestInit, fetchRSC } from \"@parcel/rsc/client\";\nimport { type ReactNode } from \"react\";\nimport { updateRoot } from \"./client-entry.js\";\nimport { RouteData } from \"./types.js\";\nimport { getPathData } from \"./util.js\";\n\n// fetch and push an rsc route\nexport async function navigate(\n pathname: string,\n routes: RouteData[],\n /** callback for a successful navigation (use this to update the history) */\n onPush: (url: string) => void,\n { push = true }: { push?: boolean } = {}\n) {\n let shouldPush = push;\n\n const path = getPathData(pathname, routes);\n\n if (!path) {\n console.warn(`No route found for ${pathname}`);\n\n if (routes.some((r) => r.path === \"/404\")) {\n // we have a 404 page, navigate there\n void navigate(\"/404\", routes, () => {\n // show the original not found path (rather than 404) in the URL\n onPush(pathname);\n });\n }\n\n return;\n }\n\n // Don't add to history if we're already on this page\n if (path.url === location.pathname) {\n shouldPush = false;\n }\n\n try {\n const root = await fetchRSCWithPreloadCache<ReactNode>(path.rsc);\n\n if (shouldPush) {\n onPush(path.url);\n }\n\n updateRoot(root);\n\n // after a successful navigation, we can clear the cache\n // note everything is static currently, we don't need to clear the cache\n // fetchCache.clear();\n } catch (e) {\n console.error(`Error fetching ${path.path} RSC`, e);\n }\n}\n\nexport const MAX_CACHE_SIZE = 50;\n\nasync function fetchRSCWithPreloadCache<T>(\n url: string | URL | Request,\n options?: RSCRequestInit\n): Promise<T> {\n const existing = fetchCache.get(url);\n if (existing) {\n // Refresh insertion order so Map acts as an LRU cache\n fetchCache.delete(url);\n fetchCache.set(url, existing);\n return existing as Promise<T>;\n }\n\n const promise = fetchRSC<T>(url, options).catch((error) => {\n fetchCache.delete(url);\n\n throw error;\n });\n\n fetchCache.set(url, promise);\n\n // keep the cache bounded, removing the least-recently-used entries\n while (fetchCache.size > MAX_CACHE_SIZE) {\n const firstKey = fetchCache.keys().next().value;\n\n if (!firstKey) {\n break;\n }\n\n fetchCache.delete(firstKey);\n }\n\n return promise;\n}\n\nexport const fetchCache = new Map<string | URL | Request, Promise<unknown>>();\n\nexport async function prefetch(\n pathname: string,\n routes: RouteData[],\n signal?: AbortSignal\n) {\n const path = getPathData(pathname, routes);\n\n if (!path) {\n return;\n }\n\n try {\n await fetchRSCWithPreloadCache<ReactNode>(path.rsc, { signal });\n } catch (error) {\n // don't throw or log if the signal is aborted - this is expected\n if (\n signal?.aborted ||\n (error instanceof Error && error.name === \"AbortError\")\n ) {\n return;\n }\n\n if (error instanceof Error) {\n console.error(`Error prefetching ${pathname} RSC`, error);\n return;\n }\n }\n}\n"],"mappings":";;;;;AASA,eAAsB,SACpB,UACA,QAEA,QACA,EAAE,OAAO,SAA6B,CAAC,GACvC;CACA,IAAI,aAAa;CAEjB,MAAM,OAAO,YAAY,UAAU,MAAM;CAEzC,IAAI,CAAC,MAAM;EACT,QAAQ,KAAK,sBAAsB,UAAU;EAE7C,IAAI,OAAO,MAAM,MAAM,EAAE,SAAS,MAAM,GAEtC,SAAc,QAAQ,cAAc;GAElC,OAAO,QAAQ;EACjB,CAAC;EAGH;CACF;CAGA,IAAI,KAAK,QAAQ,SAAS,UACxB,aAAa;CAGf,IAAI;EACF,MAAM,OAAO,MAAM,yBAAoC,KAAK,GAAG;EAE/D,IAAI,YACF,OAAO,KAAK,GAAG;EAGjB,WAAW,IAAI;CAKjB,SAAS,GAAG;EACV,QAAQ,MAAM,kBAAkB,KAAK,KAAK,OAAO,CAAC;CACpD;AACF;AAIA,eAAe,yBACb,KACA,SACY;CACZ,MAAM,WAAW,WAAW,IAAI,GAAG;CACnC,IAAI,UAAU;EAEZ,WAAW,OAAO,GAAG;EACrB,WAAW,IAAI,KAAK,QAAQ;EAC5B,OAAO;CACT;CAEA,MAAM,UAAU,SAAY,KAAK,OAAO,CAAC,CAAC,OAAO,UAAU;EACzD,WAAW,OAAO,GAAG;EAErB,MAAM;CACR,CAAC;CAED,WAAW,IAAI,KAAK,OAAO;CAG3B,OAAO,WAAW,OAAA,IAAuB;EACvC,MAAM,WAAW,WAAW,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;EAE1C,IAAI,CAAC,UACH;EAGF,WAAW,OAAO,QAAQ;CAC5B;CAEA,OAAO;AACT;AAEA,MAAa,6BAAa,IAAI,IAA8C;AAE5E,eAAsB,SACpB,UACA,QACA,QACA;CACA,MAAM,OAAO,YAAY,UAAU,MAAM;CAEzC,IAAI,CAAC,MACH;CAGF,IAAI;EACF,MAAM,yBAAoC,KAAK,KAAK,EAAE,OAAO,CAAC;CAChE,SAAS,OAAO;EAEd,KAAA,WAAA,QAAA,WAAA,KAAA,IAAA,KAAA,IACE,OAAQ,YACP,iBAAiB,SAAS,MAAM,SAAS,cAE1C;EAGF,IAAI,iBAAiB,OAAO;GAC1B,QAAQ,MAAM,qBAAqB,SAAS,OAAO,KAAK;GACxD;EACF;CACF;AACF"}
|