@teyik0/furin 0.8.0-alpha.0 → 0.9.0-alpha.0

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 (143) hide show
  1. package/README.md +37 -0
  2. package/dist/adapter/bun/app.d.ts +1 -0
  3. package/dist/adapter/bun.d.ts +3 -1
  4. package/dist/adapter/runtime-build.d.ts +8 -5
  5. package/dist/adapter/vercel.js +8 -8
  6. package/dist/build/client.d.ts +2 -2
  7. package/dist/build/discover-mounts-process.d.ts +1 -1
  8. package/dist/build/discover-pages-process.d.ts +1 -0
  9. package/dist/build/discover-pages.d.ts +11 -0
  10. package/dist/build/discovery-module.d.ts +3 -0
  11. package/dist/build/discovery-plugins.d.ts +34 -0
  12. package/dist/build/elysia-aot.d.ts +1 -1
  13. package/dist/build/entry-template.ts +6 -1
  14. package/dist/build/hydrate.d.ts +3 -2
  15. package/dist/build/index.js +7688 -8458
  16. package/dist/build/preload-manifest.d.ts +2 -2
  17. package/dist/build/route-metadata.d.ts +31 -0
  18. package/dist/build/route-types.d.ts +1 -1
  19. package/dist/build/shared.d.ts +2 -2
  20. package/dist/build/types.d.ts +11 -0
  21. package/dist/cli/dev-worker.d.ts +1 -0
  22. package/dist/cli/dev-worker.ts +123 -0
  23. package/dist/cli/dev.d.ts +1 -0
  24. package/dist/cli/index.js +7811 -8477
  25. package/dist/client/browser-events-runtime.d.ts +2 -2
  26. package/dist/client/browser-events.d.ts +2 -0
  27. package/dist/client/chunks/{client-brdfd773.js → client-rjhxgt64.js} +187 -4
  28. package/dist/client/dev-error-overlay.ts +18 -1
  29. package/dist/client/link.js +110 -156
  30. package/dist/client/query-store.d.ts +5 -0
  31. package/dist/client/router/navigation-controller.d.ts +2 -0
  32. package/dist/client/router/use-fetch-interception.d.ts +1 -1
  33. package/dist/client/router/use-sync-stream.d.ts +1 -1
  34. package/dist/client/{router/sync-catch-up.d.ts → sync-catch-up.d.ts} +5 -3
  35. package/dist/client/sync-client.d.ts +74 -0
  36. package/dist/client.d.ts +1 -0
  37. package/dist/client.js +533 -145
  38. package/dist/config.d.ts +6 -1
  39. package/dist/config.js +3 -1
  40. package/dist/define-route.d.ts +3 -3
  41. package/dist/furin.d.ts +2 -1
  42. package/dist/furin.js +1119 -346
  43. package/dist/plugin/index.js +5 -5
  44. package/dist/plugin/route-registry.d.ts +22 -2
  45. package/dist/rsc/build/index.d.ts +4 -3
  46. package/dist/server/dev/server-only-module.d.ts +5 -0
  47. package/dist/server/internal.js +201 -91
  48. package/dist/server/logging/ingest-plugin.d.ts +1 -2
  49. package/dist/server/production-server.js +36 -33
  50. package/dist/server/server-entry.d.ts +5 -0
  51. package/dist/server/server-entry.js +36 -0
  52. package/dist/server/static-export.js +201 -91
  53. package/dist/server/sync/adapter.d.ts +4 -3
  54. package/dist/server/sync/config.d.ts +7 -0
  55. package/dist/server/sync/connection.d.ts +13 -0
  56. package/dist/server/sync/drizzle/index.js +5 -4
  57. package/dist/server/sync/index.d.ts +3 -0
  58. package/dist/server/sync/index.js +1361 -243
  59. package/dist/server/sync/invalidate-sync.d.ts +6 -2
  60. package/dist/server/sync/local-client.d.ts +9 -0
  61. package/dist/server/sync/postgres/index.d.ts +1 -1
  62. package/dist/server/sync/postgres/index.js +2 -2
  63. package/dist/server/sync/prisma/index.js +2 -2
  64. package/dist/server/sync/redis/index.d.ts +1 -1
  65. package/dist/server/sync/redis/index.js +4 -3
  66. package/dist/server/sync/redis/scripts.d.ts +1 -1
  67. package/dist/server/sync/runtime.d.ts +3 -0
  68. package/dist/server/sync/sqlite/index.d.ts +1 -1
  69. package/dist/server/sync/sqlite/index.js +3 -2
  70. package/dist/shared/browser-events.d.ts +4 -0
  71. package/dist/sync-client.d.ts +3 -0
  72. package/dist/sync-client.js +1600 -0
  73. package/package.json +12 -7
  74. package/src/adapter/README.md +7 -0
  75. package/src/adapter/build.ts +34 -33
  76. package/src/adapter/bun/app.ts +5 -0
  77. package/src/adapter/bun/server.ts +2 -2
  78. package/src/adapter/bun.ts +18 -2
  79. package/src/adapter/furin-app.d.ts +1 -0
  80. package/src/adapter/package.ts +1 -0
  81. package/src/adapter/runtime-build.ts +18 -12
  82. package/src/adapter/static.ts +17 -13
  83. package/src/build/client.ts +7 -5
  84. package/src/build/discover-mounts-process.ts +5 -1
  85. package/src/build/discover-pages-process.ts +151 -0
  86. package/src/build/discover-pages.ts +69 -0
  87. package/src/build/discovery-module.ts +70 -0
  88. package/src/build/discovery-plugins.ts +101 -0
  89. package/src/build/elysia-aot-worker.ts +9 -8
  90. package/src/build/elysia-aot.ts +110 -75
  91. package/src/build/entry-template.ts +6 -1
  92. package/src/build/hydrate.ts +11 -8
  93. package/src/build/index.ts +6 -50
  94. package/src/build/preload-manifest.ts +3 -3
  95. package/src/build/route-metadata.ts +90 -0
  96. package/src/build/route-types.ts +1 -1
  97. package/src/build/shared.ts +6 -8
  98. package/src/build/types.ts +9 -0
  99. package/src/cli/config.ts +4 -0
  100. package/src/cli/dev-worker.ts +123 -0
  101. package/src/cli/dev.ts +91 -0
  102. package/src/cli/index.ts +24 -1
  103. package/src/client/browser-events-runtime.ts +97 -23
  104. package/src/client/browser-events.ts +8 -0
  105. package/src/client/dev-error-overlay.ts +18 -1
  106. package/src/client/query-store.ts +54 -3
  107. package/src/client/router/navigation-controller.ts +45 -12
  108. package/src/client/router/provider.tsx +6 -2
  109. package/src/client/router/use-fetch-interception.ts +1 -1
  110. package/src/client/router/use-sync-stream.ts +96 -44
  111. package/src/client/{router/sync-catch-up.ts → sync-catch-up.ts} +28 -12
  112. package/src/client/sync-client.ts +528 -0
  113. package/src/client/sync.ts +11 -4
  114. package/src/client.ts +10 -0
  115. package/src/config.ts +17 -10
  116. package/src/furin.ts +5 -0
  117. package/src/plugin/route-registry.ts +53 -7
  118. package/src/rsc/build/index.ts +6 -3
  119. package/src/server/browser-events/plugin.ts +79 -25
  120. package/src/server/dev/rewrite-module-specifiers.ts +2 -1
  121. package/src/server/dev/server-only-module.ts +23 -0
  122. package/src/server/dev/symbolicate.ts +4 -3
  123. package/src/server/dev-page-imports.ts +3 -1
  124. package/src/server/dev-page-plugin.ts +18 -1
  125. package/src/server/logging/ingest-plugin.ts +3 -9
  126. package/src/server/server-entry.ts +39 -0
  127. package/src/server/sync/adapter.ts +7 -3
  128. package/src/server/sync/config.ts +12 -6
  129. package/src/server/sync/connection.ts +71 -0
  130. package/src/server/sync/index.ts +3 -0
  131. package/src/server/sync/invalidate-sync.ts +30 -5
  132. package/src/server/sync/local-client.ts +84 -0
  133. package/src/server/sync/plugin.ts +27 -20
  134. package/src/server/sync/postgres/index.ts +9 -2
  135. package/src/server/sync/redis/index.ts +5 -1
  136. package/src/server/sync/redis/scripts.ts +1 -1
  137. package/src/server/sync/runtime.ts +56 -18
  138. package/src/server/sync/sqlite/index.ts +6 -2
  139. package/src/server/sync/stream.ts +17 -5
  140. package/src/shared/browser-events.ts +1 -1
  141. package/src/sync-client.ts +14 -0
  142. package/dist/server/logger.d.ts +0 -10
  143. package/src/server/logger.ts +0 -34
