akanjs 3.2.0-rc.8 → 3.2.0-rc.9

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/CHANGELOG.md CHANGED
@@ -463,6 +463,7 @@
463
463
  - **The cache stays bounded at 32 entries.** A patch whose full render can no longer come back is dropped. `clearRscNavigationCache()`, `refreshRsc()`, an HMR refresh and an account change still drop every cached patch.
464
464
  - **The default is unchanged.** Partial commit is still off unless the flag is set.
465
465
 
466
+ - c723e7a: With `AKAN_PUBLIC_RSC_PARTIAL_COMMIT=1`, a Link prefetch now carries the router state, so a prefetched sibling page comes back as a patch. A prefetched patch is used only under the full commit it was requested from, and a link the patch cache would replay is no longer prefetched.
466
467
  - 6c3a303: A page that renders a string of 1024 characters or more, such as a post body or a long comment, now renders and hydrates whatever its lines say. React sends such a string, and every typed array or `ArrayBuffer`, as a length-prefixed Flight row whose raw bytes may hold newlines. The SSR sanitizer, the client navigation redirect guard and the partial-navigation patch check all split the stream on every newline, so they read lines inside the text as rows of their own. A line shaped like a debug row was dropped and a line shaped like a redirect row was rewritten, which broke the render. On a client navigation the guard could act on such a line as a redirect, and a partial navigation fell back to a full one.
467
468
 
468
469
  All three now share one Flight row reader that follows React's row headers. It skips a length-prefixed row by its byte length across any chunk boundary, and it checks only real rows, which keep their sanitizing and redirect handling. Long text is no longer split and pattern-tested line by line. The server-rendered HTML also carries one inline script per Flight row instead of one per line, about 9% less inline-script HTML on a 400 KB payload.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.2.0-rc.8",
3
+ "version": "3.2.0-rc.9",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -323,7 +323,12 @@ const readAuthCookies = () =>
323
323
  .join("; ");
324
324
  const rscCache = new Map<string, RscCacheNode>();
325
325
  const rscPatchCache = new Map<string, RscPatchNavigationCacheNode<RscThenable>>();
326
- const rscPrefetchCache = new RscPrefetchCache({ fetchResponse: fetchRscPrefetchResponse });
326
+
327
+ const prefetchBase = () => (isAkanRscPartialCommitEnabled() ? currentFullNode : null);
328
+ const rscPrefetchCache = new RscPrefetchCache<RscCacheNode | null>({
329
+ fetchResponse: (href, signal) => fetchRscPrefetchResponse(href, signal, currentRouterState),
330
+ baseOf: prefetchBase,
331
+ });
327
332
 
328
333
  StaleReads.subscribe(() => rscPrefetchCache.clear());
329
334
  const initialThenable = createRscThenable(createInitialRscStream());
@@ -466,17 +471,20 @@ function replayedPageOf(target: string): RscCacheNode | undefined {
466
471
  return node;
467
472
  }
468
473
 
