@teyik0/furin 0.2.0-alpha.1 → 0.2.0-alpha.3

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.
Files changed (84) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +33 -33
  3. package/dist/adapter/static.d.ts +1 -1
  4. package/dist/build/compile-entry.ts +109 -109
  5. package/dist/build/entry-template.ts +156 -156
  6. package/dist/build/index.js +1724 -687
  7. package/dist/build/route-types.d.ts +3 -1
  8. package/dist/build/server-routes-entry.ts +36 -36
  9. package/dist/build/shared.d.ts +1 -0
  10. package/dist/build/ssg-cache.d.ts +1 -1
  11. package/dist/cli/index.js +1673 -658
  12. package/dist/client/link.js +883 -89
  13. package/dist/client/router/index.d.ts +4 -0
  14. package/dist/client/router/link-utils.d.ts +3 -1
  15. package/dist/client/router/navigation.d.ts +10 -0
  16. package/dist/client/router/provider.d.ts +1 -1
  17. package/dist/client/router/search/index.d.ts +18 -0
  18. package/dist/client/router/search-store.d.ts +21 -0
  19. package/dist/client/router/types.d.ts +14 -1
  20. package/dist/client/sync.d.ts +25 -0
  21. package/dist/client.d.ts +1 -0
  22. package/dist/client.js +44 -0
  23. package/dist/furin.d.ts +9 -1
  24. package/dist/furin.js +1578 -258
  25. package/dist/server/auto-invalidate/runtime.d.ts +1 -1
  26. package/dist/server/dev-page-plugin.d.ts +3 -0
  27. package/dist/server/render/assemble.d.ts +2 -0
  28. package/dist/server/render/isr.d.ts +2 -1
  29. package/dist/server/render/ssg.d.ts +3 -2
  30. package/dist/server/render/ssr.d.ts +5 -4
  31. package/dist/server/router/hmr.d.ts +4 -3
  32. package/dist/server/router/index.d.ts +1 -1
  33. package/dist/server/router/index.js +1 -2
  34. package/dist/server/router/index.ts +1 -2
  35. package/dist/server/router/plugin.d.ts +2 -1
  36. package/dist/server/router/schemas.d.ts +25 -17
  37. package/dist/server/sync/config.d.ts +6 -0
  38. package/dist/server/sync/index.d.ts +4 -0
  39. package/dist/server/sync/plugin.d.ts +369 -0
  40. package/dist/server/sync/stream.d.ts +47 -0
  41. package/dist/shared/search-params.d.ts +17 -0
  42. package/package.json +10 -5
  43. package/src/adapter/README.md +13 -13
  44. package/src/adapter/static.ts +23 -6
  45. package/src/build/client.ts +152 -149
  46. package/src/build/compile-entry.ts +109 -109
  47. package/src/build/entry-template.ts +156 -156
  48. package/src/build/hydrate.ts +340 -331
  49. package/src/build/index.ts +152 -152
  50. package/src/build/route-types.ts +145 -115
  51. package/src/build/scan-server.ts +95 -95
  52. package/src/build/server-routes-entry.ts +36 -36
  53. package/src/build/shared.ts +80 -78
  54. package/src/build/ssg-cache.ts +32 -31
  55. package/src/build/types.ts +89 -89
  56. package/src/client/link.tsx +13 -4
  57. package/src/client/router/context.ts +2 -0
  58. package/src/client/router/index.ts +9 -0
  59. package/src/client/router/link-utils.ts +184 -180
  60. package/src/client/router/navigation.ts +61 -0
  61. package/src/client/router/provider.tsx +804 -723
  62. package/src/client/router/search/index.ts +99 -0
  63. package/src/client/router/search-store.ts +86 -0
  64. package/src/client/router/types.ts +276 -263
  65. package/src/client/sync.ts +98 -0
  66. package/src/client.ts +369 -355
  67. package/src/furin.ts +465 -437
  68. package/src/server/auto-invalidate/runtime.ts +134 -133
  69. package/src/server/dev-page-plugin.ts +454 -449
  70. package/src/server/render/assemble.ts +160 -133
  71. package/src/server/render/isr.ts +323 -295
  72. package/src/server/render/not-found.ts +2 -0
  73. package/src/server/render/ssg.ts +15 -4
  74. package/src/server/render/ssr.ts +585 -545
  75. package/src/server/router/hmr.ts +14 -10
  76. package/src/server/router/index.ts +1 -2
  77. package/src/server/router/patterns.ts +5 -0
  78. package/src/server/router/plugin.ts +384 -401
  79. package/src/server/router/schemas.ts +172 -57
  80. package/src/server/sync/config.ts +29 -0
  81. package/src/server/sync/index.ts +14 -0
  82. package/src/server/sync/plugin.ts +108 -0
  83. package/src/server/sync/stream.ts +144 -0
  84. package/src/shared/search-params.ts +182 -0