@@ -1,9 +1,10 @@
1
1
  import type { BrowserEventChannel, BrowserEventEnvelope } from "../shared/browser-events.ts";
2
2
 
3
3
  type BrowserEventListener = (event: BrowserEventEnvelope) => void;
4
- type BrowserEventConnectionStatus = "connected" | "connecting" | "reconnecting";
4
+ type BrowserEventConnectionStatus = "connected" | "connecting" | "reconnecting" | "unauthorized";
5
5
 
6
6
  interface BrowserEventRuntime {
7
+ setSyncAuthentication: (authorized: boolean) => void;
7
8
  subscribe: (
8
9
  channel: BrowserEventChannel,
9
10
  listener: BrowserEventListener
@@ -18,13 +19,15 @@ interface BrowserEventCandidate {
18
19
  }
19
20
 
20
21
  interface SyncEventCandidate {
22
+ context?: unknown;
21
23
  cursor?: unknown;
22
24
  }
23
25
 
24
26
  export function installBrowserEventsRuntime(
25
27
  browser: Window,
26
28
  moduleUrlValue: string,
27
- transport?: "sse"
29
+ transport?: "sse",
30
+ sync?: boolean
28
31
  ): void {
29
32
  const runtimeKey = Symbol.for("furin.browser-events.runtime");
30
33
  const runtimeWindow = browser as Window &
@@ -53,7 +56,8 @@ export function installBrowserEventsRuntime(
53
56
  return false;
54
57
  }
55
58
  if (candidate.channel === "sync") {
56
- return typeof (candidate.data as SyncEventCandidate).cursor === "string";
59
+ const data = candidate.data as SyncEventCandidate;
60
+ return typeof data.cursor === "string" || data.context === "changed";
57
61
  }
58
62
  return candidate.channel === "diagnostic" || candidate.channel === "devtools";
59
63
  };
@@ -92,6 +96,9 @@ export function installBrowserEventsRuntime(
92
96
  let eventSource: EventSource | undefined;
93
97
  let status: BrowserEventConnectionStatus = "connecting";
94
98
  let suspended = false;
99
+ let authPaused = false;
100
+ let authorized = !sync;
101
+ let authenticating: AbortController | undefined;
95
102
 
96
103
  const updateStatus = (next: BrowserEventConnectionStatus): void => {
97
104
  if (status === next) {
@@ -136,7 +143,68 @@ export function installBrowserEventsRuntime(
136
143
  }
137
144
  };
138
145
 
146
+ const retry = (): void => {
147
+ if (suspended || authPaused || reconnectTimer !== undefined) {
148
+ return;
149
+ }
150
+ updateStatus("reconnecting");
151
+ const baseDelay = Math.min(250 * 2 ** reconnectAttempt, maxReconnectDelayMs);
152
+ const delay =
153
+ transport === "sse"
154
+ ? baseDelay
155
+ : Math.min(Math.round(baseDelay * (0.8 + Math.random() * 0.4)), maxReconnectDelayMs);
156
+ reconnectAttempt += 1;
157
+ reconnectTimer = browser.setTimeout(() => {
158
+ reconnectTimer = undefined;
159
+ connect();
160
+ }, delay);
161
+ };
162
+
163
+ const authenticate = async (): Promise<void> => {
164
+ if (authenticating) {
165
+ return;
166
+ }
167
+ const current = new AbortController();
168
+ authenticating = current;
169
+ try {
170
+ const response = await browser.fetch(eventsUrl(), {
171
+ method: "HEAD",
172
+ credentials: "same-origin",
173
+ cache: "no-store",
174
+ signal: current.signal,
175
+ });
176
+ if (current.signal.aborted) {
177
+ return;
178
+ }
179
+ if (response.status === 401 || response.status === 403) {
180
+ dispatch({ channel: "sync", data: { context: "changed" }, version: protocolVersion });
181
+ setSyncAuthentication(false);
182
+ return;
183
+ }
184
+ if (!response.ok) {
185
+ throw new Error(`Sync authentication failed: ${response.status}`);
186
+ }
187
+ authorized = true;
188
+ connect();
189
+ } catch {
190
+ if (!current.signal.aborted) {
191
+ retry();
192
+ }
193
+ } finally {
194
+ if (authenticating === current) {
195
+ authenticating = undefined;
196
+ }
197
+ }
198
+ };
199
+
139
200
  const connect = (): void => {
201
+ if (suspended || authPaused) {
202
+ return;
203
+ }
204
+ if (!authorized) {
205
+ authenticate();
206
+ return;
207
+ }
140
208
  if (transport === "sse") {
141
209
  if (
142
210
  suspended ||
@@ -165,12 +233,8 @@ export function installBrowserEventsRuntime(
165
233
  return;
166
234
  }
167
235
  eventSource = undefined;
168
- const delay = Math.min(250 * 2 ** reconnectAttempt, maxReconnectDelayMs);
169
- reconnectAttempt += 1;
170
- reconnectTimer = browser.setTimeout(() => {
171
- reconnectTimer = undefined;
172
- connect();
173
- }, delay);
236
+ authorized = !sync;
237
+ retry();
174
238
  });
175
239
  return;
176
240
  }
@@ -198,23 +262,17 @@ export function installBrowserEventsRuntime(
198
262
  if (suspended || reconnectTimer !== undefined) {
199
263
  return;
200
264
  }
201
- updateStatus("reconnecting");
202
- const baseDelay = Math.min(250 * 2 ** reconnectAttempt, maxReconnectDelayMs);
203
- const delay = Math.min(
204
- Math.round(baseDelay * (0.8 + Math.random() * 0.4)),
205
- maxReconnectDelayMs
206
- );
207
- reconnectAttempt += 1;
208
- reconnectTimer = browser.setTimeout(() => {
209
- reconnectTimer = undefined;
210
- connect();
211
- }, delay);
265
+ authorized = !sync;
266
+ retry();
212
267
  });
213
268
  connection.addEventListener("error", () => connection.close());
214
269
  };
215
270
 
216
271
  const suspend = (): void => {
217
272
  suspended = true;
273
+ authenticating?.abort();
274
+ authenticating = undefined;
275
+ authorized = !sync;
218
276
  if (reconnectTimer !== undefined) {
219
277
  browser.clearTimeout(reconnectTimer);
220
278
  reconnectTimer = undefined;
@@ -225,17 +283,33 @@ export function installBrowserEventsRuntime(
225
283
  eventSource = undefined;
226
284
  };
227
285
  const resume = (): void => {
286
+ if (authPaused) {
287
+ return;
288
+ }
228
289
  suspended = false;
229
290
  if (socket === undefined && eventSource === undefined) {
230
291
  updateStatus("connecting");
231
292
  }
232
293
  connect();
233
294
  };
295
+ const setSyncAuthentication = (valid: boolean): void => {
296
+ if (valid) {
297
+ if (authPaused) {
298
+ authPaused = false;
299
+ resume();
300
+ }
301
+ } else if (!authPaused) {
302
+ authPaused = true;
303
+ suspend();
304
+ updateStatus("unauthorized");
305
+ }
306
+ };
234
307
  browser.addEventListener("pagehide", suspend);
235
308
  browser.addEventListener("pageshow", resume);
236
309
  connect();
237
310
 
238
311
  runtimeWindow[runtimeKey] = {
312
+ setSyncAuthentication,
239
313
  subscribe(channel, listener) {
240
314
  const channelListeners = listeners[channel];
241
315
  channelListeners.add(listener);
@@ -252,10 +326,10 @@ export function installBrowserEventsRuntime(
252
326
  };
253
327
  }
254
328
 
255
- export function browserEventsClientSource(): string {
256
- return `(${installBrowserEventsRuntime.toString()})(window, import.meta.url);\n`;
329
+ export function browserEventsClientSource(sync?: boolean): string {
330
+ return `(${installBrowserEventsRuntime.toString()})(window, import.meta.url, undefined, ${sync === true});\n`;
257
331
  }
258
332
 
259
333
  export function browserEventsSseClientSource(): string {
260
- return `(${installBrowserEventsRuntime.toString()})(window, import.meta.url, "sse");\n`;
334
+ return `(${installBrowserEventsRuntime.toString()})(window, import.meta.url, "sse", true);\n`;
261
335
  }
@@ -3,12 +3,20 @@ import type { BrowserEventChannel, BrowserEventFor } from "../shared/browser-eve
3
3
  const BROWSER_EVENT_RUNTIME_KEY = Symbol.for("furin.browser-events.runtime");
4
4
 
5
5
  interface BrowserEventRuntime {
6
+ setSyncAuthentication?: (authorized: boolean) => void;
6
7
  subscribe: <TChannel extends BrowserEventChannel>(
7
8
  channel: TChannel,
8
9
  listener: (event: BrowserEventFor<TChannel>) => void
9
10
  ) => () => void;
10
11
  }
11
12
 
13
+ /** @internal Router refresh is the existing explicit resume action for cookie authentication. */
14
+ export function setBrowserSyncAuthentication(authorized: boolean): void {
15
+ (globalThis as BrowserEventGlobal)[BROWSER_EVENT_RUNTIME_KEY]?.setSyncAuthentication?.(
16
+ authorized
17
+ );
18
+ }
19
+
12
20
  interface BrowserEventGlobal {
13
21
  [BROWSER_EVENT_RUNTIME_KEY]?: BrowserEventRuntime;
14
22
  }
@@ -52,13 +52,15 @@ const initialState = stateElement
52
52
  : undefined;
53
53
  const endpointSuffix = "/_furin/dev/overlay.js";
54
54
  const blobFramePattern = /blob:https?:\/\/[^\s)]+?(?=:\d+:\d+\)?(?:\n|$))/g;
55
- const sourceMapPattern = /\/\/[#@]\s*sourceMappingURL=([^\s]+)/;
55
+ const sourceMapPattern = /^\s*\/\/[#@]\s*sourceMappingURL=(\S+)\s*$/m;
56
56
  const clientUrl = new URL(import.meta.url);
57
57
  const inferredBasePath = clientUrl.pathname.endsWith(endpointSuffix)
58
58
  ? clientUrl.pathname.slice(0, -endpointSuffix.length)
59
59
  : "";
60
60
  const basePath = initialState?.basePath ?? inferredBasePath;
61
61
  let currentEvent = initialState?.event;
62
+ let clientRenderFailed = false;
63
+ let recoverAfterUpdate = false;
62
64
  let coldFailure = initialState !== undefined;
63
65
  let host: HTMLDivElement | undefined;
64
66
 
@@ -178,9 +180,24 @@ async function reportClientError(detail: ClientErrorDetail): Promise<void> {
178
180
 
179
181
  window.addEventListener("furin:client-error", (event) => {
180
182
  const { detail } = event as CustomEvent<ClientErrorDetail>;
183
+ if (detail.phase === "client-render") {
184
+ clientRenderFailed = true;
185
+ }
181
186
  reportClientError(detail).catch(() => undefined);
182
187
  });
183
188
 
189
+ // Retry only failures from before this update, including when reporting is still
190
+ // in flight or server readiness has already dismissed the overlay.
191
+ window.addEventListener("furin:hmr-before-update", () => {
192
+ recoverAfterUpdate = clientRenderFailed;
193
+ });
194
+ window.addEventListener("furin:hmr-after-update", () => {
195
+ if (recoverAfterUpdate) {
196
+ recoverAfterUpdate = false;
197
+ window.location.reload();
198
+ }
199
+ });
200
+
184
201
  if (currentEvent) {
185
202
  render(currentEvent);
186
203
  }
@@ -19,6 +19,7 @@ export interface ReadResult {
19
19
  }
20
20
 
21
21
  export interface ReadReference {
22
+ application?: object;
22
23
  client: QueryStore;
23
24
  load: (options: unknown) => Promise<ReadResult>;
24
25
  url: string;
@@ -354,6 +355,49 @@ export class QueryStore {
354
355
  return this.epoch;
355
356
  }
356
357
 
358
+ /** Fence in-flight responses without removing active observers or their loaders. */
359
+ clear(preserveData?: boolean): void {
360
+ this.epoch += 1;
361
+ if (!preserveData) {
362
+ this.session = undefined;
363
+ }
364
+ this.projections.clear();
365
+ for (const entry of this.entries.values()) {
366
+ if (!preserveData) {
367
+ entry.base = undefined;
368
+ entry.identity = undefined;
369
+ }
370
+ entry.promise = undefined;
371
+ entry.stale = true;
372
+ entry.version += 1;
373
+ entry.snapshot = { data: entry.base, error: null, isFetching: false };
374
+ this.publish(entry);
375
+ }
376
+ }
377
+
378
+ dispose(): void {
379
+ this.epoch += 1;
380
+ for (const entry of this.entries.values()) {
381
+ entry.listeners.clear();
382
+ }
383
+ this.entries.clear();
384
+ this.requests = [];
385
+ this.projections.clear();
386
+ this.listeners.clear();
387
+ }
388
+
389
+ setError(url: string, error: unknown): void {
390
+ const entry = this.entry(url);
391
+ entry.base = undefined;
392
+ entry.stale = false;
393
+ entry.version += 1;
394
+ entry.snapshot = { data: undefined, error, isFetching: false };
395
+ for (const projection of this.projections) {
396
+ projection.transforms.delete(entry);
397
+ }
398
+ this.publish(entry);
399
+ }
400
+
357
401
  version(url: string): number {
358
402
  return this.entry(url).version;
359
403
  }
@@ -402,7 +446,7 @@ export class QueryStore {
402
446
  const { epoch } = this;
403
447
  entry.snapshot = { ...entry.snapshot, isFetching: true };
404
448
  this.publish(entry);
405
- entry.promise = Promise.resolve()
449
+ const promise = Promise.resolve()
406
450
  .then(() => entry.load?.())
407
451
  .then((result) => {
408
452
  if (!result || epoch !== this.epoch || version !== entry.version || this.pending(entry)) {
@@ -420,6 +464,9 @@ export class QueryStore {
420
464
  }
421
465
  })
422
466
  .finally(() => {
467
+ if (entry.promise !== promise) {
468
+ return;
469
+ }
423
470
  entry.promise = undefined;
424
471
  entry.snapshot = { ...entry.snapshot, isFetching: false };
425
472
  this.publish(entry);
@@ -431,14 +478,18 @@ export class QueryStore {
431
478
  this.fetch(url);
432
479
  }
433
480
  });
434
- return entry.promise;
481
+ entry.promise = promise;
482
+ return promise;
435
483
  }
436
484
 
437
485
  invalidate(identities: readonly QueryIdentity[]): boolean {
438
486
  const tags = new Set(identities.map(queryTag));
439
487
  let affected = false;
440
488
  for (const [url, entry] of this.entries) {
441
- if (entry.identity && queryTags(entry.identity).some((tag) => tags.has(tag))) {
489
+ if (
490
+ (entry.identity && queryTags(entry.identity).some((tag) => tags.has(tag))) ||
491
+ (!entry.identity && entry.load && identities.length > 0)
492
+ ) {
442
493
  affected = true;
443
494
  this.invalidateEntry(url, entry);
444
495
  }
@@ -101,9 +101,10 @@ export class NavigationController {
101
101
  #version = 0;
102
102
  /** In-flight navigation; aborted when superseded or when it ends uncommitted. */
103
103
  #pendingAbort: AbortController | null = null;
104
- /** Owns the visible page's deferred stream; aborted only when another page commits. */
104
+ /** Owns the visible page's deferred stream until replacement or an access-context change. */
105
105
  #committedAbort: AbortController | null = null;
106
- #pendingUserNavigation: Promise<void> | null = null;
106
+ #contextAbort = new AbortController();
107
+ #pendingUserNavigation: { promise: Promise<void>; resolve: () => void } | null = null;
107
108
  #renderedHistoryKey: string | undefined;
108
109
  #pendingScroll: ScrollInstruction | null = null;
109
110
 
@@ -170,7 +171,12 @@ export class NavigationController {
170
171
  const entry: PageCacheEntry = {
171
172
  createdAt: Date.now(),
172
173
  promise: this.#config
173
- .fetchPage({ currentMatch: this.#currentMatch, hmrRefresh: false, href, signal: undefined })
174
+ .fetchPage({
175
+ currentMatch: this.#currentMatch,
176
+ hmrRefresh: false,
177
+ href,
178
+ signal: this.#contextAbort.signal,
179
+ })
174
180
  .then((result) => ({ generation, result, revision: undefined })),
175
181
  staleTime: staleTime ?? this.#config.defaultStaleTime,
176
182
  };
@@ -201,6 +207,25 @@ export class NavigationController {
201
207
  }
202
208
  };
203
209
 
210
+ /** Access changes must also prevent old navigations and deferred streams from publishing. */
211
+ invalidateContext(): void {
212
+ this.#version += 1;
213
+ const pendingAbort = this.#pendingAbort;
214
+ const committedAbort = this.#committedAbort;
215
+ const contextAbort = this.#contextAbort;
216
+ const pendingNavigation = this.#pendingUserNavigation;
217
+ this.#pendingAbort = null;
218
+ this.#committedAbort = null;
219
+ this.#contextAbort = new AbortController();
220
+ this.#pendingUserNavigation = null;
221
+ this.invalidatePath("/", "layout");
222
+ this.#setNavigating(false);
223
+ pendingAbort?.abort();
224
+ committedAbort?.abort();
225
+ contextAbort.abort();
226
+ pendingNavigation?.resolve();
227
+ }
228
+
204
229
  /** Drops cached pages whose state reads an invalidated query. */
205
230
  invalidateQueries(affected: (state: RouterState) => boolean): void {
206
231
  this.#generation.bump((_href, state) => affected(state));
@@ -224,7 +249,7 @@ export class NavigationController {
224
249
  async waitForUserNavigation(): Promise<void> {
225
250
  while (this.#pendingUserNavigation) {
226
251
  // biome-ignore lint/performance/noAwaitInLoops: superseded user navigations must settle in sequence before HMR may refresh the final URL
227
- await this.#pendingUserNavigation;
252
+ await this.#pendingUserNavigation.promise;
228
253
  }
229
254
  }
230
255
 
@@ -271,16 +296,14 @@ export class NavigationController {
271
296
  }
272
297
 
273
298
  #beginUserNavigation(): () => void {
274
- let resolve: () => void = () => undefined;
275
- const promise = new Promise<void>((settle) => {
276
- resolve = settle;
277
- });
278
- this.#pendingUserNavigation = promise;
299
+ this.#pendingUserNavigation?.resolve();
300
+ const navigation = Promise.withResolvers<void>();
301
+ this.#pendingUserNavigation = navigation;
279
302
  return () => {
280
- if (this.#pendingUserNavigation === promise) {
303
+ if (this.#pendingUserNavigation === navigation) {
281
304
  this.#pendingUserNavigation = null;
282
305
  }
283
- resolve();
306
+ navigation.resolve();
284
307
  };
285
308
  }
286
309
 
@@ -309,11 +332,21 @@ export class NavigationController {
309
332
  this.#version += 1;
310
333
  const version = this.#version;
311
334
  const abort = this.#startAbort();
335
+ const cancelled = Promise.withResolvers<false>();
336
+ const onAbort = () => cancelled.resolve(false);
337
+ abort.signal.addEventListener("abort", onAbort, { once: true });
312
338
  let committed = false;
313
339
  this.#setNavigating(true);
314
340
  try {
315
- committed = await this.#load(href, kind, version, abort.signal);
341
+ if (abort.signal.aborted) {
342
+ return;
343
+ }
344
+ committed = await Promise.race([
345
+ this.#load(href, kind, version, abort.signal),
346
+ cancelled.promise,
347
+ ]);
316
348
  } finally {
349
+ abort.signal.removeEventListener("abort", onAbort);
317
350
  this.#settleAbort(abort, committed);
318
351
  finishUserNavigation?.();
319
352
  if (this.#version === version) {
@@ -14,10 +14,12 @@ import { findRouteFailure } from "../../shared/route-matches.ts";
14
14
  import type { SearchParamsInput } from "../../shared/search-params.ts";
15
15
  import type { QuerySeed } from "../../shared/sync-query.ts";
16
16
  import { collectRouteChainFromRoute } from "../../shared/utils/index.ts";
17
+ import { setBrowserSyncAuthentication } from "../browser-events.ts";
17
18
  import { DocumentProvider, useDocumentState } from "../document.tsx";
18
19
  import { QueryStoreContext } from "../query.tsx";
19
20
  import { QueryStore } from "../query-store.ts";
20
21
  import { resolveRouteElement } from "../route-element.tsx";
22
+ import { createInvalidationRefresh } from "../sync-catch-up.ts";
21
23
  import { buildRouterTree } from "./boundary-tree.tsx";
22
24
  import { RouterStoreContext } from "./context.ts";
23
25
  import {
@@ -36,7 +38,6 @@ import { createOptimisticRuntime, registerOptimisticRuntime } from "./optimistic
36
38
  import { fetchPageState } from "./page-fetch.ts";
37
39
  import { createSearchStore, SearchStoreContext } from "./search-store.ts";
38
40
  import { detectStaticMode } from "./spa-response.ts";
39
- import { createInvalidationRefresh } from "./sync-catch-up.ts";
40
41
  import type {
41
42
  ClientRoute,
42
43
  ClientSegmentBoundary,
@@ -417,7 +418,10 @@ export function RouterProvider({
417
418
  invalidatePrefetch,
418
419
  navigate,
419
420
  prefetch,
420
- refresh,
421
+ refresh: (options) => {
422
+ setBrowserSyncAuthentication(true);
423
+ return refresh(options);
424
+ },
421
425
  searchRoutes: routes,
422
426
  }),
423
427
  [
@@ -1,4 +1,5 @@
1
1
  import { useEffect } from "react";
2
+ import type { InvalidationRefresh } from "../sync-catch-up.ts";
2
3
  import { observeWindowFetch } from "./fetch-observers.ts";
3
4
  import {
4
5
  applyRevalidateHeader,
@@ -8,7 +9,6 @@ import {
8
9
  toLogical,
9
10
  } from "./link-utils.ts";
10
11
  import { SYNC_REQUEST } from "./optimistic.ts";
11
- import type { InvalidationRefresh } from "./sync-catch-up.ts";
12
12
 
13
13
  /**
14
14
  * Processes `X-Furin-Revalidate` headers on every same-origin `window.fetch`