469
- function replayCachedPatch(target: string, navId: number, replace?: boolean, requestScroll?: () => void): boolean {
470
- const cachedPatch = rscPatchCache.get(target);
471
- if (!cachedPatch) return false;
472
- const patchResult = resolveCachedRscPatchNavigation({
474
+ const cachedPatchReplayOf = (node: RscPatchNavigationCacheNode<RscThenable>, navId?: number) =>
475
+ resolveCachedRscPatchNavigation({
473
476
  currentTree: currentSegmentTree,
474
477
  currentBase: currentFullNode,
475
- node: cachedPatch,
478
+ node,
476
479
  partialCommitEnabled: isAkanRscPartialCommitEnabled(),
477
480
  navId,
478
481
  getCurrentNavId: () => navigationSeq,
479
482
  });
483
+
484
+ function replayCachedPatch(target: string, navId: number, replace?: boolean, requestScroll?: () => void): boolean {
485
+ const cachedPatch = rscPatchCache.get(target);
486
+ if (!cachedPatch) return false;
487
+ const patchResult = cachedPatchReplayOf(cachedPatch, navId);
480
488
  if (patchResult.status === "patched") {
481
489
  const { tree, patchedNode, outletKey, headSnapshot } = patchResult;
482
490
  const patch = { type: "patched" as const, tree, patchedNode, patch: cachedPatch.patch, outletKey, headSnapshot };
@@ -505,8 +513,10 @@ globalThis.__AKAN_RSC_PREFETCH__ = (href, priority) => {
505
513
  if (new URL(target).origin !== window.location.origin || pendingRequest?.target === key) return none;
506
514
 
507
515
  const authCookies = readAuthCookies();
508
- if (authCookies === cachedAuthCookies && cachedPageOf(target)) return none;
509
- return rscPrefetchCache.request(key, authCookies, priority);
516
+ const cachedPatch = rscPatchCache.get(target);
517
+ const replayable = cachedPageOf(target) || (!!cachedPatch && cachedPatchReplayOf(cachedPatch).status === "patched");
518
+ if (authCookies === cachedAuthCookies && replayable) return none;
519
+ return rscPrefetchCache.request(key, authCookies, priority, prefetchBase());
510
520
  };
511
521
 
512
522
  function Root(): ReactNode {
@@ -621,7 +631,7 @@ function Root(): ReactNode {
621
631
  let servedFromCache = !!nextNode;
622
632
  if (!nextNode) {
623
633
  if (replayCachedPatch(target, navId, options.replace, requestScroll)) return;
624
- const taken = rscPrefetchCache.take(pageKeyOf(target), cachedAuthCookies, signal);
634
+ const taken = rscPrefetchCache.take(pageKeyOf(target), cachedAuthCookies, signal, prefetchBase());
625
635
  const prefetched = (await taken?.response) ?? undefined;
626
636
  if (signal.aborted) return;
627
637
  servedFromCache = !!prefetched && !taken?.fresh;
@@ -644,7 +654,7 @@ function Root(): ReactNode {
644
654
  requestScroll,
645
655
  })
646
656
  ) {
647
- currentCommitFromCache = false;
657
+ currentCommitFromCache = servedFromCache;
648
658
  rememberPatchedRouteState(fetched.tree, fetched.patchedNode);
649
659
  const patchCacheNode = createRscPatchNavigationCacheNode({
650
660
  href: target,
@@ -45,9 +45,14 @@ const fetchKeepingHeaders = async (endpoint: URL, init: RequestInit, signal: Abo
45
45
  return response;
46
46
  };
47
47
 
48
- export const fetchRscPrefetchResponse = (href: string, signal: AbortSignal) => {
48
+ export const fetchRscPrefetchResponse = (
49
+ href: string,
50
+ signal: AbortSignal,
51
+ currentRouterState: AkanRouterStateV1 | null = null,
52
+ ) => {
49
53
  const headers = rscRequestHeaders();
50
54
  headers.set(AKAN_RSC_PREFETCH_HEADER, "1");
55
+ if (isAkanRscPartialCommitEnabled()) appendAkanRouterStateRequestHeaders(headers, currentRouterState);
51
56
  return fetchKeepingHeaders(rscEndpointOf(href), { headers, credentials: "same-origin", cache: "no-store" }, signal);
52
57
  };
53
58
 
@@ -1,7 +1,9 @@
1
1
  import type { PrefetchPriority } from "../client/rscNavigation";
2
2
 
3
- interface RscPrefetchEntry {
3
+ interface RscPrefetchEntry<B> {
4
4
  account: string;
5
+ base: B | undefined;
6
+ whole: boolean;
5
7
  startedAt: number;
6
8
  controller: AbortController;
7
9
  response: Promise<Response | null>;
@@ -18,8 +20,9 @@ export interface RscPrefetched {
18
20
  fresh: boolean;
19
21
  }
20
22
 
21
- export interface RscPrefetchCacheOptions {
23
+ export interface RscPrefetchCacheOptions<B = undefined> {
22
24
  fetchResponse: (href: string, signal: AbortSignal) => Promise<Response>;
25
+ baseOf?: () => B;
23
26
  ttlMs?: number;
24
27
  freshMs?: number;
25
28
  maxConcurrent?: number;
@@ -28,7 +31,7 @@ export interface RscPrefetchCacheOptions {
28
31
  now?: () => number;
29
32
  }
30
33
 
31
- export class RscPrefetchCache {
34
+ export class RscPrefetchCache<B = undefined> {
32
35
  static readonly defaultTtlMs = 15_000;
33
36
 
34
37
  static readonly defaultFreshMs = 5_000;
@@ -38,20 +41,22 @@ export class RscPrefetchCache {
38
41
  static readonly defaultMaxQueued = 16;
39
42
  static readonly #none = () => undefined;
40
43
 
41
- #fetchResponse: RscPrefetchCacheOptions["fetchResponse"];
44
+ #fetchResponse: RscPrefetchCacheOptions<B>["fetchResponse"];
45
+ #baseOf: RscPrefetchCacheOptions<B>["baseOf"];
42
46
  #ttlMs: number;
43
47
  #freshMs: number;
44
48
  #maxConcurrent: number;
45
49
  #maxEntries: number;
46
50
  #maxQueued: number;
47
51
  #now: () => number;
48
- #entries = new Map<string, RscPrefetchEntry>();
52
+ #entries = new Map<string, RscPrefetchEntry<B>>();
49
53
  #high = new Map<string, RscPrefetchQueued>();
50
54
  #low = new Map<string, RscPrefetchQueued>();
51
55
  #active = 0;
52
56
 
53
- constructor(options: RscPrefetchCacheOptions) {
57
+ constructor(options: RscPrefetchCacheOptions<B>) {
54
58
  this.#fetchResponse = options.fetchResponse;
59
+ this.#baseOf = options.baseOf;
55
60
  this.#ttlMs = options.ttlMs ?? RscPrefetchCache.defaultTtlMs;
56
61
  this.#freshMs = options.freshMs ?? RscPrefetchCache.defaultFreshMs;
57
62
  this.#maxConcurrent = options.maxConcurrent ?? RscPrefetchCache.defaultMaxConcurrent;
@@ -68,14 +73,14 @@ export class RscPrefetchCache {
68
73
  return this.#high.size + this.#low.size;
69
74
  }
70
75
 
71
- has(href: string, account: string) {
76
+ has(href: string, account: string, base?: B) {
72
77
  const entry = this.#entries.get(href);
73
- return !!entry && entry.account === account && !this.#isStale(entry);
78
+ return !!entry && entry.account === account && entry.base === base && !this.#isStale(entry);
74
79
  }
75
80
 
76
- request(href: string, account: string, priority: PrefetchPriority = "high"): () => void {
81
+ request(href: string, account: string, priority: PrefetchPriority = "high", base?: B): () => void {
77
82
  this.#sweep();
78
- if (this.has(href, account)) return RscPrefetchCache.#none;
83
+ if (this.has(href, account, base)) return RscPrefetchCache.#none;
79
84
  if (priority === "low" && this.#high.has(href)) return RscPrefetchCache.#none;
80
85
  const queue = priority === "high" ? this.#high : this.#low;
81
86
  const held = queue.get(href);
@@ -101,13 +106,14 @@ export class RscPrefetchCache {
101
106
  };
102
107
  }
103
108
 
104
- take(href: string, account: string, signal?: AbortSignal): RscPrefetched | null {
109
+ take(href: string, account: string, signal?: AbortSignal, base?: B): RscPrefetched | null {
105
110
  this.#high.delete(href);
106
111
  this.#low.delete(href);
107
112
  const entry = this.#entries.get(href);
108
113
  if (!entry) return null;
109
114
  this.#entries.delete(href);
110
- if (entry.account !== account || this.#isStale(entry)) {
115
+
116
+ if (entry.account !== account || this.#isStale(entry) || (entry.base !== base && !entry.whole)) {
111
117
  RscPrefetchCache.#drop(entry);
112
118
  return null;
113
119
  }
@@ -167,12 +173,15 @@ export class RscPrefetchCache {
167
173
  if (this.#entries.get(href) === entry) this.#entries.delete(href);
168
174
  };
169
175
  this.#active += 1;
170
- const entry: RscPrefetchEntry = {
176
+ const entry: RscPrefetchEntry<B> = {
171
177
  account,
178
+ base: this.#baseOf?.(),
179
+ whole: false,
172
180
  startedAt: this.#now(),
173
181
  controller,
174
182
  response: this.#fetchResponse(href, controller.signal).then(
175
183
  (response) => {
184
+ entry.whole = response.headers.get("X-Akan-Rsc-Partial") !== "patch";
176
185
 
177
186
  const optedOut = response.status === 204;
178
187
 
@@ -186,7 +195,7 @@ export class RscPrefetchCache {
186
195
 
187
196
  const [drain, branch] = response.body.tee();
188
197
  kept = branch;
189
- void RscPrefetchCache.#readToEnd(drain).then(settle, () => {
198
+ void RscPrefetchCache.#readToEnd(drain, controller.signal).then(settle, () => {
190
199
  forget();
191
200
  settle();
192
201
  });
@@ -222,16 +231,17 @@ export class RscPrefetchCache {
222
231
  }
223
232
  }
224
233
 
225
- #isStale(entry: RscPrefetchEntry): boolean {
234
+ #isStale(entry: RscPrefetchEntry<B>): boolean {
226
235
  return this.#now() - entry.startedAt > this.#ttlMs;
227
236
  }
228
237
 
229
- static async #readToEnd(stream: ReadableStream<Uint8Array>): Promise<void> {
238
+ static async #readToEnd(stream: ReadableStream<Uint8Array>, signal: AbortSignal): Promise<void> {
230
239
  const reader = stream.getReader();
240
+ signal.addEventListener("abort", () => void reader.cancel().catch(() => undefined), { once: true });
231
241
  while (!(await reader.read()).done);
232
242
  }
233
243
 
234
- static #drop(entry: RscPrefetchEntry): void {
244
+ static #drop<B>(entry: RscPrefetchEntry<B>): void {
235
245
  entry.controller.abort();
236
246
  entry.release();
237
247
  }
@@ -13,7 +13,7 @@ export type RscClientFetchResponseResult = {
13
13
  type: "redirected";
14
14
  status?: number;
15
15
  };
16
- export declare const fetchRscPrefetchResponse: (href: string, signal: AbortSignal) => Promise<Response>;
16
+ export declare const fetchRscPrefetchResponse: (href: string, signal: AbortSignal, currentRouterState?: AkanRouterStateV1 | null) => Promise<Response>;
17
17
  export declare function fetchRscNavigationResponse(href: string, options: {
18
18
  buildId?: number;
19
19
  currentRouterState: AkanRouterStateV1 | null;
@@ -3,8 +3,9 @@ export interface RscPrefetched {
3
3
  response: Promise<Response | null>;
4
4
  fresh: boolean;
5
5
  }
6
- export interface RscPrefetchCacheOptions {
6
+ export interface RscPrefetchCacheOptions<B = undefined> {
7
7
  fetchResponse: (href: string, signal: AbortSignal) => Promise<Response>;
8
+ baseOf?: () => B;
8
9
  ttlMs?: number;
9
10
  freshMs?: number;
10
11
  maxConcurrent?: number;
@@ -12,19 +13,19 @@ export interface RscPrefetchCacheOptions {
12
13
  maxQueued?: number;
13
14
  now?: () => number;
14
15
  }
15
- export declare class RscPrefetchCache {
16
+ export declare class RscPrefetchCache<B = undefined> {
16
17
  #private;
17
18
  static readonly defaultTtlMs = 15000;
18
19
  static readonly defaultFreshMs = 5000;
19
20
  static readonly defaultMaxConcurrent = 2;
20
21
  static readonly defaultMaxEntries = 16;
21
22
  static readonly defaultMaxQueued = 16;
22
- constructor(options: RscPrefetchCacheOptions);
23
+ constructor(options: RscPrefetchCacheOptions<B>);
23
24
  get activeCount(): number;
24
25
  get queuedCount(): number;
25
- has(href: string, account: string): boolean;
26
- request(href: string, account: string, priority?: PrefetchPriority): () => void;
27
- take(href: string, account: string, signal?: AbortSignal): RscPrefetched | null;
26
+ has(href: string, account: string, base?: B): boolean;
27
+ request(href: string, account: string, priority?: PrefetchPriority, base?: B): () => void;
28
+ take(href: string, account: string, signal?: AbortSignal, base?: B): RscPrefetched | null;
28
29
  prune(): void;
29
30
  clear(): void;
30
31
  }