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

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 (98) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +37 -33
  3. package/dist/adapter/bun.d.ts +17 -1
  4. package/dist/adapter/package.d.ts +26 -0
  5. package/dist/build/client.d.ts +1 -1
  6. package/dist/build/compile-entry.d.ts +3 -3
  7. package/dist/build/compile-entry.ts +114 -109
  8. package/dist/build/entry-template.d.ts +35 -9
  9. package/dist/build/entry-template.ts +206 -156
  10. package/dist/build/hydrate.d.ts +1 -1
  11. package/dist/build/index.js +643 -247
  12. package/dist/build/scan-server.d.ts +10 -4
  13. package/dist/build/server-routes-entry.d.ts +3 -3
  14. package/dist/build/server-routes-entry.ts +30 -36
  15. package/dist/build/ssg-cache.d.ts +1 -1
  16. package/dist/build/types.d.ts +36 -1
  17. package/dist/cli/config.d.ts +6 -1
  18. package/dist/cli/index.js +662 -260
  19. package/dist/client/link.js +163 -16
  20. package/dist/client/router/abort.d.ts +1 -0
  21. package/dist/client/router/sync-catch-up.d.ts +29 -0
  22. package/dist/config.d.ts +11 -2
  23. package/dist/config.js +5 -1
  24. package/dist/furin.d.ts +37 -19
  25. package/dist/furin.js +982 -378
  26. package/dist/server/auto-invalidate/registry.d.ts +8 -1
  27. package/dist/server/cache/dev-loader.d.ts +3 -2
  28. package/dist/server/cache/invalidation.d.ts +19 -0
  29. package/dist/server/cache/isr.d.ts +6 -2
  30. package/dist/server/cache/registry.d.ts +3 -3
  31. package/dist/server/cache/ssg.d.ts +6 -2
  32. package/dist/server/cache/store-view.d.ts +11 -0
  33. package/dist/server/instance.d.ts +86 -0
  34. package/dist/server/internal.d.ts +16 -1
  35. package/dist/server/internal.js +72 -5
  36. package/dist/server/render/template.d.ts +3 -2
  37. package/dist/server/sync/adapter.d.ts +62 -0
  38. package/dist/server/sync/config.d.ts +11 -0
  39. package/dist/server/sync/fingerprint.d.ts +6 -0
  40. package/dist/server/sync/index.d.ts +2 -2
  41. package/dist/server/sync/memory-adapter.d.ts +17 -0
  42. package/dist/server/sync/plugin.d.ts +12 -195
  43. package/dist/server/sync/response.d.ts +4 -0
  44. package/dist/server/sync/stream.d.ts +24 -5
  45. package/dist/shared/prefix.d.ts +15 -0
  46. package/package.json +6 -1
  47. package/src/adapter/README.md +13 -13
  48. package/src/adapter/bun.ts +113 -50
  49. package/src/adapter/package.ts +206 -0
  50. package/src/build/client.ts +158 -152
  51. package/src/build/compile-entry.ts +114 -109
  52. package/src/build/entry-template.ts +206 -156
  53. package/src/build/hydrate.ts +342 -340
  54. package/src/build/index.ts +223 -152
  55. package/src/build/route-types.ts +145 -145
  56. package/src/build/scan-server.ts +111 -95
  57. package/src/build/server-routes-entry.ts +30 -36
  58. package/src/build/shared.ts +80 -80
  59. package/src/build/ssg-cache.ts +36 -32
  60. package/src/build/types.ts +125 -89
  61. package/src/cli/config.ts +9 -3
  62. package/src/cli/index.ts +21 -3
  63. package/src/client/router/abort.ts +5 -0
  64. package/src/client/router/link-utils.ts +184 -184
  65. package/src/client/router/provider.tsx +883 -804
  66. package/src/client/router/sync-catch-up.ts +113 -0
  67. package/src/client/router/types.ts +276 -276
  68. package/src/client/sync.ts +98 -98
  69. package/src/client.ts +369 -369
  70. package/src/config.ts +14 -1
  71. package/src/furin.ts +619 -465
  72. package/src/server/auto-invalidate/registry.ts +23 -1
  73. package/src/server/auto-invalidate/runtime.ts +159 -134
  74. package/src/server/cache/dev-loader.ts +77 -38
  75. package/src/server/cache/invalidation.ts +71 -30
  76. package/src/server/cache/isr.ts +20 -5
  77. package/src/server/cache/registry.ts +17 -20
  78. package/src/server/cache/ssg.ts +19 -4
  79. package/src/server/cache/store-view.ts +30 -0
  80. package/src/server/dev-page-plugin.ts +454 -454
  81. package/src/server/instance.ts +252 -0
  82. package/src/server/internal.ts +100 -5
  83. package/src/server/render/assemble.ts +160 -160
  84. package/src/server/render/isr.ts +323 -323
  85. package/src/server/render/not-found.ts +9 -3
  86. package/src/server/render/ssr.ts +603 -585
  87. package/src/server/render/template.ts +57 -24
  88. package/src/server/router/plugin.ts +384 -384
  89. package/src/server/sync/adapter.ts +65 -0
  90. package/src/server/sync/config.ts +60 -29
  91. package/src/server/sync/fingerprint.ts +44 -0
  92. package/src/server/sync/index.ts +15 -14
  93. package/src/server/sync/memory-adapter.ts +196 -0
  94. package/src/server/sync/plugin.ts +199 -108
  95. package/src/server/sync/response.ts +77 -0
  96. package/src/server/sync/stream.ts +162 -144
  97. package/src/shared/deferred-ndjson.ts +7 -0
  98. package/src/shared/prefix.ts +38 -0
