@teyik0/furin 0.1.0-alpha.11 → 0.1.0-alpha.13
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/LICENSE +21 -0
- package/README.md +251 -0
- package/dist/build/compile-entry.ts +129 -0
- package/dist/build/entry-template.ts +133 -0
- package/dist/build/index.js +453 -282
- package/dist/build/server-routes-entry.ts +55 -0
- package/dist/cli/index.js +457 -286
- package/dist/env.d.ts +50 -0
- package/dist/furin.js +402 -263
- package/dist/internal.js +17 -0
- package/dist/link.d.ts +5 -438
- package/dist/link.js +228 -137
- package/dist/plugin/index.js +9 -7
- package/dist/render/cache.d.ts +18 -5
- package/dist/render/dev-cache.d.ts +29 -0
- package/dist/render/index.ts +870 -0
- package/dist/render/route-cache.d.ts +26 -0
- package/dist/render/shell.ts +187 -0
- package/dist/router-provider.d.ts +467 -0
- package/dist/router.js +515 -368
- package/dist/router.ts +1034 -0
- package/dist/runtime-env.js +10 -0
- package/package.json +47 -17
- package/src/adapter/bun.ts +4 -1
- package/src/build/entry-template.ts +13 -6
- package/src/build/hydrate.ts +7 -5
- package/src/build/scan-server.ts +32 -3
- package/src/dev-page-plugin.ts +6 -0
- package/src/link.tsx +43 -1297
- package/src/plugin/transform-client.ts +10 -8
- package/src/render/cache.ts +77 -67
- package/src/render/dev-cache.ts +106 -15
- package/src/render/index.ts +4 -3
- package/src/render/route-cache.ts +124 -0
- package/src/router-provider.tsx +1428 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import MagicString from "magic-string";
|
|
2
|
-
import {
|
|
2
|
+
import { parse } from "yuku-parser";
|
|
3
3
|
|
|
4
4
|
// ---------------------------------------------------------------------------
|
|
5
5
|
// Bun.Transpiler singleton — strips TypeScript + JSX before AST work.
|
|
@@ -280,9 +280,10 @@ function removeUnusedSpecifiers(
|
|
|
280
280
|
// ---------------------------------------------------------------------------
|
|
281
281
|
export function deadCodeElimination(s: MagicString): MagicString {
|
|
282
282
|
const code = s.toString();
|
|
283
|
-
const { program,
|
|
284
|
-
|
|
285
|
-
|
|
283
|
+
const { program, diagnostics } = parse(code, { sourceType: "module" });
|
|
284
|
+
const firstError = diagnostics.find((d) => d.severity === "error");
|
|
285
|
+
if (firstError) {
|
|
286
|
+
console.error("[furin] DCE: failed to parse transformed output:", firstError.message);
|
|
286
287
|
return s;
|
|
287
288
|
}
|
|
288
289
|
|
|
@@ -347,10 +348,11 @@ export function transformForClient(code: string, filename: string): TransformRes
|
|
|
347
348
|
// Pass 1 — Bun.Transpiler: strip TypeScript + JSX → plain JS.
|
|
348
349
|
const plainJs = bunTranspiler.transformSync(code);
|
|
349
350
|
|
|
350
|
-
// Pass 2 —
|
|
351
|
-
const { program,
|
|
352
|
-
|
|
353
|
-
|
|
351
|
+
// Pass 2 — yuku-parser: parse plain JS to ESTree AST with span offsets.
|
|
352
|
+
const { program, diagnostics } = parse(plainJs, { sourceType: "module" });
|
|
353
|
+
const firstError = diagnostics.find((d) => d.severity === "error");
|
|
354
|
+
if (firstError) {
|
|
355
|
+
throw new Error(`Failed to parse ${filename}: ${firstError.message}`);
|
|
354
356
|
}
|
|
355
357
|
|
|
356
358
|
// Pass 3 — MagicString: surgically remove server-only properties.
|
package/src/render/cache.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { createLogger } from "../context-logger.ts";
|
|
3
|
+
import { type Cache, createRouteCache, type RevalidateType } from "./route-cache.ts";
|
|
4
|
+
|
|
5
|
+
export type { Cache, CacheInvalidationResult, RevalidateType } from "./route-cache.ts";
|
|
6
|
+
|
|
1
7
|
export interface ISRCacheEntry {
|
|
2
8
|
generatedAt: number;
|
|
3
9
|
html: string;
|
|
@@ -15,21 +21,17 @@ const MAX_ISR_CACHE_SIZE = 1000;
|
|
|
15
21
|
/** Maximum number of SSG cache entries before LRU eviction kicks in. */
|
|
16
22
|
const MAX_SSG_CACHE_SIZE = 1000;
|
|
17
23
|
|
|
18
|
-
|
|
19
|
-
|
|
24
|
+
const isrRouteCache = createRouteCache<ISRCacheEntry>({
|
|
25
|
+
maxSize: MAX_ISR_CACHE_SIZE,
|
|
26
|
+
name: "render:isr-html",
|
|
27
|
+
});
|
|
28
|
+
const ssgRouteCache = createRouteCache<SsgCacheEntry>({
|
|
29
|
+
maxSize: MAX_SSG_CACHE_SIZE,
|
|
30
|
+
name: "render:ssg-html",
|
|
31
|
+
});
|
|
20
32
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
* JS Maps maintain insertion order, so the first key is always the oldest.
|
|
24
|
-
*/
|
|
25
|
-
function evictOldest<V>(map: Map<string, V>, maxSize: number): void {
|
|
26
|
-
if (map.size > maxSize) {
|
|
27
|
-
const oldest = map.keys().next().value;
|
|
28
|
-
if (oldest !== undefined) {
|
|
29
|
-
map.delete(oldest);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
+
export const isrCache = isrRouteCache.store;
|
|
34
|
+
export const ssgCache = ssgRouteCache.store;
|
|
33
35
|
|
|
34
36
|
/**
|
|
35
37
|
* Gets an ISR cache entry and refreshes its recency so it is treated as
|
|
@@ -37,13 +39,7 @@ function evictOldest<V>(map: Map<string, V>, maxSize: number): void {
|
|
|
37
39
|
* was written early would be evicted before a cold entry written later.
|
|
38
40
|
*/
|
|
39
41
|
export function getISRCache(key: string): ISRCacheEntry | undefined {
|
|
40
|
-
|
|
41
|
-
if (entry !== undefined) {
|
|
42
|
-
// Re-insert at the end to mark as most-recently used
|
|
43
|
-
isrCache.delete(key);
|
|
44
|
-
isrCache.set(key, entry);
|
|
45
|
-
}
|
|
46
|
-
return entry;
|
|
42
|
+
return isrRouteCache.get(key);
|
|
47
43
|
}
|
|
48
44
|
|
|
49
45
|
/**
|
|
@@ -51,27 +47,17 @@ export function getISRCache(key: string): ISRCacheEntry | undefined {
|
|
|
51
47
|
* recently used by the LRU eviction policy.
|
|
52
48
|
*/
|
|
53
49
|
export function getSSGCache(key: string): SsgCacheEntry | undefined {
|
|
54
|
-
|
|
55
|
-
if (entry !== undefined) {
|
|
56
|
-
ssgCache.delete(key);
|
|
57
|
-
ssgCache.set(key, entry);
|
|
58
|
-
}
|
|
59
|
-
return entry;
|
|
50
|
+
return ssgRouteCache.get(key);
|
|
60
51
|
}
|
|
61
52
|
|
|
62
53
|
/** Sets an ISR cache entry with LRU eviction. */
|
|
63
54
|
export function setISRCache(key: string, entry: ISRCacheEntry): void {
|
|
64
|
-
|
|
65
|
-
isrCache.delete(key);
|
|
66
|
-
isrCache.set(key, entry);
|
|
67
|
-
evictOldest(isrCache, MAX_ISR_CACHE_SIZE);
|
|
55
|
+
isrRouteCache.set(key, entry);
|
|
68
56
|
}
|
|
69
57
|
|
|
70
58
|
/** Sets an SSG cache entry with LRU eviction. */
|
|
71
59
|
export function setSSGCache(key: string, entry: SsgCacheEntry): void {
|
|
72
|
-
|
|
73
|
-
ssgCache.set(key, entry);
|
|
74
|
-
evictOldest(ssgCache, MAX_SSG_CACHE_SIZE);
|
|
60
|
+
ssgRouteCache.set(key, entry);
|
|
75
61
|
}
|
|
76
62
|
|
|
77
63
|
// ── Build ID ─────────────────────────────────────────────────────────────────
|
|
@@ -88,7 +74,6 @@ export function getBuildId(): string {
|
|
|
88
74
|
return _buildId;
|
|
89
75
|
}
|
|
90
76
|
|
|
91
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
92
77
|
// ── Pending invalidations (server → client bridge) ───────────────────────────
|
|
93
78
|
//
|
|
94
79
|
// Per-request scoping via AsyncLocalStorage: furin wraps each request's full
|
|
@@ -96,7 +81,6 @@ import { AsyncLocalStorage } from "node:async_hooks";
|
|
|
96
81
|
// that `revalidatePath()` and `consumePendingInvalidations()` share an isolated
|
|
97
82
|
// Set per request. The global `_globalPendingInvalidations` is a fallback for
|
|
98
83
|
// calls made outside a request context (e.g. scripts, tests, warmup code).
|
|
99
|
-
import { createLogger } from "../context-logger.ts";
|
|
100
84
|
|
|
101
85
|
const _requestInvalidationScope = new AsyncLocalStorage<Set<string>>();
|
|
102
86
|
const _globalPendingInvalidations = new Set<string>();
|
|
@@ -177,13 +161,58 @@ export function callCachePurger(paths: string[]): void {
|
|
|
177
161
|
});
|
|
178
162
|
}
|
|
179
163
|
|
|
164
|
+
// ── Path cache invalidators ──────────────────────────────────────────────────
|
|
165
|
+
|
|
166
|
+
export type CacheInvalidator = Pick<Cache<unknown>, "invalidatePath" | "name">;
|
|
167
|
+
|
|
168
|
+
const _cacheInvalidators = new Map<string, CacheInvalidator>();
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Register a cache invalidator that participates in `revalidatePath()`.
|
|
172
|
+
*
|
|
173
|
+
* Internal extension point for render caches that are not backed by the
|
|
174
|
+
* production HTML maps. The returned function unregisters the same instance.
|
|
175
|
+
*
|
|
176
|
+
* @internal
|
|
177
|
+
*/
|
|
178
|
+
export function registerCacheInvalidator(invalidator: CacheInvalidator): () => void {
|
|
179
|
+
const existing = _cacheInvalidators.get(invalidator.name);
|
|
180
|
+
if (existing !== undefined && existing !== invalidator) {
|
|
181
|
+
throw new Error(
|
|
182
|
+
`Cache invalidator "${invalidator.name}" is already registered with a different instance.`
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
if (existing === invalidator) {
|
|
186
|
+
return () => {
|
|
187
|
+
if (_cacheInvalidators.get(invalidator.name) === invalidator) {
|
|
188
|
+
_cacheInvalidators.delete(invalidator.name);
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
_cacheInvalidators.set(invalidator.name, invalidator);
|
|
193
|
+
return () => {
|
|
194
|
+
if (_cacheInvalidators.get(invalidator.name) === invalidator) {
|
|
195
|
+
_cacheInvalidators.delete(invalidator.name);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function dedupePaths(paths: string[]): string[] {
|
|
201
|
+
return [...new Set(paths)];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
registerCacheInvalidator(isrRouteCache);
|
|
205
|
+
registerCacheInvalidator(ssgRouteCache);
|
|
206
|
+
|
|
180
207
|
// ── revalidatePath ───────────────────────────────────────────────────────────
|
|
181
208
|
|
|
182
209
|
/**
|
|
183
210
|
* Programmatically invalidate the server-side cache for a given path.
|
|
184
211
|
*
|
|
185
|
-
* - `type: 'page'`
|
|
186
|
-
* - `type: 'layout'
|
|
212
|
+
* - `type: 'page'` — exact URL match.
|
|
213
|
+
* - `type: 'layout'` — the path itself plus all nested children (prefix match).
|
|
214
|
+
*
|
|
215
|
+
* The `type` parameter is required and must be provided by callers.
|
|
187
216
|
*
|
|
188
217
|
* Works for ISR and SSG routes. SSR routes are always fresh (no server-side
|
|
189
218
|
* cache), but calling this still queues a client-side prefetch invalidation
|
|
@@ -199,45 +228,23 @@ export function callCachePurger(paths: string[]): void {
|
|
|
199
228
|
* // In an API route or webhook handler:
|
|
200
229
|
* import { revalidatePath } from "@teyik0/furin";
|
|
201
230
|
*
|
|
202
|
-
* revalidatePath("/blog/my-post");
|
|
231
|
+
* revalidatePath("/blog/my-post", "page"); // invalidate a single page
|
|
203
232
|
* revalidatePath("/blog", "layout"); // invalidate /blog + all children
|
|
204
233
|
* ```
|
|
205
234
|
*/
|
|
206
|
-
export function revalidatePath(path: string, type:
|
|
235
|
+
export function revalidatePath(path: string, type: RevalidateType): boolean {
|
|
207
236
|
// Queue for client-side notification via X-Furin-Revalidate header
|
|
208
237
|
_activeInvalidationSet().add(type === "layout" ? `${path}:layout` : path);
|
|
209
238
|
|
|
210
239
|
let deleted = false;
|
|
211
|
-
|
|
212
|
-
if (type === "page") {
|
|
213
|
-
deleted = isrCache.delete(path) || deleted;
|
|
214
|
-
deleted = ssgCache.delete(path) || deleted;
|
|
215
|
-
callCachePurger([path]);
|
|
216
|
-
return deleted;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
// layout: prefix match — invalidate the path itself + all nested children
|
|
220
|
-
const prefix = path === "/" || path.endsWith("/") ? path : `${path}/`;
|
|
221
240
|
const purgedPaths: string[] = [];
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
deleted = true;
|
|
227
|
-
purgedPaths.push(key);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
for (const key of ssgCache.keys()) {
|
|
231
|
-
if (key === path || key.startsWith(prefix)) {
|
|
232
|
-
ssgCache.delete(key);
|
|
233
|
-
deleted = true;
|
|
234
|
-
if (!purgedPaths.includes(key)) {
|
|
235
|
-
purgedPaths.push(key);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
241
|
+
for (const invalidator of _cacheInvalidators.values()) {
|
|
242
|
+
const result = invalidator.invalidatePath(path, type);
|
|
243
|
+
deleted = result.deleted || deleted;
|
|
244
|
+
purgedPaths.push(...result.purgedPaths);
|
|
238
245
|
}
|
|
239
246
|
|
|
240
|
-
callCachePurger(purgedPaths
|
|
247
|
+
callCachePurger(dedupePaths([...purgedPaths, path]));
|
|
241
248
|
return deleted;
|
|
242
249
|
}
|
|
243
250
|
|
|
@@ -248,4 +255,7 @@ export function __resetCacheState(): void {
|
|
|
248
255
|
_buildId = "";
|
|
249
256
|
_globalPendingInvalidations.clear();
|
|
250
257
|
_cachePurger = null;
|
|
258
|
+
_cacheInvalidators.clear();
|
|
259
|
+
registerCacheInvalidator(isrRouteCache);
|
|
260
|
+
registerCacheInvalidator(ssgRouteCache);
|
|
251
261
|
}
|
package/src/render/dev-cache.ts
CHANGED
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { statSync } from "node:fs";
|
|
20
|
+
import { registerCacheInvalidator } from "./cache.ts";
|
|
21
|
+
import { createRouteCache, type RevalidateType } from "./route-cache.ts";
|
|
20
22
|
|
|
21
23
|
export interface DevLoaderCacheEntry {
|
|
22
24
|
/**
|
|
@@ -36,9 +38,6 @@ export interface DevLoaderCacheEntry {
|
|
|
36
38
|
revalidate: number;
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
const devISRLoaderCache = new Map<string, DevLoaderCacheEntry>();
|
|
40
|
-
const devSSGLoaderCache = new Map<string, DevLoaderCacheEntry>();
|
|
41
|
-
|
|
42
41
|
/**
|
|
43
42
|
* Reverse index: source file → set of cache keys whose entries depend on it.
|
|
44
43
|
* Populated by `setDevISRLoaderCache` / `setDevSSGLoaderCache`, drained by
|
|
@@ -48,13 +47,10 @@ const devSSGLoaderCache = new Map<string, DevLoaderCacheEntry>();
|
|
|
48
47
|
const sourceFileToCacheKeys = new Map<string, Set<string>>();
|
|
49
48
|
|
|
50
49
|
interface CacheKindHandle {
|
|
51
|
-
cache:
|
|
50
|
+
cache: ReturnType<typeof createDevLoaderCache>;
|
|
52
51
|
kind: "isr" | "ssg";
|
|
53
52
|
}
|
|
54
53
|
|
|
55
|
-
const isrHandle: CacheKindHandle = { cache: devISRLoaderCache, kind: "isr" };
|
|
56
|
-
const ssgHandle: CacheKindHandle = { cache: devSSGLoaderCache, kind: "ssg" };
|
|
57
|
-
|
|
58
54
|
function indexEntryDependencies(cacheKey: string, deps: string[]): void {
|
|
59
55
|
for (const dep of deps) {
|
|
60
56
|
let bucket = sourceFileToCacheKeys.get(dep);
|
|
@@ -79,14 +75,31 @@ function unindexEntryDependencies(cacheKey: string, deps: string[]): void {
|
|
|
79
75
|
}
|
|
80
76
|
}
|
|
81
77
|
|
|
78
|
+
function createDevLoaderCache(name: string) {
|
|
79
|
+
return createRouteCache<DevLoaderCacheEntry>({
|
|
80
|
+
name,
|
|
81
|
+
onDelete: (key, entry) => {
|
|
82
|
+
unindexEntryDependencies(key, entry.dependencies);
|
|
83
|
+
},
|
|
84
|
+
onSet: (key, entry, previous) => {
|
|
85
|
+
// Drop any stale reverse-index links from a previous entry under this key.
|
|
86
|
+
if (previous) {
|
|
87
|
+
unindexEntryDependencies(key, previous.dependencies);
|
|
88
|
+
}
|
|
89
|
+
indexEntryDependencies(key, entry.dependencies);
|
|
90
|
+
},
|
|
91
|
+
pathFromKey: urlPathFromCacheKey,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const devISRLoaderCache = createDevLoaderCache("render:dev-isr-loader");
|
|
96
|
+
const devSSGLoaderCache = createDevLoaderCache("render:dev-ssg-loader");
|
|
97
|
+
|
|
98
|
+
const isrHandle: CacheKindHandle = { cache: devISRLoaderCache, kind: "isr" };
|
|
99
|
+
const ssgHandle: CacheKindHandle = { cache: devSSGLoaderCache, kind: "ssg" };
|
|
100
|
+
|
|
82
101
|
function setEntry(handle: CacheKindHandle, key: string, entry: DevLoaderCacheEntry): void {
|
|
83
|
-
// Drop any stale reverse-index links from a previous entry under this key.
|
|
84
|
-
const previous = handle.cache.get(key);
|
|
85
|
-
if (previous) {
|
|
86
|
-
unindexEntryDependencies(key, previous.dependencies);
|
|
87
|
-
}
|
|
88
102
|
handle.cache.set(key, entry);
|
|
89
|
-
indexEntryDependencies(key, entry.dependencies);
|
|
90
103
|
}
|
|
91
104
|
|
|
92
105
|
export function getDevISRLoaderCache(key: string): DevLoaderCacheEntry | undefined {
|
|
@@ -115,6 +128,84 @@ export interface InvalidateOutcome {
|
|
|
115
128
|
ssg: number;
|
|
116
129
|
}
|
|
117
130
|
|
|
131
|
+
/**
|
|
132
|
+
* Extracts the URL path part from a dev cache key.
|
|
133
|
+
*
|
|
134
|
+
* Cache keys have the format `${rootPath}:${urlPath}` where `urlPath` always
|
|
135
|
+
* starts with `/` (it's a route pattern). Filesystem `rootPath` may contain
|
|
136
|
+
* `:` on Windows (e.g. `C:/Users/...`), so we use `lastIndexOf(":/")` to find
|
|
137
|
+
* the boundary — the URL part is always the trailing `/...` segment.
|
|
138
|
+
*
|
|
139
|
+
* Returns `null` if the key doesn't match the expected shape (defensive — all
|
|
140
|
+
* keys produced by `renderDevISRWithLoaderCache` are well-formed, but a
|
|
141
|
+
* future caller could violate the invariant).
|
|
142
|
+
*
|
|
143
|
+
* @internal Exported for unit testing only.
|
|
144
|
+
*/
|
|
145
|
+
export function urlPathFromCacheKey(key: string): string | null {
|
|
146
|
+
const sep = key.lastIndexOf(":/");
|
|
147
|
+
if (sep === -1) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
return key.slice(sep + 1);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Drops every dev cache entry whose URL path matches `path` according to the
|
|
155
|
+
* same rules as `revalidatePath`:
|
|
156
|
+
*
|
|
157
|
+
* - `type: "page"` — exact match
|
|
158
|
+
* - `type: "layout"` — prefix match (the path itself + any descendant)
|
|
159
|
+
*
|
|
160
|
+
* Called from `cache.ts:revalidatePath` so a single API works for both prod
|
|
161
|
+
* (HTML caches `isrCache` / `ssgCache`) and dev (loader-data caches
|
|
162
|
+
* `devISRLoaderCache` / `devSSGLoaderCache`). Without this, dev users see
|
|
163
|
+
* stale loader data after mutations until the revalidate window expires.
|
|
164
|
+
*/
|
|
165
|
+
export function invalidateDevLoaderCacheByPath(
|
|
166
|
+
path: string,
|
|
167
|
+
type: RevalidateType
|
|
168
|
+
): InvalidateOutcome {
|
|
169
|
+
const cleared: string[] = [];
|
|
170
|
+
let isr = 0;
|
|
171
|
+
let ssg = 0;
|
|
172
|
+
|
|
173
|
+
const matches = (urlPath: string): boolean => {
|
|
174
|
+
if (type === "page") {
|
|
175
|
+
return urlPath === path;
|
|
176
|
+
}
|
|
177
|
+
// layout: the path itself + any descendant
|
|
178
|
+
const prefix = path === "/" || path.endsWith("/") ? path : `${path}/`;
|
|
179
|
+
return urlPath === path || urlPath.startsWith(prefix);
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
for (const handle of [isrHandle, ssgHandle]) {
|
|
183
|
+
// Snapshot keys before mutation — drop-as-we-go invalidates the iterator.
|
|
184
|
+
for (const key of [...handle.cache.keys()]) {
|
|
185
|
+
const urlPath = urlPathFromCacheKey(key);
|
|
186
|
+
if (urlPath === null || !matches(urlPath)) {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const entry = handle.cache.get(key);
|
|
190
|
+
if (!entry) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
handle.cache.delete(key);
|
|
194
|
+
cleared.push(key);
|
|
195
|
+
if (handle.kind === "isr") {
|
|
196
|
+
isr++;
|
|
197
|
+
} else {
|
|
198
|
+
ssg++;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return { cleared, isr, ssg };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
registerCacheInvalidator(devISRLoaderCache);
|
|
207
|
+
registerCacheInvalidator(devSSGLoaderCache);
|
|
208
|
+
|
|
118
209
|
/**
|
|
119
210
|
* Drops every dev cache entry whose dependency chain includes `filePath`.
|
|
120
211
|
* Safe to call for files that were never registered — returns an empty
|
|
@@ -137,7 +228,6 @@ export function invalidateDevLoaderCacheBySource(filePath: string): InvalidateOu
|
|
|
137
228
|
const isrEntry = devISRLoaderCache.get(key);
|
|
138
229
|
if (isrEntry) {
|
|
139
230
|
devISRLoaderCache.delete(key);
|
|
140
|
-
unindexEntryDependencies(key, isrEntry.dependencies);
|
|
141
231
|
cleared.push(key);
|
|
142
232
|
isr++;
|
|
143
233
|
continue;
|
|
@@ -145,7 +235,6 @@ export function invalidateDevLoaderCacheBySource(filePath: string): InvalidateOu
|
|
|
145
235
|
const ssgEntry = devSSGLoaderCache.get(key);
|
|
146
236
|
if (ssgEntry) {
|
|
147
237
|
devSSGLoaderCache.delete(key);
|
|
148
|
-
unindexEntryDependencies(key, ssgEntry.dependencies);
|
|
149
238
|
cleared.push(key);
|
|
150
239
|
ssg++;
|
|
151
240
|
}
|
|
@@ -220,4 +309,6 @@ export function __resetDevLoaderCacheState(): void {
|
|
|
220
309
|
devISRLoaderCache.clear();
|
|
221
310
|
devSSGLoaderCache.clear();
|
|
222
311
|
sourceFileToCacheKeys.clear();
|
|
312
|
+
registerCacheInvalidator(devISRLoaderCache);
|
|
313
|
+
registerCacheInvalidator(devSSGLoaderCache);
|
|
223
314
|
}
|
package/src/render/index.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
// biome-ignore-all lint/correctness/useHookAtTopLevel: useLogger is not a hook attached to a react component
|
|
1
2
|
import { createElement, type ReactNode } from "react";
|
|
2
3
|
import { renderToReadableStream } from "react-dom/server";
|
|
3
|
-
import { RouterContext, type RouterContextValue } from "../link.tsx";
|
|
4
4
|
import { FurinNotFoundError } from "../not-found.ts";
|
|
5
5
|
import type { RootLayout } from "../router.ts";
|
|
6
|
+
import { normalizeHref, RouterContext, type RouterContextValue } from "../router-provider.tsx";
|
|
6
7
|
import { assembleHTML, resolvePath, splitTemplate, streamToString } from "./assemble.ts";
|
|
7
8
|
import {
|
|
8
9
|
getISRCache,
|
|
@@ -146,7 +147,7 @@ async function prepareRender(
|
|
|
146
147
|
// while the client hydrates with the real pathname.
|
|
147
148
|
const ssrContext: RouterContextValue = {
|
|
148
149
|
basePath: basePath ?? "",
|
|
149
|
-
currentHref: ctx.path,
|
|
150
|
+
currentHref: normalizeHref(ctx.path),
|
|
150
151
|
navigate: () => Promise.resolve(),
|
|
151
152
|
prefetch: () => {
|
|
152
153
|
/* noop */
|
|
@@ -473,7 +474,7 @@ export async function renderRootNotFound(
|
|
|
473
474
|
|
|
474
475
|
const notFoundContext: RouterContextValue = {
|
|
475
476
|
basePath: "",
|
|
476
|
-
currentHref: request ? new URL(request.url).pathname : "/",
|
|
477
|
+
currentHref: request ? normalizeHref(new URL(request.url).pathname) : "/",
|
|
477
478
|
navigate: () => Promise.resolve(),
|
|
478
479
|
prefetch: () => {
|
|
479
480
|
/* noop */
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
export type RevalidateType = "page" | "layout";
|
|
2
|
+
|
|
3
|
+
export interface CacheInvalidationResult {
|
|
4
|
+
deleted: boolean;
|
|
5
|
+
purgedPaths: string[];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface Cache<Entry> {
|
|
9
|
+
clear(): void;
|
|
10
|
+
delete(key: string): boolean;
|
|
11
|
+
entries(): IterableIterator<[string, Entry]>;
|
|
12
|
+
get(key: string): Entry | undefined;
|
|
13
|
+
has(key: string): boolean;
|
|
14
|
+
invalidatePath(path: string, type: RevalidateType): CacheInvalidationResult;
|
|
15
|
+
keys(): IterableIterator<string>;
|
|
16
|
+
readonly name: string;
|
|
17
|
+
set(key: string, entry: Entry): void;
|
|
18
|
+
get size(): number;
|
|
19
|
+
readonly store: Map<string, Entry>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RouteCacheOptions<Entry> {
|
|
23
|
+
maxSize?: number;
|
|
24
|
+
name: string;
|
|
25
|
+
onDelete?: (key: string, entry: Entry) => void;
|
|
26
|
+
onSet?: (key: string, entry: Entry, previous: Entry | undefined) => void;
|
|
27
|
+
pathFromKey?: (key: string) => string | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function defaultPathFromKey(key: string): string {
|
|
31
|
+
return key;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function matchesPath(urlPath: string, path: string, type: RevalidateType): boolean {
|
|
35
|
+
if (type === "page") {
|
|
36
|
+
return urlPath === path;
|
|
37
|
+
}
|
|
38
|
+
const prefix = path === "/" || path.endsWith("/") ? path : `${path}/`;
|
|
39
|
+
return urlPath === path || urlPath.startsWith(prefix);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function createRouteCache<Entry>(options: RouteCacheOptions<Entry>): Cache<Entry> {
|
|
43
|
+
const store = new Map<string, Entry>();
|
|
44
|
+
const pathFromKey = options.pathFromKey ?? defaultPathFromKey;
|
|
45
|
+
|
|
46
|
+
const evictOldest = (): void => {
|
|
47
|
+
if (options.maxSize === undefined || store.size <= options.maxSize) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const oldest = store.keys().next().value;
|
|
51
|
+
if (oldest !== undefined) {
|
|
52
|
+
deleteEntry(oldest);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const deleteEntry = (key: string): boolean => {
|
|
57
|
+
const entry = store.get(key);
|
|
58
|
+
if (entry === undefined) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
store.delete(key);
|
|
62
|
+
options.onDelete?.(key, entry);
|
|
63
|
+
return true;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
name: options.name,
|
|
68
|
+
store,
|
|
69
|
+
get size() {
|
|
70
|
+
return store.size;
|
|
71
|
+
},
|
|
72
|
+
get(key) {
|
|
73
|
+
const entry = store.get(key);
|
|
74
|
+
if (entry !== undefined && options.maxSize !== undefined) {
|
|
75
|
+
store.delete(key);
|
|
76
|
+
store.set(key, entry);
|
|
77
|
+
}
|
|
78
|
+
return entry;
|
|
79
|
+
},
|
|
80
|
+
set(key, entry) {
|
|
81
|
+
const previous = store.get(key);
|
|
82
|
+
if (previous !== undefined) {
|
|
83
|
+
store.delete(key);
|
|
84
|
+
}
|
|
85
|
+
store.set(key, entry);
|
|
86
|
+
options.onSet?.(key, entry, previous);
|
|
87
|
+
evictOldest();
|
|
88
|
+
},
|
|
89
|
+
delete(key) {
|
|
90
|
+
return deleteEntry(key);
|
|
91
|
+
},
|
|
92
|
+
clear() {
|
|
93
|
+
for (const key of [...store.keys()]) {
|
|
94
|
+
deleteEntry(key);
|
|
95
|
+
}
|
|
96
|
+
store.clear();
|
|
97
|
+
},
|
|
98
|
+
has(key) {
|
|
99
|
+
return store.has(key);
|
|
100
|
+
},
|
|
101
|
+
entries() {
|
|
102
|
+
return store.entries();
|
|
103
|
+
},
|
|
104
|
+
keys() {
|
|
105
|
+
return store.keys();
|
|
106
|
+
},
|
|
107
|
+
invalidatePath(path, type) {
|
|
108
|
+
let deleted = false;
|
|
109
|
+
const purgedPaths: string[] = [];
|
|
110
|
+
|
|
111
|
+
for (const key of [...store.keys()]) {
|
|
112
|
+
const urlPath = pathFromKey(key);
|
|
113
|
+
if (urlPath === null || !matchesPath(urlPath, path, type)) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
deleteEntry(key);
|
|
117
|
+
deleted = true;
|
|
118
|
+
purgedPaths.push(urlPath);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return { deleted, purgedPaths: [...new Set(purgedPaths)] };
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|