@@ -1,89 +1,89 @@
1
- import type { BuildTarget, StaticExportConfig } from "../config";
2
- import type { ResolvedRoute } from "../server/router/index.ts";
3
-
4
- export interface BuildClientOptions {
5
- /**
6
- * Sub-path prefix for the deployment (e.g. "/furin" for GitHub Pages).
7
- * Passed through to the generated hydrate entry so SPA navigation uses
8
- * correct physical URLs. Pass "" for root deployments.
9
- */
10
- basePath: string;
11
- /** Inject the evlog client logger into the hydrate entry. Off by default. */
12
- clientLogging: boolean;
13
- outDir: string;
14
- pagesDir?: string;
15
- plugins?: Bun.BunPlugin[];
16
- /**
17
- * Public path prefix for all emitted JS/CSS chunks.
18
- * Pass "/_client/" for root deployments; override for basePath deployments,
19
- * e.g. "/furin/_client/".
20
- */
21
- publicPath: string;
22
- rootLayout: string;
23
- }
24
-
25
- export interface BuildRouteManifestEntry {
26
- hasLayout: boolean;
27
- hasStaticParams: boolean;
28
- mode: ResolvedRoute["mode"];
29
- pagePath: string;
30
- pattern: string;
31
- revalidate: number | null;
32
- }
33
-
34
- export interface TargetBuildManifest {
35
- buildId: string;
36
- clientDir: string | null;
37
- generatedAt: string;
38
- serverEntry: string | null;
39
- serverPath: string | null;
40
- targetDir: string;
41
- templatePath: string | null;
42
- }
43
-
44
- export type AnyTargetManifest = TargetBuildManifest | StaticTargetBuildManifest;
45
-
46
- export interface BuildManifest {
47
- generatedAt: string;
48
- pagesDir: string;
49
- rootDir: string;
50
- rootPath: string;
51
- routes: BuildRouteManifestEntry[];
52
- serverEntry: string | null;
53
- targets: Partial<Record<BuildTarget, AnyTargetManifest>>;
54
- version: 1;
55
- }
56
-
57
- export interface BuildAppOptions {
58
- /** Inject the evlog client logger into the hydrate entry. Defaults to false. */
59
- clientLogging?: boolean;
60
- compile?: "server" | "embed";
61
- pagesDir?: string;
62
- plugins?: Bun.BunPlugin[];
63
- rootDir?: string;
64
- serverEntry?: string;
65
- /** Configuration for the `static` build target. */
66
- staticConfig?: StaticExportConfig;
67
- target: BuildTarget | "all";
68
- }
69
-
70
- /** Build manifest entry produced by the `static` adapter. */
71
- export interface StaticTargetBuildManifest {
72
- basePath: string;
73
- generatedAt: string;
74
- outDir: string;
75
- renderedRoutes: string[];
76
- skippedRoutes: string[];
77
- }
78
-
79
- export interface BuildAppResult {
80
- manifest: BuildManifest;
81
- targets: Partial<Record<BuildTarget, AnyTargetManifest>>;
82
- }
83
-
84
- export type BunBuildAliasConfig = Bun.BuildConfig & {
85
- alias?: Record<string, string>;
86
- outfile?: string;
87
- packages?: "bundle" | "external";
88
- write?: boolean;
89
- };
1
+ import type { BuildTarget, StaticExportConfig } from "../config";
2
+ import type { ResolvedRoute } from "../server/router/index.ts";
3
+
4
+ export interface BuildClientOptions {
5
+ /**
6
+ * Sub-path prefix for the deployment (e.g. "/furin" for GitHub Pages).
7
+ * Passed through to the generated hydrate entry so SPA navigation uses
8
+ * correct physical URLs. Pass "" for root deployments.
9
+ */
10
+ basePath: string;
11
+ /** Inject the evlog client logger into the hydrate entry. Off by default. */
12
+ clientLogging: boolean;
13
+ outDir: string;
14
+ pagesDir?: string;
15
+ plugins?: Bun.BunPlugin[];
16
+ /**
17
+ * Public path prefix for all emitted JS/CSS chunks.
18
+ * Pass "/_client/" for root deployments; override for basePath deployments,
19
+ * e.g. "/furin/_client/".
20
+ */
21
+ publicPath: string;
22
+ rootLayout: string;
23
+ }
24
+
25
+ export interface BuildRouteManifestEntry {
26
+ hasLayout: boolean;
27
+ hasStaticParams: boolean;
28
+ mode: ResolvedRoute["mode"];
29
+ pagePath: string;
30
+ pattern: string;
31
+ revalidate: number | null;
32
+ }
33
+
34
+ export interface TargetBuildManifest {
35
+ buildId: string;
36
+ clientDir: string | null;
37
+ generatedAt: string;
38
+ serverEntry: string | null;
39
+ serverPath: string | null;
40
+ targetDir: string;
41
+ templatePath: string | null;
42
+ }
43
+
44
+ export type AnyTargetManifest = TargetBuildManifest | StaticTargetBuildManifest;
45
+
46
+ export interface BuildManifest {
47
+ generatedAt: string;
48
+ pagesDir: string;
49
+ rootDir: string;
50
+ rootPath: string;
51
+ routes: BuildRouteManifestEntry[];
52
+ serverEntry: string | null;
53
+ targets: Partial<Record<BuildTarget, AnyTargetManifest>>;
54
+ version: 1;
55
+ }
56
+
57
+ export interface BuildAppOptions {
58
+ /** Inject the evlog client logger into the hydrate entry. Defaults to false. */
59
+ clientLogging?: boolean;
60
+ compile?: "server" | "embed";
61
+ pagesDir?: string;
62
+ plugins?: Bun.BunPlugin[];
63
+ rootDir?: string;
64
+ serverEntry?: string;
65
+ /** Configuration for the `static` build target. */
66
+ staticConfig?: StaticExportConfig;
67
+ target: BuildTarget | "all";
68
+ }
69
+
70
+ /** Build manifest entry produced by the `static` adapter. */
71
+ export interface StaticTargetBuildManifest {
72
+ basePath: string;
73
+ generatedAt: string;
74
+ outDir: string;
75
+ renderedRoutes: string[];
76
+ skippedRoutes: string[];
77
+ }
78
+
79
+ export interface BuildAppResult {
80
+ manifest: BuildManifest;
81
+ targets: Partial<Record<BuildTarget, AnyTargetManifest>>;
82
+ }
83
+
84
+ export type BunBuildAliasConfig = Bun.BuildConfig & {
85
+ alias?: Record<string, string>;
86
+ outfile?: string;
87
+ packages?: "bundle" | "external";
88
+ write?: boolean;
89
+ };
@@ -1,5 +1,9 @@
1
1
  import type React from "react";