@@ -1,3 +1,5 @@
1
+ import { type FurinInstance, instanceSlot } from "../instance.ts";
2
+
1
3
  export class AutoInvalidateRegistry {
2
4
  private readonly pathToTags = new Map<string, Set<string>>();
3
5
  private readonly tagToPaths = new Map<string, Set<string>>();
@@ -57,4 +59,24 @@ export class AutoInvalidateRegistry {
57
59
  }
58
60
  }
59
61
 
60
- export const autoInvalidateRegistry = new AutoInvalidateRegistry();
62
+ const instanceAutoInvalidateRegistry = instanceSlot(() => new AutoInvalidateRegistry());
63
+
64
+ /** The current furin instance's registry (see server/instance.ts). */
65
+ export function getAutoInvalidateRegistry(instance?: FurinInstance): AutoInvalidateRegistry {
66
+ return instanceAutoInvalidateRegistry(instance);
67
+ }
68
+
69
+ /**
70
+ * Instance-scoped facade kept under the historical singleton name so call
71
+ * sites read the same — every method resolves the current instance's registry.
72
+ */
73
+ export const autoInvalidateRegistry: Pick<
74
+ AutoInvalidateRegistry,
75
+ "registerLoaderTags" | "pathsForTags" | "unregisterPath" | "reset"
76
+ > = {
77
+ registerLoaderTags: (urlPath, tags) =>
78
+ instanceAutoInvalidateRegistry().registerLoaderTags(urlPath, tags),
79
+ pathsForTags: (tags) => instanceAutoInvalidateRegistry().pathsForTags(tags),
80
+ unregisterPath: (urlPath) => instanceAutoInvalidateRegistry().unregisterPath(urlPath),
81
+ reset: () => instanceAutoInvalidateRegistry().reset(),
82
+ };
@@ -1,134 +1,159 @@
1
- import type { Context } from "elysia";
2
- import { consumePendingInvalidations, revalidatePath } from "../cache/invalidation.ts";
3
- import { autoInvalidateRegistry } from "./registry.ts";
4
- import type { InvalidationInput, InvalidationRule } from "./types.ts";
5
-
6
- function toRules(input: InvalidationInput): readonly InvalidationRule[] {
7
- return Array.isArray(input) ? input : [input as InvalidationRule];
8
- }
9
-
10
- function statusFromResponseValue(responseValue: unknown): number | undefined {
11
- if (responseValue instanceof Response) {
12
- return responseValue.status;
13
- }
14
- if (responseValue && typeof responseValue === "object" && "status" in responseValue) {
15
- const status = (responseValue as { status?: unknown }).status;
16
- if (typeof status === "number") {
17
- return status;
18
- }
19
- }
20
- if (responseValue && typeof responseValue === "object" && "code" in responseValue) {
21
- const code = (responseValue as { code?: unknown }).code;
22
- if (typeof code === "number") {
23
- return code;
24
- }
25
- }
26
- return;
27
- }
28
-
29
- export function isSuccessfulMutationResponse(
30
- ctx: Pick<Context, "set"> & {
31
- responseValue?: unknown;
32
- response?: unknown;
33
- }
34
- ): boolean {
35
- const responseStatus =
36
- statusFromResponseValue(ctx.responseValue) ?? statusFromResponseValue(ctx.response);
37
- if (responseStatus !== undefined) {
38
- return responseStatus >= 200 && responseStatus < 400;
39
- }
40
-
41
- const setStatus = ctx.set.status;
42
- if (typeof setStatus === "number") {
43
- return setStatus >= 200 && setStatus < 400;
44
- }
45
- if (typeof setStatus === "string") {
46
- // Elysia allows string status codes (e.g. "Not Found"). We cannot map
47
- // every possible string to its numeric code without the StatusMap, so we
48
- // treat known client-error (4xx) and server-error (5xx) texts as failures
49
- // and everything else as success (1xx, 2xx, 3xx, and any unknown string).
50
- const errorStatusTexts = new Set([
51
- "Bad Request",
52
- "Unauthorized",
53
- "Payment Required",
54
- "Forbidden",
55
- "Not Found",
56
- "Method Not Allowed",
57
- "Not Acceptable",
58
- "Proxy Authentication Required",
59
- "Request Timeout",
60
- "Conflict",
61
- "Gone",
62
- "Length Required",
63
- "Precondition Failed",
64
- "Payload Too Large",
65
- "URI Too Long",
66
- "Unsupported Media Type",
67
- "Range Not Satisfiable",
68
- "Expectation Failed",
69
- "I'm a teapot",
70
- "Misdirected Request",
71
- "Unprocessable Content",
72
- "Locked",
73
- "Failed Dependency",
74
- "Too Early",
75
- "Upgrade Required",
76
- "Precondition Required",
77
- "Too Many Requests",
78
- "Enhance Your Calm",
79
- "Request Header Fields Too Large",
80
- "Unavailable For Legal Reasons",
81
- "Internal Server Error",
82
- "Not Implemented",
83
- "Bad Gateway",
84
- "Service Unavailable",
85
- "Gateway Timeout",
86
- "HTTP Version Not Supported",
87
- "Variant Also Negotiates",
88
- "Insufficient Storage",
89
- "Loop Detected",
90
- "Not Extended",
91
- "Network Authentication Required",
92
- ]);
93
- return !errorStatusTexts.has(setStatus);
94
- }
95
- // No explicit status was set — default to success (200).
96
- return true;
97
- }
98
-
99
- export function appendPendingInvalidationHeader(set: Context["set"]): string[] {
100
- const pending = consumePendingInvalidations();
101
- if (pending.length === 0) {
102
- return [];
103
- }
104
-
105
- const headerName = "x-furin-revalidate";
106
- const existing = set.headers[headerName];
107
- set.headers[headerName] =
108
- typeof existing === "string" && existing.length > 0
109
- ? `${existing},${pending.join(",")}`
110
- : pending.join(",");
111
- return pending;
112
- }
113
-
114
- export function revalidateTag(tags: string | readonly string[]): boolean {
115
- const tagList = typeof tags === "string" ? [tags] : [...tags];
116
- let deleted = false;
117
- for (const path of autoInvalidateRegistry.pathsForTags(tagList)) {
118
- deleted = revalidatePath(path, "page") || deleted;
119
- }
120
- return deleted;
121
- }
122
-
123
- export function runInvalidationRules(input: InvalidationInput): boolean {
124
- let deleted = false;
125
- for (const rule of toRules(input)) {
126
- if ("path" in rule && rule.path) {
127
- deleted = revalidatePath(rule.path, rule.type) || deleted;
128
- }
129
- if (rule.tags && rule.tags.length > 0) {
130
- deleted = revalidateTag(rule.tags) || deleted;
131
- }
132
- }
133
- return deleted;
134
- }
1
+ import type { Context } from "elysia";
2
+ import {
3
+ callCachePurger,
4
+ consumePendingInvalidations,
5
+ revalidatePath,
6
+ revalidatePathForInstance,
7
+ } from "../cache/invalidation.ts";
8
+ import { allInstances } from "../instance.ts";
9
+ import { getAutoInvalidateRegistry } from "./registry.ts";
10
+ import type { InvalidationInput, InvalidationRule } from "./types.ts";
11
+
12
+ function toRules(input: InvalidationInput): readonly InvalidationRule[] {
13
+ return Array.isArray(input) ? input : [input as InvalidationRule];
14
+ }
15
+
16
+ function statusFromResponseValue(responseValue: unknown): number | undefined {
17
+ if (responseValue instanceof Response) {
18
+ return responseValue.status;
19
+ }
20
+ if (responseValue && typeof responseValue === "object" && "status" in responseValue) {
21
+ const status = (responseValue as { status?: unknown }).status;
22
+ if (typeof status === "number") {
23
+ return status;
24
+ }
25
+ }
26
+ if (responseValue && typeof responseValue === "object" && "code" in responseValue) {
27
+ const code = (responseValue as { code?: unknown }).code;
28
+ if (typeof code === "number") {
29
+ return code;
30
+ }
31
+ }
32
+ return;
33
+ }
34
+
35
+ export function isSuccessfulMutationResponse(
36
+ ctx: Pick<Context, "set"> & {
37
+ responseValue?: unknown;
38
+ response?: unknown;
39
+ }
40
+ ): boolean {
41
+ const responseStatus =
42
+ statusFromResponseValue(ctx.responseValue) ?? statusFromResponseValue(ctx.response);
43
+ if (responseStatus !== undefined) {
44
+ return responseStatus >= 200 && responseStatus < 400;
45
+ }
46
+
47
+ const setStatus = ctx.set.status;
48
+ if (typeof setStatus === "number") {
49
+ return setStatus >= 200 && setStatus < 400;
50
+ }
51
+ if (typeof setStatus === "string") {
52
+ // Elysia allows string status codes (e.g. "Not Found"). We cannot map
53
+ // every possible string to its numeric code without the StatusMap, so we
54
+ // treat known client-error (4xx) and server-error (5xx) texts as failures
55
+ // and everything else as success (1xx, 2xx, 3xx, and any unknown string).
56
+ const errorStatusTexts = new Set([
57
+ "Bad Request",
58
+ "Unauthorized",
59
+ "Payment Required",
60
+ "Forbidden",
61
+ "Not Found",
62
+ "Method Not Allowed",
63
+ "Not Acceptable",
64
+ "Proxy Authentication Required",
65
+ "Request Timeout",
66
+ "Conflict",
67
+ "Gone",
68
+ "Length Required",
69
+ "Precondition Failed",
70
+ "Payload Too Large",
71
+ "URI Too Long",
72
+ "Unsupported Media Type",
73
+ "Range Not Satisfiable",
74
+ "Expectation Failed",
75
+ "I'm a teapot",
76
+ "Misdirected Request",
77
+ "Unprocessable Content",
78
+ "Locked",
79
+ "Failed Dependency",
80
+ "Too Early",
81
+ "Upgrade Required",
82
+ "Precondition Required",
83
+ "Too Many Requests",
84
+ "Enhance Your Calm",
85
+ "Request Header Fields Too Large",
86
+ "Unavailable For Legal Reasons",
87
+ "Internal Server Error",
88
+ "Not Implemented",
89
+ "Bad Gateway",
90
+ "Service Unavailable",
91
+ "Gateway Timeout",
92
+ "HTTP Version Not Supported",
93
+ "Variant Also Negotiates",
94
+ "Insufficient Storage",
95
+ "Loop Detected",
96
+ "Not Extended",
97
+ "Network Authentication Required",
98
+ ]);
99
+ return !errorStatusTexts.has(setStatus);
100
+ }
101
+ // No explicit status was set — default to success (200).
102
+ return true;
103
+ }
104
+
105
+ export function appendPendingInvalidationHeader(set: Context["set"]): string[] {
106
+ const pending = consumePendingInvalidations();
107
+ if (pending.length === 0) {
108
+ return [];
109
+ }
110
+
111
+ const headerName = "x-furin-revalidate";
112
+ const existing = set.headers[headerName];
113
+ set.headers[headerName] =
114
+ typeof existing === "string" && existing.length > 0
115
+ ? `${existing},${pending.join(",")}`
116
+ : pending.join(",");
117
+ return pending;
118
+ }
119
+
120
+ export function revalidateTag(tags: string | readonly string[]): boolean {
121
+ const tagList = typeof tags === "string" ? [tags] : [...tags];
122
+ // Tags are cross-app by design: with several mounted furin instances a
123
+ // shared mutation must be able to invalidate pages rendered by any of them.
124
+ // But each instance's tag-registered paths are evicted from THAT instance's
125
+ // caches only — the cross-app fan-out of `revalidatePath` would also evict
126
+ // a sibling app's unrelated page that merely shares the pathname.
127
+ let deleted = false;
128
+ const purgedPaths = new Set<string>();
129
+ for (const instance of allInstances()) {
130
+ // Registry paths are LOGICAL (unprefixed); the CDN caches the PHYSICAL
131
+ // request URL, so prefix each with the instance's mount prefix before
132
+ // queueing it for purge — otherwise a mounted app's `/admin/x` stays stale.
133
+ for (const path of getAutoInvalidateRegistry(instance).pathsForTags(tagList)) {
134
+ const result = revalidatePathForInstance(instance, path, "page");
135
+ deleted = result.deleted || deleted;
136
+ purgedPaths.add(`${instance.prefix}${path}`);
137
+ for (const purged of result.purgedPaths) {
138
+ purgedPaths.add(`${instance.prefix}${purged}`);
139
+ }
140
+ }
141
+ }
142
+ // One batched CDN purge — the CDN sits in front of every mounted app, so
143
+ // physical paths from all instances go out together, deduped.
144
+ callCachePurger([...purgedPaths]);
145
+ return deleted;
146
+ }
147
+
148
+ export function runInvalidationRules(input: InvalidationInput): boolean {
149
+ let deleted = false;
150
+ for (const rule of toRules(input)) {
151
+ if ("path" in rule && rule.path) {
152
+ deleted = revalidatePath(rule.path, rule.type) || deleted;
153
+ }
154
+ if (rule.tags && rule.tags.length > 0) {
155
+ deleted = revalidateTag(rule.tags) || deleted;
156
+ }
157
+ }
158
+ return deleted;
159
+ }
@@ -1,5 +1,12 @@
1
1
  import { statSync } from "node:fs";
