@renr/parcel-rsc-router 1.0.0-alpha.3 → 1.0.0-alpha.6
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.ts → Link.d.mts} +2 -2
- package/dist/esm/Link.d.mts.map +1 -0
- package/dist/esm/{Link.js → Link.mjs} +4 -7
- package/dist/esm/Link.mjs.map +1 -0
- package/dist/esm/{Router.d.ts → Router.d.mts} +2 -2
- package/dist/esm/Router.d.mts.map +1 -0
- package/dist/esm/{Router.js → Router.mjs} +5 -8
- package/dist/esm/Router.mjs.map +1 -0
- package/dist/esm/{client-entry.js → client-entry.mjs} +2 -5
- package/dist/esm/client-entry.mjs.map +1 -0
- package/dist/esm/{history.js → history.mjs} +2 -4
- package/dist/esm/history.mjs.map +1 -0
- package/dist/esm/{index.d.ts → index.d.mts} +3 -3
- package/dist/esm/{index.js → index.mjs} +3 -4
- package/dist/esm/{rsc.js → rsc.mjs} +4 -7
- package/dist/esm/rsc.mjs.map +1 -0
- package/dist/esm/{types.d.ts → types.d.mts} +1 -1
- package/dist/esm/types.d.mts.map +1 -0
- package/dist/esm/{util.js → util.mjs} +3 -5
- package/dist/esm/util.mjs.map +1 -0
- package/package.json +2 -2
- package/dist/esm/Link.d.ts.map +0 -1
- package/dist/esm/Link.js.map +0 -1
- package/dist/esm/Router.d.ts.map +0 -1
- package/dist/esm/Router.js.map +0 -1
- package/dist/esm/client-entry.js.map +0 -1
- package/dist/esm/history.js.map +0 -1
- package/dist/esm/rsc.js.map +0 -1
- package/dist/esm/types.d.ts.map +0 -1
- package/dist/esm/util.js.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RoutePath } from "./types.
|
|
1
|
+
import { RoutePath } from "./types.mjs";
|
|
2
2
|
import React, { HTMLAttributeAnchorTarget } from "react";
|
|
3
3
|
|
|
4
4
|
//#region src/Link.d.ts
|
|
@@ -20,4 +20,4 @@ declare function Link({
|
|
|
20
20
|
} & React.AnchorHTMLAttributes<HTMLAnchorElement>): React.JSX.Element;
|
|
21
21
|
//#endregion
|
|
22
22
|
export { Link };
|
|
23
|
-
//# sourceMappingURL=Link.d.
|
|
23
|
+
//# sourceMappingURL=Link.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Link.d.mts","names":[],"sources":["../../src/Link.tsx"],"mappings":";;;;iBAMgB,IAAA,CAAA;EACd,EAAA;EACA,QAAA;EACA,IAAA;EACA,MAAA;EACA,MAAA;EACA,OAAA;EAAA,GACG;AAAA;EAEH,EAAA,EAAI,SAAA;EACJ,IAAA;EACA,MAAA,GAAS,MAAA;EACT,QAAA,EAAU,KAAA,CAAM,SAAA;EAChB,OAAA,IAAW,CAAA,EAAG,KAAA,CAAM,UAAA,CAAW,iBAAA;EAC/B,MAAA,GAAS,yBAAA;AAAA,IACP,KAAA,CAAM,oBAAA,CAAqB,iBAAA,IAAkB,KAAA,CAAA,GAAA,CAAA,OAAA"}
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { useRouter } from "./Router.js";
|
|
2
|
+
import { useRouter } from "./Router.mjs";
|
|
5
3
|
import React from "react";
|
|
6
|
-
|
|
7
4
|
//#region src/Link.tsx
|
|
8
|
-
function Link({ to, children, hash, target, search, onClick
|
|
5
|
+
function Link({ to, children, hash, target, search, onClick, ...props }) {
|
|
9
6
|
const { navigate } = useRouter();
|
|
10
7
|
const href = `${to}${hash ? `#${hash}` : ""}${search ? `?${new URLSearchParams(search)}` : ""}`;
|
|
11
8
|
return /* @__PURE__ */ React.createElement("a", {
|
|
@@ -20,7 +17,7 @@ function Link({ to, children, hash, target, search, onClick,...props }) {
|
|
|
20
17
|
...props
|
|
21
18
|
}, children);
|
|
22
19
|
}
|
|
23
|
-
|
|
24
20
|
//#endregion
|
|
25
21
|
export { Link };
|
|
26
|
-
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=Link.mjs.map
|
|
@@ -0,0 +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 ...props\n}: {\n to: RoutePath;\n hash?: string;\n search?: Record<string, string>;\n children: React.ReactNode;\n onClick?: (e: React.MouseEvent<HTMLAnchorElement>) => void;\n target?: HTMLAttributeAnchorTarget;\n} & React.AnchorHTMLAttributes<HTMLAnchorElement>) {\n const { navigate } = useRouter();\n const href = `${to}${hash ? `#${hash}` : \"\"}${search ? `?${new URLSearchParams(search)}` : \"\"}`;\n\n return (\n <a\n href={href}\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,SAAgB,KAAK,EACnB,IACA,UACA,MACA,QACA,QACA,SACA,GAAG,SAQ8C;CACjD,MAAM,EAAE,aAAa,WAAW;CAChC,MAAM,OAAO,GAAG,KAAK,OAAO,IAAI,SAAS,KAAK,SAAS,IAAI,IAAI,gBAAgB,OAAO,KAAK;CAE3F,OACE,sBAAA,cAAC,KAAD;EACQ;EACN,UAAU,MAAM;GACd,IAAI,SACF,QAAQ,EAAE;GAGZ,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,gBAAgB;IAClB,SAAS,KAAK;;;EAGlB,GAAI;EAGF,EADD,SACC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RouteData, RoutePath } from "./types.
|
|
1
|
+
import { RouteData, RoutePath } from "./types.mjs";
|
|
2
2
|
import * as React$1 from "react";
|
|
3
3
|
|
|
4
4
|
//#region src/Router.d.ts
|
|
@@ -22,4 +22,4 @@ declare const RouterProvider: ({
|
|
|
22
22
|
}) => React$1.JSX.Element;
|
|
23
23
|
//#endregion
|
|
24
24
|
export { RouterProvider, useCurrentRoute, useLocation, useRouter };
|
|
25
|
-
//# sourceMappingURL=Router.d.
|
|
25
|
+
//# sourceMappingURL=Router.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Router.d.mts","names":[],"sources":["../../src/Router.tsx"],"mappings":";;;;iBAuBgB,SAAA,CAAA;EAAS,QAAA,GAHZ,EAAA;AAAA;AAAA,iBAaG,WAAA,CAAA,GAAW,QAAA;AAAA,iBAQX,eAAA,CAAA;;;QAAe,SAAA;;;;cAelB,cAAA;EAAkB,QAAA;EAAA;AAAA;EAI7B,QAAA,EAAU,OAAA,CAAM,SAAA;EAChB,MAAA,EAAQ,SAAA;AAAA,MACT,OAAA,CAAA,GAAA,CAAA,OAAA"}
|
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import { getPathData } from "./util.js";
|
|
6
|
-
import { navigate } from "./rsc.js";
|
|
2
|
+
import { createHistory } from "./history.mjs";
|
|
3
|
+
import { getPathData } from "./util.mjs";
|
|
4
|
+
import { navigate } from "./rsc.mjs";
|
|
7
5
|
import * as React$1 from "react";
|
|
8
6
|
import { useSyncExternalStore } from "react";
|
|
9
|
-
|
|
10
7
|
//#region src/Router.tsx
|
|
11
8
|
const RoutesContext = React$1.createContext(null);
|
|
12
9
|
const useRoutes = () => {
|
|
@@ -82,7 +79,7 @@ const RouterProvider = ({ children, routes }) => {
|
|
|
82
79
|
]);
|
|
83
80
|
return /* @__PURE__ */ React$1.createElement(RoutesContext.Provider, { value: routes }, /* @__PURE__ */ React$1.createElement(LocationContext.Provider, { value: location }, /* @__PURE__ */ React$1.createElement(RouterContext.Provider, { value: routerContextValue }, children)));
|
|
84
81
|
};
|
|
85
|
-
|
|
86
82
|
//#endregion
|
|
87
83
|
export { RouterProvider, useCurrentRoute, useLocation, useRouter };
|
|
88
|
-
|
|
84
|
+
|
|
85
|
+
//# sourceMappingURL=Router.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Router.mjs","names":["React","navigate","rscNavigate"],"sources":["../../src/Router.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\nimport { useSyncExternalStore } from \"react\";\nimport { createHistory } from \"./history.js\";\nimport { navigate as rscNavigate } from \"./rsc.js\";\nimport { RouteData } from \"./types.js\";\nimport { getPathData } from \"./util.js\";\n\nconst RoutesContext = React.createContext<RouteData[] | null>(null);\n\nexport const useRoutes = () => {\n const routes = React.useContext(RoutesContext);\n if (routes === null) {\n throw new Error(\"useRoutes must be used within a RouterProvider\");\n }\n return routes;\n};\n\nconst RouterContext = React.createContext<{\n navigate: (to: string) => void;\n} | null>(null);\n\nexport function useRouter() {\n const context = React.useContext(RouterContext);\n if (context === null) {\n throw new Error(\"useRouter must be used within a RouterProvider\");\n }\n return context;\n}\n\nconst LocationContext = React.createContext<Location | null>(null);\n\nexport function useLocation() {\n const context = React.useContext(LocationContext);\n if (context === null) {\n throw new Error(\"useLocation must be used within a RouterProvider\");\n }\n return context;\n}\n\nexport function useCurrentRoute() {\n const location = useLocation();\n const routes = useRoutes();\n return getPathData(location.pathname, routes);\n}\n\nconst history = createHistory();\n\nconst ssrSnapshot = {\n location: null,\n state: null,\n};\n\nconst getSsrSnapshot = () => ssrSnapshot;\n\nexport const RouterProvider = ({\n children,\n routes,\n}: {\n children: React.ReactNode;\n routes: RouteData[];\n}) => {\n // subscribe to history changes\n const { location, state } = useSyncExternalStore(\n history.subscribe,\n history.getSnapshot,\n getSsrSnapshot\n );\n\n const navigate = React.useCallback(\n (to: string) => {\n if (!location?.href) {\n throw new Error(\"Location is not available\");\n }\n\n if (location.href === new URL(to, location.href).href) {\n return;\n }\n\n // save scroll position for the current entry\n history.replace(location.href, {\n ...history.snapshot.state,\n key: history.snapshot.state?.key ?? Math.random().toString(36).slice(2),\n scrollX: window.scrollX,\n scrollY: window.scrollY,\n });\n\n void rscNavigate(to, routes, (url) => {\n history.push(url, {\n key: Math.random().toString(36).slice(2),\n scrollX: 0,\n scrollY: 0,\n });\n });\n },\n [routes, location?.href]\n );\n\n const routerContextValue = React.useMemo(() => ({ navigate }), [navigate]);\n\n React.useEffect(() => {\n const handlePopState = () => {\n void rscNavigate(\n window.location.pathname,\n routes,\n () => {\n // noop, history is already updated\n },\n { push: false }\n );\n };\n\n window.addEventListener(\"popstate\", handlePopState);\n return () => window.removeEventListener(\"popstate\", handlePopState);\n }, [routes]);\n\n React.useLayoutEffect(() => {\n // hash scrolling takes priority\n if (location?.hash) {\n const element = document.querySelector(location.hash);\n if (element) {\n element.scrollIntoView();\n return;\n }\n }\n\n // otherwise use saved scroll position\n if (state?.scrollY !== undefined) {\n window.scrollTo(state.scrollX, state.scrollY);\n } else {\n window.scrollTo(0, 0);\n }\n }, [location?.pathname, location?.hash, state]);\n\n return (\n <RoutesContext.Provider value={routes}>\n <LocationContext.Provider value={location}>\n <RouterContext.Provider value={routerContextValue}>\n {children}\n </RouterContext.Provider>\n </LocationContext.Provider>\n </RoutesContext.Provider>\n );\n};\n"],"mappings":"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"}
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"use client-entry";
|
|
2
|
-
|
|
3
|
-
|
|
4
2
|
import { hydrate } from "@parcel/rsc/client";
|
|
5
|
-
|
|
6
3
|
//#region src/client-entry.ts
|
|
7
4
|
const updateRoot = hydrate({});
|
|
8
|
-
|
|
9
5
|
//#endregion
|
|
10
6
|
export { updateRoot };
|
|
11
|
-
|
|
7
|
+
|
|
8
|
+
//# sourceMappingURL=client-entry.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-entry.mjs","names":[],"sources":["../../src/client-entry.ts"],"sourcesContent":["\"use client-entry\";\n\nimport { hydrate } from \"@parcel/rsc/client\";\n\n//TODO https://github.com/microsoft/TypeScript/issues/42873#issuecomment-1416128545\nexport const updateRoot: ReturnType<typeof hydrate> = hydrate({\n // TODO - can't build due to reading window\n // onHmrReload() {},\n});\n"],"mappings":";;;AAKA,MAAa,aAAyC,QAAQ,EAG7D,CAAC"}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
|
|
3
|
-
|
|
4
2
|
//#region src/history.ts
|
|
5
3
|
function createHistory() {
|
|
6
4
|
const listeners = /* @__PURE__ */ new Set();
|
|
@@ -36,7 +34,7 @@ function createHistory() {
|
|
|
36
34
|
}
|
|
37
35
|
};
|
|
38
36
|
}
|
|
39
|
-
|
|
40
37
|
//#endregion
|
|
41
38
|
export { createHistory };
|
|
42
|
-
|
|
39
|
+
|
|
40
|
+
//# sourceMappingURL=history.mjs.map
|
|
@@ -0,0 +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 const newSnapshot = {\n location: window.location,\n state: window.history.state as HistoryState | null,\n };\n // Only update currentSnapshot if something actually changed to prevent infinite loops\n if (\n !currentSnapshot ||\n currentSnapshot.location !== newSnapshot.location ||\n currentSnapshot.state !== newSnapshot.state\n ) {\n currentSnapshot = newSnapshot;\n }\n return currentSnapshot;\n };\n\n if (typeof window !== \"undefined\") {\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 window.history.pushState(state, \"\", url);\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 window.history.replaceState(state, \"\", url);\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,KAAiB;CACvC,IAAI;CAKJ,MAAM,oBAAoB;EACxB,MAAM,cAAc;GAClB,UAAU,OAAO;GACjB,OAAO,OAAO,QAAQ;GACvB;EAED,IACE,CAAC,mBACD,gBAAgB,aAAa,YAAY,YACzC,gBAAgB,UAAU,YAAY,OAEtC,kBAAkB;EAEpB,OAAO;;CAGT,IAAI,OAAO,WAAW,aACpB,OAAO,iBAAiB,kBAAkB;EAExC,qBAAqB,UAAU,SAAS,aAAa,UAAU,CAAC,CAAC;GACjE;CAGJ,OAAO;EACL,IAAI,WAAW;GACb,OAAO,aAAa;;EAGtB;EAEA,UAAsB,UAAsB;GAC1C,UAAU,IAAI,SAAS;GACvB,aAAa;IACX,UAAU,OAAO,SAAS;;;EAI9B,KAAK,KAAa,OAAqB;GACrC,OAAO,QAAQ,UAAU,OAAO,IAAI,IAAI;GAExC,qBAAqB,UAAU,SAAS,aAAa,UAAU,CAAC,CAAC;;EAGnE,QAAQ,KAAa,OAAqB;GACxC,OAAO,QAAQ,aAAa,OAAO,IAAI,IAAI;;EAG9C"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { App, RouteData, RouteNode, RoutePath } from "./types.
|
|
2
|
-
import { Link } from "./Link.
|
|
3
|
-
import { RouterProvider, useCurrentRoute, useLocation, useRouter } from "./Router.
|
|
1
|
+
import { App, RouteData, RouteNode, RoutePath } from "./types.mjs";
|
|
2
|
+
import { Link } from "./Link.mjs";
|
|
3
|
+
import { RouterProvider, useCurrentRoute, useLocation, useRouter } from "./Router.mjs";
|
|
4
4
|
export { type App, Link, type RouteData, type RouteNode, type RoutePath, RouterProvider, useCurrentRoute, useLocation, useRouter };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { RouterProvider, useCurrentRoute, useLocation, useRouter } from "./Router.
|
|
2
|
-
import { Link } from "./Link.
|
|
3
|
-
|
|
4
|
-
export { Link, RouterProvider, useCurrentRoute, useLocation, useRouter };
|
|
1
|
+
import { RouterProvider, useCurrentRoute, useLocation, useRouter } from "./Router.mjs";
|
|
2
|
+
import { Link } from "./Link.mjs";
|
|
3
|
+
export { Link, RouterProvider, useCurrentRoute, useLocation, useRouter };
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { updateRoot } from "./client-entry.js";
|
|
5
|
-
import { getPathData } from "./util.js";
|
|
2
|
+
import { updateRoot } from "./client-entry.mjs";
|
|
3
|
+
import { getPathData } from "./util.mjs";
|
|
6
4
|
import { fetchRSC } from "@parcel/rsc/client";
|
|
7
|
-
|
|
8
5
|
//#region src/rsc.ts
|
|
9
6
|
async function navigate(pathname, routes, onPush, { push = true } = {}) {
|
|
10
7
|
console.debug("client navigating to:", pathname);
|
|
@@ -46,7 +43,7 @@ async function fetchRSCWithPreloadCache(url, options) {
|
|
|
46
43
|
});
|
|
47
44
|
}
|
|
48
45
|
const fetchCache = /* @__PURE__ */ new Map();
|
|
49
|
-
|
|
50
46
|
//#endregion
|
|
51
47
|
export { navigate };
|
|
52
|
-
|
|
48
|
+
|
|
49
|
+
//# sourceMappingURL=rsc.mjs.map
|
|
@@ -0,0 +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 console.debug(\"client navigating to:\", pathname);\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 updateRoot(root, () => {\n if (shouldPush) {\n onPush(path.url);\n }\n });\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.log(`Error fetching ${path.path} RSC`, e);\n }\n}\n\nasync function fetchRSCWithPreloadCache<T>(\n url: string | URL | Request,\n options?: RSCRequestInit\n): Promise<T> {\n if (fetchCache.has(url)) {\n console.log(\"Cache hit\", url);\n\n return fetchCache.get(url) as Promise<T>;\n }\n\n const promise = fetchRSC<T>(url, options);\n\n fetchCache.set(url, promise);\n\n // keep the cache small, removing the oldest entries\n while (fetchCache.size > 5) {\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 .then((data) => {\n return data;\n })\n .catch((error) => {\n fetchCache.delete(url);\n\n throw error;\n });\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 if (error instanceof Error) {\n console.error(`Error fetching ${pathname} RSC`, error);\n\n return;\n }\n\n // don't throw if the signal is aborted - this is expected\n if (signal?.aborted) {\n console.log(signal.reason);\n\n return;\n }\n }\n}\n"],"mappings":";;;;;AASA,eAAsB,SACpB,UACA,QAEA,QACA,EAAE,OAAO,SAA6B,EAAE,EACxC;CACA,QAAQ,MAAM,yBAAyB,SAAS;CAEhD,IAAI,aAAa;CAEjB,MAAM,OAAO,YAAY,UAAU,OAAO;CAE1C,IAAI,CAAC,MAAM;EACT,QAAQ,KAAK,sBAAsB,WAAW;EAE9C,IAAI,OAAO,MAAM,MAAM,EAAE,SAAS,OAAO,EAEvC,SAAc,QAAQ,cAAc;GAElC,OAAO,SAAS;IAChB;EAGJ;;CAIF,IAAI,KAAK,QAAQ,SAAS,UACxB,aAAa;CAGf,IAAI;EAGF,WAAW,MAFQ,yBAAoC,KAAK,IAAI,QAEzC;GACrB,IAAI,YACF,OAAO,KAAK,IAAI;IAElB;UAKK,GAAG;EACV,QAAQ,IAAI,kBAAkB,KAAK,KAAK,OAAO,EAAE;;;AAIrD,eAAe,yBACb,KACA,SACY;CACZ,IAAI,WAAW,IAAI,IAAI,EAAE;EACvB,QAAQ,IAAI,aAAa,IAAI;EAE7B,OAAO,WAAW,IAAI,IAAI;;CAG5B,MAAM,UAAU,SAAY,KAAK,QAAQ;CAEzC,WAAW,IAAI,KAAK,QAAQ;CAG5B,OAAO,WAAW,OAAO,GAAG;EAC1B,MAAM,WAAW,WAAW,MAAM,CAAC,MAAM,CAAC;EAE1C,IAAI,CAAC,UACH;EAGF,WAAW,OAAO,SAAS;;CAG7B,OAAO,QACJ,MAAM,SAAS;EACd,OAAO;GACP,CACD,OAAO,UAAU;EAChB,WAAW,OAAO,IAAI;EAEtB,MAAM;GACN;;AAGN,MAAa,6BAAa,IAAI,KAA+C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.mts","names":[],"sources":["../../src/types.ts"],"mappings":";kBAAiB,GAAA;EAAA,UACE,MAAA;AAAA;AAAA,KAGP,SAAA,SAAkB,GAAA,CAAI,MAAA,gCAExB,GAAA,CAAI,MAAA;AAAA,KAEF,SAAA,GAAY,IAAA,CAAK,SAAA;AAAA,KAExB,eAAA,qBAAoC,CAAA,yBAErC,CAAA,4CACE,eAAA,CAAgB,IAAA,IAChB,CAAA;AAAA,UAEW,SAAA;EACf,QAAA,EAAU,SAAA;EACV,IAAA,EAAM,SAAA;EACN,IAAA,EAAM,eAAA,CAAgB,SAAA;EACtB,GAAA;EACA,IAAA;AAAA"}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
|
|
3
|
-
|
|
4
2
|
//#region src/util.ts
|
|
5
3
|
const normalizeRoutePath = (path) => {
|
|
6
4
|
for (const suffix of ["/", "/index"]) if (path.endsWith(suffix)) return path.slice(0, -suffix.length) || "/";
|
|
@@ -9,7 +7,7 @@ const normalizeRoutePath = (path) => {
|
|
|
9
7
|
function getPathData(pathname, routes) {
|
|
10
8
|
const normalizedPath = normalizeRoutePath(pathname.startsWith("/") ? pathname : `/${pathname}`);
|
|
11
9
|
const routePathName = normalizeRoutePath(new URL(pathname, window.location.origin).pathname);
|
|
12
|
-
const route = routes.find((route
|
|
10
|
+
const route = routes.find((route) => route.path === routePathName);
|
|
13
11
|
if (!route) return null;
|
|
14
12
|
return {
|
|
15
13
|
...route,
|
|
@@ -17,7 +15,7 @@ function getPathData(pathname, routes) {
|
|
|
17
15
|
url: normalizedPath
|
|
18
16
|
};
|
|
19
17
|
}
|
|
20
|
-
|
|
21
18
|
//#endregion
|
|
22
19
|
export { getPathData };
|
|
23
|
-
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=util.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.mjs","names":[],"sources":["../../src/util.ts"],"sourcesContent":["\"use client\";\nimport { RouteData } from \"./types.js\";\n\n// remove trailing slashes and index from the path\nexport const normalizeRoutePath = (path: string) => {\n for (const suffix of [\"/\", \"/index\"]) {\n if (path.endsWith(suffix)) {\n return path.slice(0, -suffix.length) || \"/\";\n }\n }\n\n return path;\n};\n\n// convert a path back to a route path\nexport const denormalizeRoutePath = (path: string) => {\n if (path === \"/\") {\n return \"index\";\n }\n\n if (path.startsWith(\"/\")) {\n return path.substring(1);\n }\n\n return path;\n};\n\nexport function getPathData(pathname: string, routes: RouteData[]) {\n const path = pathname.startsWith(\"/\") ? pathname : `/${pathname}`;\n\n // path for the URL (including query string/hash)\n const normalizedPath = normalizeRoutePath(path);\n\n // we want the pathname without the query string/hash to match the route\n const url = new URL(pathname, window.location.origin);\n\n const routePathName = normalizeRoutePath(url.pathname);\n\n const route = routes.find((route) => route.path === routePathName);\n\n if (!route) {\n return null;\n }\n\n return {\n ...route,\n rsc: route.rsc,\n url: normalizedPath,\n };\n}\n"],"mappings":";;AAIA,MAAa,sBAAsB,SAAiB;CAClD,KAAK,MAAM,UAAU,CAAC,KAAK,SAAS,EAClC,IAAI,KAAK,SAAS,OAAO,EACvB,OAAO,KAAK,MAAM,GAAG,CAAC,OAAO,OAAO,IAAI;CAI5C,OAAO;;AAgBT,SAAgB,YAAY,UAAkB,QAAqB;CAIjE,MAAM,iBAAiB,mBAHV,SAAS,WAAW,IAAI,GAAG,WAAW,IAAI,WAGR;CAK/C,MAAM,gBAAgB,mBAAmB,IAFzB,IAAI,UAAU,OAAO,SAAS,OAEF,CAAC,SAAS;CAEtD,MAAM,QAAQ,OAAO,MAAM,UAAU,MAAM,SAAS,cAAc;CAElE,IAAI,CAAC,OACH,OAAO;CAGT,OAAO;EACL,GAAG;EACH,KAAK,MAAM;EACX,KAAK;EACN"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@renr/parcel-rsc-router",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.6",
|
|
4
4
|
"author": "Ren Rizzolo",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Parcel rsc router",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"react-server-dom-parcel": "^19"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"react": "^19.2.
|
|
32
|
+
"react": "^19.2.3",
|
|
33
33
|
"@types/react": "^19.2.2",
|
|
34
34
|
"@parcel/rsc": "^2.16.1"
|
|
35
35
|
},
|
package/dist/esm/Link.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Link.d.ts","names":[],"sources":["../../src/Link.tsx"],"sourcesContent":[],"mappings":";;;;iBAMgB,IAAA;;;;;;;;AAKd;MAII;EATU,IAAA,CAAA,EAAI,MAAA;EAClB,MAAA,CAAA,EAUS,MAVT,CAAA,MAAA,EAAA,MAAA,CAAA;EACA,QAAA,EAUU,KAAA,CAAM,SAVhB;EACA,OAAA,CAAA,EAAA,CAAA,CAAA,EAUc,KAAA,CAAM,UAVpB,CAU+B,iBAV/B,CAAA,EAAA,GAAA,IAAA;EACA,MAAA,CAAA,EAUS,yBAVT;CACA,GAUE,KAAA,CAAM,oBAVR,CAU6B,iBAV7B,CAAA,CAAA,EAU+C,KAAA,CAAA,GAAA,CAAA,OAV/C"}
|
package/dist/esm/Link.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Link.js","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 ...props\n}: {\n to: RoutePath;\n hash?: string;\n search?: Record<string, string>;\n children: React.ReactNode;\n onClick?: (e: React.MouseEvent<HTMLAnchorElement>) => void;\n target?: HTMLAttributeAnchorTarget;\n} & React.AnchorHTMLAttributes<HTMLAnchorElement>) {\n const { navigate } = useRouter();\n const href = `${to}${hash ? `#${hash}` : \"\"}${search ? `?${new URLSearchParams(search)}` : \"\"}`;\n\n return (\n <a\n href={href}\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,SAAgB,KAAK,EACnB,IACA,UACA,MACA,QACA,QACA,QACA,GAAG,SAQ8C;CACjD,MAAM,EAAE,aAAa,WAAW;CAChC,MAAM,OAAO,GAAG,KAAK,OAAO,IAAI,SAAS,KAAK,SAAS,IAAI,IAAI,gBAAgB,OAAO,KAAK;AAE3F,QACE,oCAAC;EACO;EACN,UAAU,MAAM;AACd,OAAI,QACF,SAAQ,EAAE;AAGZ,QACG,CAAC,UAAU,WAAW,YACvB,EAAE,WAAW,KACb,CAAC,EAAE,WACH,CAAC,EAAE,WACH,CAAC,EAAE,UACH,CAAC,EAAE,YACH,CAAC,EAAE,kBACH;AAEA,MAAE,gBAAgB;AAClB,aAAS,KAAK;;;EAGlB,GAAI;IAEH,SACC"}
|
package/dist/esm/Router.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Router.d.ts","names":[],"sources":["../../src/Router.tsx"],"sourcesContent":[],"mappings":";;;;iBAuBgB,SAAA,CAAA;EAAA,QAAA,EAAA,CAAA,EAAS,EAAA,MAAA,EAAA,GAAA,IAAA;AAUzB,CAAA;AAQgB,iBARA,WAAA,CAAA,CAQe,EARJ,QAQI;AAelB,iBAfG,eAAA,CAAA,CAuGf,EAAA;EAxF8B,GAAA,EAAA,GAAA,MAAA,MAAA;EAAA,GAAA,EAAA,MAAA;EAInB,IAAA,EAnBmB,SAmBb;EACR,IAAA,EAAA,MAAA;EACT,IAAA,EAAA,GAAA,MAAA,OAAA;CAkFA,GAAA,IAAA;cAxFY;;;;YAID,OAAA,CAAM;UACR;MACT,OAAA,CAAA,GAAA,CAAA"}
|
package/dist/esm/Router.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Router.js","names":["React","navigate","rscNavigate"],"sources":["../../src/Router.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\nimport { useSyncExternalStore } from \"react\";\nimport { createHistory } from \"./history.js\";\nimport { navigate as rscNavigate } from \"./rsc.js\";\nimport { RouteData } from \"./types.js\";\nimport { getPathData } from \"./util.js\";\n\nconst RoutesContext = React.createContext<RouteData[] | null>(null);\n\nexport const useRoutes = () => {\n const routes = React.useContext(RoutesContext);\n if (routes === null) {\n throw new Error(\"useRoutes must be used within a RouterProvider\");\n }\n return routes;\n};\n\nconst RouterContext = React.createContext<{\n navigate: (to: string) => void;\n} | null>(null);\n\nexport function useRouter() {\n const context = React.useContext(RouterContext);\n if (context === null) {\n throw new Error(\"useRouter must be used within a RouterProvider\");\n }\n return context;\n}\n\nconst LocationContext = React.createContext<Location | null>(null);\n\nexport function useLocation() {\n const context = React.useContext(LocationContext);\n if (context === null) {\n throw new Error(\"useLocation must be used within a RouterProvider\");\n }\n return context;\n}\n\nexport function useCurrentRoute() {\n const location = useLocation();\n const routes = useRoutes();\n return getPathData(location.pathname, routes);\n}\n\nconst history = createHistory();\n\nconst ssrSnapshot = {\n location: null,\n state: null,\n};\n\nconst getSsrSnapshot = () => ssrSnapshot;\n\nexport const RouterProvider = ({\n children,\n routes,\n}: {\n children: React.ReactNode;\n routes: RouteData[];\n}) => {\n // subscribe to history changes\n const { location, state } = useSyncExternalStore(\n history.subscribe,\n history.getSnapshot,\n getSsrSnapshot\n );\n\n const navigate = React.useCallback(\n (to: string) => {\n if (!location?.href) {\n throw new Error(\"Location is not available\");\n }\n\n if (location.href === new URL(to, location.href).href) {\n return;\n }\n\n // save scroll position for the current entry\n history.replace(location.href, {\n ...history.snapshot.state,\n key: history.snapshot.state?.key ?? Math.random().toString(36).slice(2),\n scrollX: window.scrollX,\n scrollY: window.scrollY,\n });\n\n void rscNavigate(to, routes, (url) => {\n history.push(url, {\n key: Math.random().toString(36).slice(2),\n scrollX: 0,\n scrollY: 0,\n });\n });\n },\n [routes, location?.href]\n );\n\n const routerContextValue = React.useMemo(() => ({ navigate }), [navigate]);\n\n React.useEffect(() => {\n const handlePopState = () => {\n void rscNavigate(\n window.location.pathname,\n routes,\n () => {\n // noop, history is already updated\n },\n { push: false }\n );\n };\n\n window.addEventListener(\"popstate\", handlePopState);\n return () => window.removeEventListener(\"popstate\", handlePopState);\n }, [routes]);\n\n React.useLayoutEffect(() => {\n // hash scrolling takes priority\n if (location?.hash) {\n const element = document.querySelector(location.hash);\n if (element) {\n element.scrollIntoView();\n return;\n }\n }\n\n // otherwise use saved scroll position\n if (state?.scrollY !== undefined) {\n window.scrollTo(state.scrollX, state.scrollY);\n } else {\n window.scrollTo(0, 0);\n }\n }, [location?.pathname, location?.hash, state]);\n\n return (\n <RoutesContext.Provider value={routes}>\n <LocationContext.Provider value={location}>\n <RouterContext.Provider value={routerContextValue}>\n {children}\n </RouterContext.Provider>\n </LocationContext.Provider>\n </RoutesContext.Provider>\n );\n};\n"],"mappings":";;;;;;;;;;AASA,MAAM,gBAAgBA,QAAM,cAAkC,KAAK;AAEnE,MAAa,kBAAkB;CAC7B,MAAM,SAASA,QAAM,WAAW,cAAc;AAC9C,KAAI,WAAW,KACb,OAAM,IAAI,MAAM,iDAAiD;AAEnE,QAAO;;AAGT,MAAM,gBAAgBA,QAAM,cAElB,KAAK;AAEf,SAAgB,YAAY;CAC1B,MAAM,UAAUA,QAAM,WAAW,cAAc;AAC/C,KAAI,YAAY,KACd,OAAM,IAAI,MAAM,iDAAiD;AAEnE,QAAO;;AAGT,MAAM,kBAAkBA,QAAM,cAA+B,KAAK;AAElE,SAAgB,cAAc;CAC5B,MAAM,UAAUA,QAAM,WAAW,gBAAgB;AACjD,KAAI,YAAY,KACd,OAAM,IAAI,MAAM,mDAAmD;AAErE,QAAO;;AAGT,SAAgB,kBAAkB;CAChC,MAAM,WAAW,aAAa;CAC9B,MAAM,SAAS,WAAW;AAC1B,QAAO,YAAY,SAAS,UAAU,OAAO;;AAG/C,MAAM,UAAU,eAAe;AAE/B,MAAM,cAAc;CAClB,UAAU;CACV,OAAO;CACR;AAED,MAAM,uBAAuB;AAE7B,MAAa,kBAAkB,EAC7B,UACA,aAII;CAEJ,MAAM,EAAE,UAAU,UAAU,qBAC1B,QAAQ,WACR,QAAQ,aACR,eACD;CAED,MAAMC,aAAWD,QAAM,aACpB,OAAe;;AACd,MAAI,sDAAC,SAAU,MACb,OAAM,IAAI,MAAM,4BAA4B;AAG9C,MAAI,SAAS,SAAS,IAAI,IAAI,IAAI,SAAS,KAAK,CAAC,KAC/C;AAIF,UAAQ,QAAQ,SAAS,MAAM;GAC7B,GAAG,QAAQ,SAAS;GACpB,wDAAK,QAAQ,SAAS,uFAAO,4EAAO,KAAK,QAAQ,CAAC,SAAS,GAAG,CAAC,MAAM,EAAE;GACvE,SAAS,OAAO;GAChB,SAAS,OAAO;GACjB,CAAC;AAEF,EAAKE,SAAY,IAAI,SAAS,QAAQ;AACpC,WAAQ,KAAK,KAAK;IAChB,KAAK,KAAK,QAAQ,CAAC,SAAS,GAAG,CAAC,MAAM,EAAE;IACxC,SAAS;IACT,SAAS;IACV,CAAC;IACF;IAEJ,CAAC,4DAAQ,SAAU,KAAK,CACzB;CAED,MAAM,qBAAqBF,QAAM,eAAe,EAAE,sBAAU,GAAG,CAACC,WAAS,CAAC;AAE1E,SAAM,gBAAgB;EACpB,MAAM,uBAAuB;AAC3B,GAAKC,SACH,OAAO,SAAS,UAChB,cACM,IAGN,EAAE,MAAM,OAAO,CAChB;;AAGH,SAAO,iBAAiB,YAAY,eAAe;AACnD,eAAa,OAAO,oBAAoB,YAAY,eAAe;IAClE,CAAC,OAAO,CAAC;AAEZ,SAAM,sBAAsB;AAE1B,0DAAI,SAAU,MAAM;GAClB,MAAM,UAAU,SAAS,cAAc,SAAS,KAAK;AACrD,OAAI,SAAS;AACX,YAAQ,gBAAgB;AACxB;;;AAKJ,qDAAI,MAAO,aAAY,OACrB,QAAO,SAAS,MAAM,SAAS,MAAM,QAAQ;MAE7C,QAAO,SAAS,GAAG,EAAE;IAEtB;sDAAC,SAAU;sDAAU,SAAU;EAAM;EAAM,CAAC;AAE/C,QACE,sCAAC,cAAc,YAAS,OAAO,UAC7B,sCAAC,gBAAgB,YAAS,OAAO,YAC/B,sCAAC,cAAc,YAAS,OAAO,sBAC5B,SACsB,CACA,CACJ"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client-entry.js","names":["updateRoot: ReturnType<typeof hydrate>"],"sources":["../../src/client-entry.ts"],"sourcesContent":["\"use client-entry\";\n\nimport { hydrate } from \"@parcel/rsc/client\";\n\n//TODO https://github.com/microsoft/TypeScript/issues/42873#issuecomment-1416128545\nexport const updateRoot: ReturnType<typeof hydrate> = hydrate({\n // TODO - can't build due to reading window\n // onHmrReload() {},\n});\n"],"mappings":";;;;;;AAKA,MAAaA,aAAyC,QAAQ,EAG7D,CAAC"}
|
package/dist/esm/history.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"history.js","names":["currentSnapshot: {\n location: Location | null;\n state: HistoryState | null;\n }"],"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 const newSnapshot = {\n location: window.location,\n state: window.history.state as HistoryState | null,\n };\n // Only update currentSnapshot if something actually changed to prevent infinite loops\n if (\n !currentSnapshot ||\n currentSnapshot.location !== newSnapshot.location ||\n currentSnapshot.state !== newSnapshot.state\n ) {\n currentSnapshot = newSnapshot;\n }\n return currentSnapshot;\n };\n\n if (typeof window !== \"undefined\") {\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 window.history.pushState(state, \"\", url);\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 window.history.replaceState(state, \"\", url);\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,KAAiB;CACvC,IAAIA;CAKJ,MAAM,oBAAoB;EACxB,MAAM,cAAc;GAClB,UAAU,OAAO;GACjB,OAAO,OAAO,QAAQ;GACvB;AAED,MACE,CAAC,mBACD,gBAAgB,aAAa,YAAY,YACzC,gBAAgB,UAAU,YAAY,MAEtC,mBAAkB;AAEpB,SAAO;;AAGT,KAAI,OAAO,WAAW,YACpB,QAAO,iBAAiB,kBAAkB;AAExC,uBAAqB,UAAU,SAAS,aAAa,UAAU,CAAC,CAAC;GACjE;AAGJ,QAAO;EACL,IAAI,WAAW;AACb,UAAO,aAAa;;EAGtB;EAEA,UAAsB,UAAsB;AAC1C,aAAU,IAAI,SAAS;AACvB,gBAAa;AACX,cAAU,OAAO,SAAS;;;EAI9B,KAAK,KAAa,OAAqB;AACrC,UAAO,QAAQ,UAAU,OAAO,IAAI,IAAI;AAExC,wBAAqB,UAAU,SAAS,aAAa,UAAU,CAAC,CAAC;;EAGnE,QAAQ,KAAa,OAAqB;AACxC,UAAO,QAAQ,aAAa,OAAO,IAAI,IAAI;;EAG9C"}
|
package/dist/esm/rsc.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rsc.js","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 console.debug(\"client navigating to:\", pathname);\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 updateRoot(root, () => {\n if (shouldPush) {\n onPush(path.url);\n }\n });\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.log(`Error fetching ${path.path} RSC`, e);\n }\n}\n\nasync function fetchRSCWithPreloadCache<T>(\n url: string | URL | Request,\n options?: RSCRequestInit\n): Promise<T> {\n if (fetchCache.has(url)) {\n console.log(\"Cache hit\", url);\n\n return fetchCache.get(url) as Promise<T>;\n }\n\n const promise = fetchRSC<T>(url, options);\n\n fetchCache.set(url, promise);\n\n // keep the cache small, removing the oldest entries\n while (fetchCache.size > 5) {\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 .then((data) => {\n return data;\n })\n .catch((error) => {\n fetchCache.delete(url);\n\n throw error;\n });\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 if (error instanceof Error) {\n console.error(`Error fetching ${pathname} RSC`, error);\n\n return;\n }\n\n // don't throw if the signal is aborted - this is expected\n if (signal?.aborted) {\n console.log(signal.reason);\n\n return;\n }\n }\n}\n"],"mappings":";;;;;;;;AASA,eAAsB,SACpB,UACA,QAEA,QACA,EAAE,OAAO,SAA6B,EAAE,EACxC;AACA,SAAQ,MAAM,yBAAyB,SAAS;CAEhD,IAAI,aAAa;CAEjB,MAAM,OAAO,YAAY,UAAU,OAAO;AAE1C,KAAI,CAAC,MAAM;AACT,UAAQ,KAAK,sBAAsB,WAAW;AAE9C,MAAI,OAAO,MAAM,MAAM,EAAE,SAAS,OAAO,CAEvC,CAAK,SAAS,QAAQ,cAAc;AAElC,UAAO,SAAS;IAChB;AAGJ;;AAIF,KAAI,KAAK,QAAQ,SAAS,SACxB,cAAa;AAGf,KAAI;AAGF,aAFa,MAAM,yBAAoC,KAAK,IAAI,QAEzC;AACrB,OAAI,WACF,QAAO,KAAK,IAAI;IAElB;UAKK,GAAG;AACV,UAAQ,IAAI,kBAAkB,KAAK,KAAK,OAAO,EAAE;;;AAIrD,eAAe,yBACb,KACA,SACY;AACZ,KAAI,WAAW,IAAI,IAAI,EAAE;AACvB,UAAQ,IAAI,aAAa,IAAI;AAE7B,SAAO,WAAW,IAAI,IAAI;;CAG5B,MAAM,UAAU,SAAY,KAAK,QAAQ;AAEzC,YAAW,IAAI,KAAK,QAAQ;AAG5B,QAAO,WAAW,OAAO,GAAG;EAC1B,MAAM,WAAW,WAAW,MAAM,CAAC,MAAM,CAAC;AAE1C,MAAI,CAAC,SACH;AAGF,aAAW,OAAO,SAAS;;AAG7B,QAAO,QACJ,MAAM,SAAS;AACd,SAAO;GACP,CACD,OAAO,UAAU;AAChB,aAAW,OAAO,IAAI;AAEtB,QAAM;GACN;;AAGN,MAAa,6BAAa,IAAI,KAA+C"}
|
package/dist/esm/types.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","names":[],"sources":["../../src/types.ts"],"sourcesContent":[],"mappings":";kBAAiB,GAAA;EAAA,UAAG,MAAA,CAAA,CAIR;AAIZ;AAEK,KANO,SAAA,GAMQ,MANU,GAAA,CAAI,MAMd,SAAA,KAAA,GAAA,MAAA,GAAA,MAJV,GAAA,CAAI,MAIM;AAAqB,KAF7B,SAAA,GAAY,IAEiB,CAFZ,SAEY,EAAA,UAAA,CAAA;KAApC,eAED,CAAA,UAAA,MAAA,CAAA,GAFqC,CAErC,SAAA,GAAA,GAAA,OAAA,GAAA,CAAA,SAAA,GAAA,KAAA,QAAA,IAAA,KAAA,KAAA,EAAA,GACE,eADF,CACkB,IADlB,CAAA,GAEE,CAFF;AACkB,UAGL,SAAA,CAHK;EAAhB,QAAA,EAIM,SAJN,EAAA;EACA,IAAA,EAIE,SAJF;EAAC,IAAA,EAKC,eALD,CAKiB,SALjB,CAAA;EAEU,GAAA,EAAA,GAAA,MAAS,MAAA;EACd,IAAA,EAAA,GAAA,MAAA,OAAA"}
|
package/dist/esm/util.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"util.js","names":["route"],"sources":["../../src/util.ts"],"sourcesContent":["\"use client\";\nimport { RouteData } from \"./types.js\";\n\n// remove trailing slashes and index from the path\nexport const normalizeRoutePath = (path: string) => {\n for (const suffix of [\"/\", \"/index\"]) {\n if (path.endsWith(suffix)) {\n return path.slice(0, -suffix.length) || \"/\";\n }\n }\n\n return path;\n};\n\n// convert a path back to a route path\nexport const denormalizeRoutePath = (path: string) => {\n if (path === \"/\") {\n return \"index\";\n }\n\n if (path.startsWith(\"/\")) {\n return path.substring(1);\n }\n\n return path;\n};\n\nexport function getPathData(pathname: string, routes: RouteData[]) {\n const path = pathname.startsWith(\"/\") ? pathname : `/${pathname}`;\n\n // path for the URL (including query string/hash)\n const normalizedPath = normalizeRoutePath(path);\n\n // we want the pathname without the query string/hash to match the route\n const url = new URL(pathname, window.location.origin);\n\n const routePathName = normalizeRoutePath(url.pathname);\n\n const route = routes.find((route) => route.path === routePathName);\n\n if (!route) {\n return null;\n }\n\n return {\n ...route,\n rsc: route.rsc,\n url: normalizedPath,\n };\n}\n"],"mappings":";;;;AAIA,MAAa,sBAAsB,SAAiB;AAClD,MAAK,MAAM,UAAU,CAAC,KAAK,SAAS,CAClC,KAAI,KAAK,SAAS,OAAO,CACvB,QAAO,KAAK,MAAM,GAAG,CAAC,OAAO,OAAO,IAAI;AAI5C,QAAO;;AAgBT,SAAgB,YAAY,UAAkB,QAAqB;CAIjE,MAAM,iBAAiB,mBAHV,SAAS,WAAW,IAAI,GAAG,WAAW,IAAI,WAGR;CAK/C,MAAM,gBAAgB,mBAFV,IAAI,IAAI,UAAU,OAAO,SAAS,OAAO,CAER,SAAS;CAEtD,MAAM,QAAQ,OAAO,MAAM,YAAUA,QAAM,SAAS,cAAc;AAElE,KAAI,CAAC,MACH,QAAO;AAGT,QAAO;EACL,GAAG;EACH,KAAK,MAAM;EACX,KAAK;EACN"}
|