2
2
  import { createElement, useCallback, useEffect, useRef } from "react";
3
+ import {
4
+ findSearchDefaultsForRouteTarget,
5
+ type SearchParamsInput,
6
+ } from "../shared/search-params.ts";
3
7
  import {
4
8
  buildHref,
5
9
  CLIENT_FALLBACK_ROUTER,
@@ -39,15 +43,18 @@ function computeLinkView<To extends RouteTo>(
39
43
  }: Pick<LinkProps<To>, "to" | "search" | "hash" | "children" | "activeProps" | "inactiveProps">,
40
44
  router: RouterContextValue
41
45
  ): LinkView {
46
+ const searchDefaults = findSearchDefaultsForRouteTarget(to as string, router.searchRoutes);
42
47
  const logicalHref = buildHref(
43
48
  to as string,
44
- search as Record<string, unknown> | null | undefined,
45
- hash
49
+ search as SearchParamsInput | null | undefined,
50
+ hash,
51
+ searchDefaults
46
52
  );
47
53
  const logicalHrefWithoutHash = buildHref(
48
54
  to as string,
49
- search as Record<string, unknown> | null | undefined,
50
- undefined
55
+ search as SearchParamsInput | null | undefined,
56
+ undefined,
57
+ searchDefaults
51
58
  );
52
59
  const isAbsolute =
53
60
  logicalHref.startsWith("http://") ||
@@ -238,6 +245,8 @@ function LinkInteractive<To extends RouteTo>({
238
245
  export const SSR_FALLBACK_ROUTER: RouterContextValue = {
239
246
  basePath: "",
240
247
  currentHref: "/",
248
+ search: {},
249
+ searchRoutes: [],
241
250
  navigate: (_href, _opts) => Promise.resolve(),
242
251
  prefetch: (_href, _opts) => {
243
252
  /* noop */
@@ -8,6 +8,8 @@ export const CLIENT_FALLBACK_ROUTER: RouterContextValue = {
8
8
  // Use the same "/" as SSR_FALLBACK_ROUTER so SSR and client render the
9
9
  // same active-state when no RouterProvider is present, avoiding hydration mismatches.
10
10
  currentHref: "/",
11
+ search: {},
12
+ searchRoutes: [],
11
13
  navigate: (href, _opts) => {
12
14
  window.location.href = href;
13
15
  return Promise.resolve();
@@ -20,7 +20,16 @@ export {
20
20
  TRAILING_SLASHES_RE,
21
21
  toLogical,
22
22
  } from "./link-utils.ts";
23
+ export type { Navigate, NavigateInput } from "./navigation.ts";
24
+ export { useNavigate } from "./navigation.ts";
23
25
  export { RouterProvider } from "./provider.tsx";
26
+ export type { SearchStore, SearchStoreSnapshot } from "./search-store.ts";
27
+ export {
28
+ createSearchStore,
29
+ FALLBACK_SEARCH_STORE,
30
+ SearchStoreContext,
31
+ searchSnapshotFromRouterContext,
32
+ } from "./search-store.ts";
24
33
  export {
25
34
  buildDataEndpoint,
26
35
  buildNotFoundPageElement,
@@ -1,180 +1,184 @@
1
- import type { CacheEntry } from "./types.ts";
2
-
3
- const HASH_FRAGMENT_RE = /#.*$/;
4
- /** Strips one or more trailing slashes — used by `buildDataEndpoint` in static mode. */
5
- export const TRAILING_SLASHES_RE = /\/+$/;
6
-
7
- /**
8
- * Builds a full href string from a pathname, optional search params, and optional hash.
9
- * Null/undefined search values are omitted.
10
- *
11
- * @internal Exported for unit testing only.
12
- */
13
- export function buildHref(
14
- to: string,
15
- search: Record<string, unknown> | null | undefined,
16
- hash: string | undefined
17
- ): string {
18
- let url = to;
19
- if (search && Object.keys(search).length > 0) {
20
- const params = new URLSearchParams();
21
- for (const [k, v] of Object.entries(search)) {
22
- if (v != null) {
23
- params.set(k, String(v));
24
- }
25
- }
26
- const qs = params.toString();
27
- if (qs) {
28
- url += `?${qs}`;
29
- }
30
- }
31
- if (hash) {
32
- url += `#${hash}`;
33
- }
34
- return url;
35
- }
36
-
37
- /**
38
- * Parses the `X-Furin-Revalidate` response header and calls `invalidate` for
39
- * each path entry. Header format: `"/path1,/path2:layout,/path3"`
40
- */
41
- export function applyRevalidateHeader(
42
- headers: Headers,
43
- invalidate: (path: string, type: "page" | "layout" | undefined) => void
44
- ): void {
45
- const headerValue = headers.get("x-furin-revalidate");
46
- if (!headerValue) {
47
- return;
48
- }
49
- for (const entry of headerValue.split(",")) {
50
- const trimmed = entry.trim();
51
- if (!trimmed) {
52
- continue;
53
- }
54
- if (trimmed.endsWith(":layout")) {
55
- invalidate(trimmed.slice(0, -":layout".length), "layout");
56
- } else {
57
- invalidate(trimmed, "page");
58
- }
59
- }
60
- }
61
-
62
- /**
63
- * Returns `true` when `currentPath` is targeted by at least one invalidation entry.
64
- *
65
- * @internal Exported for unit testing only.
66
- */
67
- export function shouldAutoRefreshPath(
68
- currentPath: string,
69
- invalidations: ReadonlyArray<{ path: string; type: "page" | "layout" }>
70
- ): boolean {
71
- const normalizedCurrent = currentPath.split("?")[0] ?? currentPath;
72
- return invalidations.some(({ path, type }) => {
73
- if (type === "page") {
74
- return path === normalizedCurrent;
75
- }
76
- const prefix = path === "/" || path.endsWith("/") ? path : `${path}/`;
77
- return normalizedCurrent === path || normalizedCurrent.startsWith(prefix);
78
- });
79
- }
80
-
81
- export function stripHashFromHref(href: string): string {
82
- return href.replace(HASH_FRAGMENT_RE, "");
83
- }
84
-
85
- /** Strips trailing slashes from a pathname or href. Root `/` is preserved.
86
- * Query strings and hashes are preserved and re-attached after normalization. */
87
- export function normalizeHref(href: string): string {
88
- const url = new URL(href, "http://localhost");
89
- let pathname = url.pathname;
90
- if (pathname !== "/") {
91
- pathname = pathname.replace(/\/+$/g, "");
92
- }
93
- return pathname + url.search + url.hash;
94
- }
95
-
96
- /** Strips a single trailing slash from a pathname. Root `/` and empty string are preserved. */
97
- export function normalizePath(path: string): string {
98
- if (path === "/" || path === "") {
99
- return path;
100
- }
101
- return path.endsWith("/") ? path.slice(0, -1) : path;
102
- }
103
-
104
- /** @internal Exported for unit testing only. */
105
- export function shouldRefetch(entry: CacheEntry): boolean {
106
- return Date.now() - entry.createdAt > entry.staleTime;
107
- }
108
-
109
- /**
110
- * Decides whether a native `<a>` click should be intercepted and converted
111
- * to an SPA navigation. Returns the logical href to navigate to, or `null`
112
- * when the browser should handle the click normally.
113
- *
114
- * @internal Exported for unit testing only.
115
- */
116
- export function shouldInterceptClick(
117
- anchor: { href: string; target: string; hasAttribute(name: string): boolean },
118
- event: { metaKey: boolean; ctrlKey: boolean; shiftKey: boolean; altKey: boolean },
119
- basePath: string,
120
- currentOrigin: string,
121
- currentPathname: string
122
- ): string | null {
123
- if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
124
- return null;
125
- }
126
-
127
- if (anchor.target && anchor.target !== "_self") {
128
- return null;
129
- }
130
-
131
- if (anchor.hasAttribute("download")) {
132
- return null;
133
- }
134
-
135
- const href = anchor.href;
136
- if (!href) {
137
- return null;
138
- }
139
-
140
- let url: URL;
141
- try {
142
- url = new URL(href, currentOrigin);
143
- } catch {
144
- return null;
145
- }
146
-
147
- if (url.origin !== currentOrigin) {
148
- return null;
149
- }
150
-
151
- // Hash-only navigation on the same page — let the browser handle scrolling.
152
- if (normalizePath(url.pathname) === normalizePath(currentPathname) && url.hash) {
153
- return null;
154
- }
155
-
156
- const logicalPath = normalizeHref(toLogical(url.pathname, basePath));
157
- const normalizedBase = normalizePath(basePath);
158
- const normalizedPathname = normalizePath(url.pathname);
159
- if (
160
- normalizedBase &&
161
- normalizedPathname !== normalizedBase &&
162
- !normalizedPathname.startsWith(`${normalizedBase}/`)
163
- ) {
164
- return null;
165
- }
166
- return logicalPath + url.search + url.hash;
167
- }
168
-
169
- /** Strips basePath prefix from a physical pathname, returning the logical path. */
170
- export function toLogical(physicalPathname: string, basePath: string): string {
171
- if (
172
- basePath &&
173
- physicalPathname.startsWith(basePath) &&
174
- // Require a path boundary after the prefix so "/furin" doesn't match "/furinity/foo"
175
- (physicalPathname.length === basePath.length || physicalPathname[basePath.length] === "/")
176
- ) {
177
- return physicalPathname.slice(basePath.length) || "/";
178
- }
179
- return physicalPathname;
180
- }
1
+ import { buildSearchParams, type SearchParamsInput } from "../../shared/search-params.ts";
2
+ import type { CacheEntry } from "./types.ts";
3
+
4
+ const HASH_FRAGMENT_RE = /#.*$/;
5
+ /** Strips one or more trailing slashes — used by `buildDataEndpoint` in static mode. */
6
+ export const TRAILING_SLASHES_RE = /\/+$/;
7
+
8
+ /**
9
+ * Builds a full href string from a pathname, optional search params, and optional hash.
10
+ * Null/undefined search values are omitted.
11
+ *
12
+ * @internal Exported for unit testing only.
13
+ */
14
+ export function buildHref(
15
+ to: string,
16
+ search: SearchParamsInput | null | undefined,
17
+ hash: string | undefined,
18
+ searchDefaults?: SearchParamsInput
19
+ ): string {
20
+ let url = to;
21
+ if (search && Object.keys(search).length > 0) {
22
+ const params = buildSearchParams(search, searchDefaults);
23
+ const qs = params.toString();
24
+ if (qs) {
25
+ url += `?${qs}`;
26
+ }
27
+ }
28
+ if (hash) {
29
+ url += `#${hash}`;
30
+ }
31
+ return url;
32
+ }
33
+
34
+ /**
35
+ * Parses the `X-Furin-Revalidate` response header and calls `invalidate` for
36
+ * each path entry. Header format: `"/path1,/path2:layout,/path3"`
37
+ */
38
+ export function applyRevalidateHeader(
39
+ headers: Headers,
40
+ invalidate: (path: string, type: "page" | "layout" | undefined) => void
41
+ ): void {
42
+ const headerValue = headers.get("x-furin-revalidate");
43
+ if (!headerValue) {
44
+ return;
45
+ }
46
+ applyRevalidateEntries(headerValue.split(","), invalidate);
47
+ }
48
+
49
+ export function applyRevalidateEntries(
50
+ entries: Iterable<string>,
51
+ invalidate: (path: string, type: "page" | "layout" | undefined) => void
52
+ ): void {
53
+ for (const entry of entries) {
54
+ const trimmed = entry.trim();
55
+ if (!trimmed) {
56
+ continue;
57
+ }
58
+ if (trimmed.endsWith(":layout")) {
59
+ invalidate(trimmed.slice(0, -":layout".length), "layout");
60
+ } else {
61
+ invalidate(trimmed, "page");
62
+ }
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Returns `true` when `currentPath` is targeted by at least one invalidation entry.
68
+ *
69
+ * @internal Exported for unit testing only.
70
+ */
71
+ export function shouldAutoRefreshPath(
72
+ currentPath: string,
73
+ invalidations: ReadonlyArray<{ path: string; type: "page" | "layout" }>
74
+ ): boolean {
75
+ const normalizedCurrent = currentPath.split("?")[0] ?? currentPath;
76
+ return invalidations.some(({ path, type }) => {
77
+ if (type === "page") {
78
+ return path === normalizedCurrent;
79
+ }
80
+ const prefix = path === "/" || path.endsWith("/") ? path : `${path}/`;
81
+ return normalizedCurrent === path || normalizedCurrent.startsWith(prefix);
82
+ });
83
+ }
84
+
85
+ export function stripHashFromHref(href: string): string {
86
+ return href.replace(HASH_FRAGMENT_RE, "");
87
+ }
88
+
89
+ /** Strips trailing slashes from a pathname or href. Root `/` is preserved.
90
+ * Query strings and hashes are preserved and re-attached after normalization. */
91
+ export function normalizeHref(href: string): string {
92
+ const url = new URL(href, "http://localhost");
93
+ let pathname = url.pathname;
94
+ if (pathname !== "/") {
95
+ pathname = pathname.replace(/\/+$/g, "");
96
+ }
97
+ return pathname + url.search + url.hash;
98
+ }
99
+
100
+ /** Strips a single trailing slash from a pathname. Root `/` and empty string are preserved. */
101
+ export function normalizePath(path: string): string {
102
+ if (path === "/" || path === "") {
103
+ return path;
104
+ }
105
+ return path.endsWith("/") ? path.slice(0, -1) : path;
106
+ }
107
+
108
+ /** @internal Exported for unit testing only. */
109
+ export function shouldRefetch(entry: CacheEntry): boolean {
110
+ return Date.now() - entry.createdAt > entry.staleTime;
111
+ }
112
+
113
+ /**
114
+ * Decides whether a native `<a>` click should be intercepted and converted
115
+ * to an SPA navigation. Returns the logical href to navigate to, or `null`
116
+ * when the browser should handle the click normally.
117
+ *
118
+ * @internal Exported for unit testing only.
119
+ */
120
+ export function shouldInterceptClick(
121
+ anchor: { href: string; target: string; hasAttribute(name: string): boolean },
122
+ event: { metaKey: boolean; ctrlKey: boolean; shiftKey: boolean; altKey: boolean },
123
+ basePath: string,
124
+ currentOrigin: string,
125
+ currentPathname: string
126
+ ): string | null {
127
+ if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
128
+ return null;
129
+ }
130
+
131
+ if (anchor.target && anchor.target !== "_self") {
132
+ return null;
133
+ }
134
+
135
+ if (anchor.hasAttribute("download")) {
136
+ return null;
137
+ }
138
+
139
+ const href = anchor.href;
140
+ if (!href) {
141
+ return null;
142
+ }
143
+
144
+ let url: URL;
145
+ try {
146
+ url = new URL(href, currentOrigin);
147
+ } catch {
148
+ return null;
149
+ }
150
+
151
+ if (url.origin !== currentOrigin) {
152
+ return null;
153
+ }
154
+
155
+ // Hash-only navigation on the same page — let the browser handle scrolling.
156
+ if (normalizePath(url.pathname) === normalizePath(currentPathname) && url.hash) {
157
+ return null;
158
+ }
159
+
160
+ const logicalPath = normalizeHref(toLogical(url.pathname, basePath));
161
+ const normalizedBase = normalizePath(basePath);
162
+ const normalizedPathname = normalizePath(url.pathname);
163
+ if (
164
+ normalizedBase &&
165
+ normalizedPathname !== normalizedBase &&
166
+ !normalizedPathname.startsWith(`${normalizedBase}/`)
167
+ ) {
168
+ return null;
169
+ }
170
+ return logicalPath + url.search + url.hash;
171
+ }
172
+
173
+ /** Strips basePath prefix from a physical pathname, returning the logical path. */
174
+ export function toLogical(physicalPathname: string, basePath: string): string {
175
+ if (
176
+ basePath &&
177
+ physicalPathname.startsWith(basePath) &&
178
+ // Require a path boundary after the prefix so "/furin" doesn't match "/furinity/foo"
179
+ (physicalPathname.length === basePath.length || physicalPathname[basePath.length] === "/")
180
+ ) {
181
+ return physicalPathname.slice(basePath.length) || "/";
182
+ }
183
+ return physicalPathname;
184
+ }