2
2
  import { autoInvalidateRegistry } from "../auto-invalidate/registry";
3
+ import {
4
+ allStateBuckets,
5
+ currentInstance,
6
+ type FurinInstance,
7
+ instanceSlot,
8
+ withInstance,
9
+ } from "../instance.ts";
3
10
  import { registerCacheInvalidator } from "./registry";
4
11
  import { createRouteCache, type RevalidateType } from "./route-cache";
5
12
 
@@ -18,42 +25,54 @@ export interface InvalidateOutcome {
18
25
  ssg: number;
19
26
  }
20
27
 
21
- const sourceFileToCacheKeys = new Map<string, Set<string>>();
22
-
23
28
  interface CacheKindHandle {
24
29
  cache: ReturnType<typeof createDevLoaderCache>;
25
30
  kind: "isr" | "ssg";
26
31
  }
27
32
 
28
- function indexEntryDependencies(cacheKey: string, deps: string[]): void {
33
+ interface DevLoaderState {
34
+ isr: CacheKindHandle;
35
+ sourceFileToCacheKeys: Map<string, Set<string>>;
36
+ ssg: CacheKindHandle;
37
+ }
38
+
39
+ function indexEntryDependencies(
40
+ index: Map<string, Set<string>>,
41
+ cacheKey: string,
42
+ deps: string[]
43
+ ): void {
29
44
  for (const dep of deps) {
30
- let bucket = sourceFileToCacheKeys.get(dep);
45
+ let bucket = index.get(dep);
31
46
  if (!bucket) {
32
47
  bucket = new Set<string>();
33
- sourceFileToCacheKeys.set(dep, bucket);
48
+ index.set(dep, bucket);
34
49
  }
35
50
  bucket.add(cacheKey);
36
51
  }
37
52
  }
38
53
 
39
- function unindexEntryDependencies(cacheKey: string, deps: string[]): void {
54
+ function unindexEntryDependencies(
55
+ index: Map<string, Set<string>>,
56
+ cacheKey: string,
57
+ deps: string[]
58
+ ): void {
40
59
  for (const dep of deps) {
41
- const bucket = sourceFileToCacheKeys.get(dep);
60
+ const bucket = index.get(dep);
42
61
  if (!bucket) {
43
62
  continue;
44
63
  }
45
64
  bucket.delete(cacheKey);
46
65
  if (bucket.size === 0) {
47
- sourceFileToCacheKeys.delete(dep);
66
+ index.delete(dep);
48
67
  }
49
68
  }
50
69
  }
51
70
 
52
- function createDevLoaderCache(name: string) {
71
+ function createDevLoaderCache(name: string, index: Map<string, Set<string>>) {
53
72
  return createRouteCache<DevLoaderCacheEntry>({
54
73
  name,
55
74
  onDelete: (key, entry) => {
56
- unindexEntryDependencies(key, entry.dependencies);
75
+ unindexEntryDependencies(index, key, entry.dependencies);
57
76
  const urlPath = urlPathFromCacheKey(key);
58
77
  if (urlPath) {
59
78
  autoInvalidateRegistry.unregisterPath(urlPath);
@@ -61,38 +80,43 @@ function createDevLoaderCache(name: string) {
61
80
  },
62
81
  onSet: (key, entry, previous) => {
63
82
  if (previous) {
64
- unindexEntryDependencies(key, previous.dependencies);
83
+ unindexEntryDependencies(index, key, previous.dependencies);
65
84
  }
66
- indexEntryDependencies(key, entry.dependencies);
85
+ indexEntryDependencies(index, key, entry.dependencies);
67
86
  },
68
87
  pathFromKey: urlPathFromCacheKey,
69
88
  });
70
89
  }
71
90
 
72
- export const devISRLoaderCache = createDevLoaderCache("render:dev-isr-loader");
73
- export const devSSGLoaderCache = createDevLoaderCache("render:dev-ssg-loader");
74
-
75
- const isrHandle: CacheKindHandle = { cache: devISRLoaderCache, kind: "isr" };
76
- const ssgHandle: CacheKindHandle = { cache: devSSGLoaderCache, kind: "ssg" };
77
-
78
- function setEntry(handle: CacheKindHandle, key: string, entry: DevLoaderCacheEntry): void {
79
- handle.cache.set(key, entry);
80
- }
91
+ // Per-instance dev loader caches + source-dependency index — two furin
92
+ // instances in one dev process must not invalidate each other's loader data.
93
+ const instanceDevLoaderState = instanceSlot((instance): DevLoaderState => {
94
+ const sourceFileToCacheKeys = new Map<string, Set<string>>();
95
+ const isrCache = createDevLoaderCache("render:dev-isr-loader", sourceFileToCacheKeys);
96
+ const ssgCache = createDevLoaderCache("render:dev-ssg-loader", sourceFileToCacheKeys);
97
+ registerCacheInvalidator(isrCache, instance);
98
+ registerCacheInvalidator(ssgCache, instance);
99
+ return {
100
+ isr: { cache: isrCache, kind: "isr" },
101
+ sourceFileToCacheKeys,
102
+ ssg: { cache: ssgCache, kind: "ssg" },
103
+ };
104
+ });
81
105
 
82
106
  export function getDevISRLoaderCache(key: string): DevLoaderCacheEntry | undefined {
83
- return devISRLoaderCache.get(key);
107
+ return instanceDevLoaderState().isr.cache.get(key);
84
108
  }
85
109
 
86
110
  export function setDevISRLoaderCache(key: string, entry: DevLoaderCacheEntry): void {
87
- setEntry(isrHandle, key, entry);
111
+ instanceDevLoaderState().isr.cache.set(key, entry);
88
112
  }
89
113
 
90
114
  export function getDevSSGLoaderCache(key: string): DevLoaderCacheEntry | undefined {
91
- return devSSGLoaderCache.get(key);
115
+ return instanceDevLoaderState().ssg.cache.get(key);
92
116
  }
93
117
 
94
118
  export function setDevSSGLoaderCache(key: string, entry: DevLoaderCacheEntry): void {
95
- setEntry(ssgHandle, key, entry);
119
+ instanceDevLoaderState().ssg.cache.set(key, entry);
96
120
  }
97
121
 
98
122
  export function urlPathFromCacheKey(key: string): string | null {
@@ -107,6 +131,7 @@ export function invalidateDevLoaderCacheByPath(
107
131
  path: string,
108
132
  type: RevalidateType
109
133
  ): InvalidateOutcome {
134
+ const state = instanceDevLoaderState();
110
135
  const cleared: string[] = [];
111
136
  let isr = 0;
112
137
  let ssg = 0;
@@ -119,7 +144,7 @@ export function invalidateDevLoaderCacheByPath(
119
144
  return urlPath === path || urlPath.startsWith(prefix);
120
145
  };
121
146
 
122
- for (const handle of [isrHandle, ssgHandle]) {
147
+ for (const handle of [state.isr, state.ssg]) {
123
148
  for (const key of [...handle.cache.keys()]) {
124
149
  const urlPath = urlPathFromCacheKey(key);
125
150
  if (urlPath === null || !matches(urlPath)) {
@@ -143,7 +168,8 @@ export function invalidateDevLoaderCacheByPath(
143
168
  }
144
169
 
145
170
  export function invalidateDevLoaderCacheBySource(filePath: string): InvalidateOutcome {
146
- const keys = sourceFileToCacheKeys.get(filePath);
171
+ const state = instanceDevLoaderState();
172
+ const keys = state.sourceFileToCacheKeys.get(filePath);
147
173
  if (!keys || keys.size === 0) {
148
174
  return { cleared: [], isr: 0, ssg: 0 };
149
175
  }
@@ -154,16 +180,16 @@ export function invalidateDevLoaderCacheBySource(filePath: string): InvalidateOu
154
180
 
155
181
  const snapshot = [...keys];
156
182
  for (const key of snapshot) {
157
- const isrEntry = devISRLoaderCache.get(key);
183
+ const isrEntry = state.isr.cache.get(key);
158
184
  if (isrEntry) {
159
- devISRLoaderCache.delete(key);
185
+ state.isr.cache.delete(key);
160
186
  cleared.push(key);
161
187
  isr++;
162
188
  continue;
163
189
  }
164
- const ssgEntry = devSSGLoaderCache.get(key);
190
+ const ssgEntry = state.ssg.cache.get(key);
165
191
  if (ssgEntry) {
166
- devSSGLoaderCache.delete(key);
192
+ state.ssg.cache.delete(key);
167
193
  cleared.push(key);
168
194
  ssg++;
169
195
  }
@@ -195,17 +221,30 @@ export function isDevLoaderCacheValid(entry: DevLoaderCacheEntry): boolean {
195
221
  }
196
222
 
197
223
  export function getAllDevISRLoaderEntries(): [string, DevLoaderCacheEntry][] {
198
- return [...devISRLoaderCache.entries()];
224
+ return [...instanceDevLoaderState().isr.cache.entries()];
199
225
  }
200
226
 
201
227
  export function getAllDevSSGLoaderEntries(): [string, DevLoaderCacheEntry][] {
202
- return [...devSSGLoaderCache.entries()];
228
+ return [...instanceDevLoaderState().ssg.cache.entries()];
229
+ }
230
+
231
+ /** @internal — clears dev loader caches for `instance` (default: current). */
232
+ export function clearDevLoaderCaches(instance?: FurinInstance): void {
233
+ const target = instance ?? currentInstance();
234
+ const state = instanceDevLoaderState(target);
235
+ // `.clear()` fires onDelete per entry, which unregisters the path from the
236
+ // auto-invalidate registry via the `currentInstance()`-scoped facade — bind
237
+ // the scope here so the unregistration hits the cleared instance's registry
238
+ // (re-entering an already-active identical scope is harmless).
239
+ withInstance(target, () => {
240
+ state.isr.cache.clear();
241
+ state.ssg.cache.clear();
242
+ });
243
+ state.sourceFileToCacheKeys.clear();
203
244
  }
204
245
 
205
246
  export function __resetDevLoaderCacheState(): void {
206
- devISRLoaderCache.clear();
207
- devSSGLoaderCache.clear();
208
- sourceFileToCacheKeys.clear();
209
- registerCacheInvalidator(devISRLoaderCache);
210
- registerCacheInvalidator(devSSGLoaderCache);
247
+ for (const instance of allStateBuckets()) {
248
+ clearDevLoaderCaches(instance);
249
+ }
211
250
  }