@timber-js/app 0.2.0-alpha.224 → 0.2.0-alpha.226
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/dist/_chunks/{actions-7dTXYJfu.js → actions-B1xSIv32.js} +2 -2
- package/dist/_chunks/{actions-7dTXYJfu.js.map → actions-B1xSIv32.js.map} +1 -1
- package/dist/_chunks/{cache-api-TDZ4hlDc.js → cache-api-DsSXA4Sp.js} +68 -47
- package/dist/_chunks/cache-api-DsSXA4Sp.js.map +1 -0
- package/dist/_chunks/{cli-check-Dv3f8d2j.js → cli-check-B33yhAPa.js} +2 -2
- package/dist/_chunks/{cli-check-Dv3f8d2j.js.map → cli-check-B33yhAPa.js.map} +1 -1
- package/dist/_chunks/cli-context-BPdyA7Fp.js +1 -0
- package/dist/_chunks/{cli-context-CbZp32SK.js → cli-context-CwTKMjVe.js} +10 -2
- package/dist/_chunks/cli-context-CwTKMjVe.js.map +1 -0
- package/dist/_chunks/{error-boundary-DRQXGWCp.js → error-boundary-D3YyVVuP.js} +2 -2
- package/dist/_chunks/{error-boundary-DRQXGWCp.js.map → error-boundary-D3YyVVuP.js.map} +1 -1
- package/dist/_chunks/navigation-root-M3B2ZrdO.js.map +1 -1
- package/dist/_chunks/{singleflight-BChPMjwH.js → singleflight-CzoOVB9Y.js} +2 -3
- package/dist/_chunks/singleflight-CzoOVB9Y.js.map +1 -0
- package/dist/_chunks/{status-page-marker-XIQXlTOb.js → status-page-marker-C8POawuF.js} +2 -2
- package/dist/_chunks/{status-page-marker-XIQXlTOb.js.map → status-page-marker-C8POawuF.js.map} +1 -1
- package/dist/adapters/cloudflare-kv-cache.d.ts +0 -1
- package/dist/adapters/cloudflare-kv-cache.d.ts.map +1 -1
- package/dist/adapters/cloudflare-kv-cache.js.map +1 -1
- package/dist/cache/index.d.ts +5 -5
- package/dist/cache/index.d.ts.map +1 -1
- package/dist/cache/index.js +1 -1
- package/dist/cache/redis-handler.d.ts +0 -1
- package/dist/cache/redis-handler.d.ts.map +1 -1
- package/dist/cache/singleflight.d.ts +12 -2
- package/dist/cache/singleflight.d.ts.map +1 -1
- package/dist/cache/stable-stringify.d.ts +6 -0
- package/dist/cache/stable-stringify.d.ts.map +1 -1
- package/dist/cache/tag-aware-handler.d.ts +0 -1
- package/dist/cache/tag-aware-handler.d.ts.map +1 -1
- package/dist/cache/timber-cache.d.ts +1 -1
- package/dist/cache/timber-cache.d.ts.map +1 -1
- package/dist/cli-context.d.ts +8 -0
- package/dist/cli-context.d.ts.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +8 -2
- package/dist/cli.js.map +1 -1
- package/dist/client/error-boundary.js +1 -1
- package/dist/client/internal.js +3 -3
- package/dist/client/navigation-root.d.ts.map +1 -1
- package/dist/client/nuqs-adapter.d.ts +4 -1
- package/dist/client/nuqs-adapter.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +112 -0
- package/dist/index.js.map +1 -1
- package/dist/plugins/client-in-server-package-deps.d.ts +63 -0
- package/dist/plugins/client-in-server-package-deps.d.ts.map +1 -0
- package/dist/plugins/optimize-package-imports.d.ts +11 -0
- package/dist/plugins/optimize-package-imports.d.ts.map +1 -1
- package/dist/server/flight-injector-core.d.ts.map +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/internal.js +162 -33
- package/dist/server/internal.js.map +1 -1
- package/dist/server/pipeline-helpers.d.ts +5 -2
- package/dist/server/pipeline-helpers.d.ts.map +1 -1
- package/dist/server/pipeline.d.ts.map +1 -1
- package/dist/server/prebuilt-runtime.d.ts.map +1 -1
- package/dist/server/response-framing.d.ts +43 -0
- package/dist/server/response-framing.d.ts.map +1 -0
- package/dist/server/ssr-entry.d.ts.map +1 -1
- package/dist/server/ssr-wrappers.d.ts +9 -10
- package/dist/server/ssr-wrappers.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/adapters/cloudflare-kv-cache.ts +1 -5
- package/src/cache/index.ts +7 -27
- package/src/cache/redis-handler.ts +1 -5
- package/src/cache/singleflight.ts +17 -5
- package/src/cache/stable-stringify.ts +53 -0
- package/src/cache/tag-aware-handler.ts +1 -5
- package/src/cache/timber-cache.ts +55 -52
- package/src/cli-context.ts +9 -0
- package/src/cli.ts +16 -0
- package/src/client/navigation-root.tsx +7 -6
- package/src/client/nuqs-adapter.tsx +46 -28
- package/src/index.ts +2 -0
- package/src/plugins/client-in-server-package-deps.ts +139 -0
- package/src/plugins/optimize-package-imports.ts +1 -1
- package/src/server/flight-injector-core.ts +13 -9
- package/src/server/pipeline-helpers.ts +9 -5
- package/src/server/pipeline.ts +7 -0
- package/src/server/prebuilt-runtime.ts +125 -121
- package/src/server/response-framing.ts +59 -0
- package/src/server/ssr-entry.ts +0 -2
- package/src/server/ssr-wrappers.tsx +39 -49
- package/dist/_chunks/cache-api-TDZ4hlDc.js.map +0 -1
- package/dist/_chunks/cli-context-CbZp32SK.js.map +0 -1
- package/dist/_chunks/cli-context-n5hmkdla.js +0 -1
- package/dist/_chunks/singleflight-BChPMjwH.js.map +0 -1
- package/dist/server/nuqs-ssr-provider.d.ts +0 -32
- package/dist/server/nuqs-ssr-provider.d.ts.map +0 -1
- package/src/server/nuqs-ssr-provider.tsx +0 -64
|
@@ -95,10 +95,13 @@ import {
|
|
|
95
95
|
/** Default timeout for component re-renders (overridden by renderTimeoutMs). */
|
|
96
96
|
const DEFAULT_COMPONENT_TIMEOUT_MS = DEFAULT_RENDER_TIMEOUT_MS;
|
|
97
97
|
|
|
98
|
-
/**
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Singleflight for component re-renders (inline and background) and dev
|
|
100
|
+
* captures. One instance for the process: replacing it at boot would drop
|
|
101
|
+
* every flight in progress and let a caller after the swap run a second
|
|
102
|
+
* execution beside one still writing. The timeout is passed per call.
|
|
103
|
+
*/
|
|
104
|
+
const componentSingleflight = createSingleflight();
|
|
102
105
|
let componentTimeoutMs = DEFAULT_COMPONENT_TIMEOUT_MS;
|
|
103
106
|
|
|
104
107
|
/**
|
|
@@ -107,7 +110,6 @@ let componentTimeoutMs = DEFAULT_COMPONENT_TIMEOUT_MS;
|
|
|
107
110
|
*/
|
|
108
111
|
export function setComponentRefreshTimeout(timeoutMs: number): void {
|
|
109
112
|
componentTimeoutMs = timeoutMs;
|
|
110
|
-
componentSingleflight = createSingleflight({ timeoutMs });
|
|
111
113
|
}
|
|
112
114
|
|
|
113
115
|
let warnedNoWaitUntil = false;
|
|
@@ -365,43 +367,68 @@ async function storeCapturedEntry(
|
|
|
365
367
|
);
|
|
366
368
|
}
|
|
367
369
|
|
|
370
|
+
/** A re-render of one component entry: what both re-render paths capture. */
|
|
371
|
+
interface Rerender {
|
|
372
|
+
id: string;
|
|
373
|
+
component: ServerComponent;
|
|
374
|
+
props: Record<string, unknown>;
|
|
375
|
+
cacheKey: string;
|
|
376
|
+
options: PrebuiltComponentOptions;
|
|
377
|
+
hasSeed: boolean;
|
|
378
|
+
}
|
|
379
|
+
|
|
368
380
|
/**
|
|
369
|
-
*
|
|
370
|
-
*
|
|
381
|
+
* Capture flight bytes and store them in the overlay, coalesced into one
|
|
382
|
+
* flight per overlay key. The inline and the background (SWR) re-render
|
|
383
|
+
* share the flight key: two executions for one key could otherwise both
|
|
384
|
+
* store, the older, slower one landing last (TIM-1447). A caller joining
|
|
385
|
+
* a flight started by the other path gets its bytes, or its error.
|
|
386
|
+
*
|
|
387
|
+
* Keyed by the overlay key, which carries the dev namespace: a request
|
|
388
|
+
* after a save never joins a re-render of the pre-save code. `wrap` runs
|
|
389
|
+
* the work inside the flight body — the background path reports its
|
|
390
|
+
* failure there, once per flight.
|
|
371
391
|
*/
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
cacheKey
|
|
377
|
-
options: PrebuiltComponentOptions,
|
|
378
|
-
hasSeed: boolean,
|
|
379
|
-
outcome: string
|
|
380
|
-
): Promise<{ element: unknown } | null> {
|
|
392
|
+
function rerenderFlight(
|
|
393
|
+
r: Rerender,
|
|
394
|
+
wrap?: (signal: AbortSignal, work: () => Promise<Uint8Array>) => Promise<Uint8Array>
|
|
395
|
+
): Promise<Uint8Array> {
|
|
396
|
+
const { id, component, props, cacheKey, options, hasSeed } = r;
|
|
381
397
|
const tags = resolveTags(options, props);
|
|
382
398
|
const oKey = overlayKey(id, cacheKey);
|
|
383
|
-
// Keyed by the overlay key, which carries the dev namespace: a request
|
|
384
|
-
// after a save never joins a re-render of the pre-save code.
|
|
385
399
|
const sfKey = `prebuilt:${oKey}\0${lastInvalidationEpochFor(oKey, tags)}`;
|
|
400
|
+
return componentSingleflight.do(
|
|
401
|
+
sfKey,
|
|
402
|
+
(signal) => {
|
|
403
|
+
const startEpoch = currentInvalidationEpoch();
|
|
404
|
+
const work = async (): Promise<Uint8Array> => {
|
|
405
|
+
const captured = await captureComponentPayload(id, component, props, options);
|
|
406
|
+
// Skip the store if aborted (singleflight timeout) or if the
|
|
407
|
+
// key/tags were invalidated, or a dev save landed, while rendering.
|
|
408
|
+
if (
|
|
409
|
+
!signal.aborted &&
|
|
410
|
+
hasRuntimeLifetime(options) &&
|
|
411
|
+
mayStoreRerender(startEpoch, id, cacheKey, oKey, tags)
|
|
412
|
+
) {
|
|
413
|
+
await storeCapturedEntry(id, cacheKey, captured, options, tags, hasSeed);
|
|
414
|
+
}
|
|
415
|
+
return captured.bytes;
|
|
416
|
+
};
|
|
417
|
+
return wrap ? wrap(signal, work) : work();
|
|
418
|
+
},
|
|
419
|
+
componentTimeoutMs
|
|
420
|
+
);
|
|
421
|
+
}
|
|
386
422
|
|
|
423
|
+
/**
|
|
424
|
+
* Inline re-render: capture (or join the flight capturing) the entry, and
|
|
425
|
+
* revive it for the current request.
|
|
426
|
+
*/
|
|
427
|
+
async function inlineRerender(r: Rerender, outcome: string): Promise<{ element: unknown } | null> {
|
|
387
428
|
try {
|
|
388
|
-
|
|
389
|
-
const startEpoch = currentInvalidationEpoch();
|
|
390
|
-
const captured = await captureComponentPayload(id, component, props, options);
|
|
391
|
-
// Skip the store if aborted (singleflight timeout) or if the
|
|
392
|
-
// key/tags were invalidated, or a dev save landed, while rendering.
|
|
393
|
-
if (
|
|
394
|
-
!signal.aborted &&
|
|
395
|
-
hasRuntimeLifetime(options) &&
|
|
396
|
-
mayStoreRerender(startEpoch, id, cacheKey, oKey, tags)
|
|
397
|
-
) {
|
|
398
|
-
await storeCapturedEntry(id, cacheKey, captured, options, tags, hasSeed);
|
|
399
|
-
}
|
|
400
|
-
return captured.bytes;
|
|
401
|
-
});
|
|
402
|
-
return await revivePayload(id, payload, outcome);
|
|
429
|
+
return await revivePayload(r.id, await rerenderFlight(r), outcome);
|
|
403
430
|
} catch (error) {
|
|
404
|
-
console.error(`[timber] cache.component re-render failed for "${id}" — ${outcome}:`, error);
|
|
431
|
+
console.error(`[timber] cache.component re-render failed for "${r.id}" — ${outcome}:`, error);
|
|
405
432
|
return null;
|
|
406
433
|
}
|
|
407
434
|
}
|
|
@@ -432,44 +459,20 @@ function snapshotRequestContext(store: RequestContextStore): RequestContextStore
|
|
|
432
459
|
* Kick off a background re-render via waitUntil. The re-render runs inside
|
|
433
460
|
* a fresh ALS scope with snapshotted request context.
|
|
434
461
|
*/
|
|
435
|
-
function backgroundRerender(
|
|
436
|
-
id: string,
|
|
437
|
-
component: ServerComponent,
|
|
438
|
-
props: Record<string, unknown>,
|
|
439
|
-
cacheKey: string,
|
|
440
|
-
options: PrebuiltComponentOptions,
|
|
441
|
-
hasSeed: boolean,
|
|
442
|
-
snapshot: RequestContextStore
|
|
443
|
-
): void {
|
|
462
|
+
function backgroundRerender(r: Rerender, snapshot: RequestContextStore): void {
|
|
444
463
|
const waitUntil = getWaitUntil();
|
|
445
|
-
const tags = resolveTags(options, props);
|
|
446
|
-
const oKey = overlayKey(id, cacheKey);
|
|
447
|
-
const startEpoch = currentInvalidationEpoch();
|
|
448
|
-
const sfKey = `prebuilt-bg:${oKey}\0${lastInvalidationEpochFor(oKey, tags)}`;
|
|
449
464
|
|
|
450
465
|
const work = requestContextAls.run(snapshot, () =>
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
async () => {
|
|
459
|
-
const captured = await captureComponentPayload(id, component, props, options);
|
|
460
|
-
if (!signal.aborted && mayStoreRerender(startEpoch, id, cacheKey, oKey, tags)) {
|
|
461
|
-
await storeCapturedEntry(id, cacheKey, captured, options, tags, hasSeed);
|
|
462
|
-
}
|
|
463
|
-
},
|
|
464
|
-
(error) =>
|
|
465
|
-
console.error(
|
|
466
|
-
`[timber] cache.component background re-render failed for "${id}":`,
|
|
467
|
-
error
|
|
468
|
-
)
|
|
469
|
-
)
|
|
466
|
+
// The stale payload already answered the request, so nothing else sees
|
|
467
|
+
// a failure here: logged and reported once per flight, inside the body.
|
|
468
|
+
// Inline re-render and revive failures are only logged: a live render
|
|
469
|
+
// answers instead.
|
|
470
|
+
rerenderFlight(r, (signal, capture) =>
|
|
471
|
+
runBackgroundFlight(signal, capture, (error) =>
|
|
472
|
+
console.error(`[timber] cache.component background re-render failed for "${r.id}":`, error)
|
|
470
473
|
)
|
|
471
|
-
|
|
472
|
-
//
|
|
474
|
+
)
|
|
475
|
+
// Reported inside the flight; this handle only keeps it unhandled-free.
|
|
473
476
|
.catch(() => {})
|
|
474
477
|
);
|
|
475
478
|
|
|
@@ -518,38 +521,42 @@ async function resolveDevSeed(
|
|
|
518
521
|
const guardTags = [...tags, devComponentTag(id)];
|
|
519
522
|
const sfKey = `prebuilt-dev:${guardKey}\0${lastInvalidationEpochFor(guardKey, guardTags)}`;
|
|
520
523
|
try {
|
|
521
|
-
return await componentSingleflight.do(
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
if (outcome.bytes === null) return null;
|
|
532
|
-
// Smoke-revive before storing, as the build does: bytes that cannot
|
|
533
|
-
// be decoded would fail every later request until the next save.
|
|
534
|
-
if ((await revivePayload(id, outcome.bytes, 'not storing the dev capture')) === null) {
|
|
535
|
-
return null;
|
|
536
|
-
}
|
|
537
|
-
// A save (or revalidateTag) while this rendered: the bytes may come
|
|
538
|
-
// from the old code, so this request gets them but the store does not.
|
|
539
|
-
const moved = isr && overlayKey(id, cacheKey) !== guardKey;
|
|
540
|
-
if (!signal.aborted && !moved && !wasInvalidatedSince(startEpoch, guardKey, guardTags)) {
|
|
541
|
-
setDevSeed(id, cacheKey, {
|
|
542
|
-
bytes: outcome.bytes,
|
|
543
|
-
tags,
|
|
544
|
-
capturedAt,
|
|
545
|
-
isr,
|
|
546
|
-
});
|
|
547
|
-
warnNestingClamps({ id, options, tags }, outcome.nested, warnedNestingPairs, (m) =>
|
|
548
|
-
console.warn(m)
|
|
524
|
+
return await componentSingleflight.do(
|
|
525
|
+
sfKey,
|
|
526
|
+
async (signal) => {
|
|
527
|
+
const startEpoch = currentInvalidationEpoch();
|
|
528
|
+
const capturedAt = Date.now();
|
|
529
|
+
const { renderFlight } = requireRscFunctions();
|
|
530
|
+
// Outside the request's React.cache scope, like every stored capture
|
|
531
|
+
// (TIM-1529).
|
|
532
|
+
const outcome = await runOutsideReactCacheScope(() =>
|
|
533
|
+
renderEntry(registration, props, keyParams, getCaptureTimeoutMs(), false, renderFlight)
|
|
549
534
|
);
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
535
|
+
if (outcome.failure instanceof BuildTimeContextError) throw outcome.failure;
|
|
536
|
+
if (outcome.bytes === null) return null;
|
|
537
|
+
// Smoke-revive before storing, as the build does: bytes that cannot
|
|
538
|
+
// be decoded would fail every later request until the next save.
|
|
539
|
+
if ((await revivePayload(id, outcome.bytes, 'not storing the dev capture')) === null) {
|
|
540
|
+
return null;
|
|
541
|
+
}
|
|
542
|
+
// A save (or revalidateTag) while this rendered: the bytes may come
|
|
543
|
+
// from the old code, so this request gets them but the store does not.
|
|
544
|
+
const moved = isr && overlayKey(id, cacheKey) !== guardKey;
|
|
545
|
+
if (!signal.aborted && !moved && !wasInvalidatedSince(startEpoch, guardKey, guardTags)) {
|
|
546
|
+
setDevSeed(id, cacheKey, {
|
|
547
|
+
bytes: outcome.bytes,
|
|
548
|
+
tags,
|
|
549
|
+
capturedAt,
|
|
550
|
+
isr,
|
|
551
|
+
});
|
|
552
|
+
warnNestingClamps({ id, options, tags }, outcome.nested, warnedNestingPairs, (m) =>
|
|
553
|
+
console.warn(m)
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
return { payload: outcome.bytes, capturedAt };
|
|
557
|
+
},
|
|
558
|
+
componentTimeoutMs
|
|
559
|
+
);
|
|
553
560
|
} catch (error) {
|
|
554
561
|
if (error instanceof BuildTimeContextError) throw error;
|
|
555
562
|
console.error(`[timber] cache.component dev capture failed for "${id}":`, error);
|
|
@@ -624,7 +631,10 @@ async function tryRevivePayload(
|
|
|
624
631
|
if (overlayResult) {
|
|
625
632
|
const overlayHasSeed = overlayResult.record?.s ?? false;
|
|
626
633
|
if (overlayResult.kind === 'tombstone') {
|
|
627
|
-
return await inlineRerender(
|
|
634
|
+
return await inlineRerender(
|
|
635
|
+
{ id, component, props, cacheKey, options, hasSeed: true },
|
|
636
|
+
outcome
|
|
637
|
+
);
|
|
628
638
|
}
|
|
629
639
|
// Unreadable is decided after the seed lookup: the seed says whether
|
|
630
640
|
// this key was ever enumerated (a strict miss must still throw), but
|
|
@@ -636,23 +646,13 @@ async function tryRevivePayload(
|
|
|
636
646
|
if (overlayResult.kind === 'stale' && overlayResult.payload) {
|
|
637
647
|
if (options.staleWhileRevalidate) {
|
|
638
648
|
backgroundRerender(
|
|
639
|
-
id,
|
|
640
|
-
component,
|
|
641
|
-
props,
|
|
642
|
-
cacheKey,
|
|
643
|
-
options,
|
|
644
|
-
overlayHasSeed,
|
|
649
|
+
{ id, component, props, cacheKey, options, hasSeed: overlayHasSeed },
|
|
645
650
|
snapshotRequestContext(store)
|
|
646
651
|
);
|
|
647
652
|
return await revivePayload(id, overlayResult.payload, outcome);
|
|
648
653
|
}
|
|
649
654
|
return await inlineRerender(
|
|
650
|
-
id,
|
|
651
|
-
component,
|
|
652
|
-
props,
|
|
653
|
-
cacheKey,
|
|
654
|
-
options,
|
|
655
|
-
overlayHasSeed,
|
|
655
|
+
{ id, component, props, cacheKey, options, hasSeed: overlayHasSeed },
|
|
656
656
|
outcome
|
|
657
657
|
);
|
|
658
658
|
}
|
|
@@ -669,7 +669,10 @@ async function tryRevivePayload(
|
|
|
669
669
|
: null;
|
|
670
670
|
if (seed !== null && overlayUnreadable) {
|
|
671
671
|
// Render, as for a tombstone (design/06 §Store failures).
|
|
672
|
-
return await inlineRerender(
|
|
672
|
+
return await inlineRerender(
|
|
673
|
+
{ id, component, props, cacheKey, options, hasSeed: true },
|
|
674
|
+
outcome
|
|
675
|
+
);
|
|
673
676
|
}
|
|
674
677
|
if (seed !== null) {
|
|
675
678
|
// Check seed freshness when the component has a TTL.
|
|
@@ -679,17 +682,15 @@ async function tryRevivePayload(
|
|
|
679
682
|
// Stale seed
|
|
680
683
|
if (options.staleWhileRevalidate) {
|
|
681
684
|
backgroundRerender(
|
|
682
|
-
id,
|
|
683
|
-
component,
|
|
684
|
-
props,
|
|
685
|
-
cacheKey,
|
|
686
|
-
options,
|
|
687
|
-
true,
|
|
685
|
+
{ id, component, props, cacheKey, options, hasSeed: true },
|
|
688
686
|
snapshotRequestContext(store)
|
|
689
687
|
);
|
|
690
688
|
return await revivePayload(id, seed.payload, outcome);
|
|
691
689
|
}
|
|
692
|
-
return await inlineRerender(
|
|
690
|
+
return await inlineRerender(
|
|
691
|
+
{ id, component, props, cacheKey, options, hasSeed: true },
|
|
692
|
+
outcome
|
|
693
|
+
);
|
|
693
694
|
}
|
|
694
695
|
}
|
|
695
696
|
// Fresh seed (or no TTL — immutable until redeploy)
|
|
@@ -710,7 +711,10 @@ async function tryRevivePayload(
|
|
|
710
711
|
// "only prebuilt content", so an unenumerated miss must throw (the
|
|
711
712
|
// wrapper handles the throw after we return null).
|
|
712
713
|
if (hasRuntime && options.fallbackOnMissingSegmentParams !== false) {
|
|
713
|
-
return await inlineRerender(
|
|
714
|
+
return await inlineRerender(
|
|
715
|
+
{ id, component, props, cacheKey, options, hasSeed: false },
|
|
716
|
+
outcome
|
|
717
|
+
);
|
|
714
718
|
}
|
|
715
719
|
|
|
716
720
|
return null;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Upstream framing on forwarded responses (GHSA-pc58-hwjq-p385 class,
|
|
3
|
+
* design/13-security.md #42).
|
|
4
|
+
*
|
|
5
|
+
* `fetch()` transparently decodes a gzip/br/deflate/zstd body but keeps the
|
|
6
|
+
* upstream's `Content-Encoding` and `Content-Length`, along with its
|
|
7
|
+
* connection-level headers. A route handler, proxy.ts or middleware that
|
|
8
|
+
* returns that `Response` — directly or through
|
|
9
|
+
* `new Response(res.body, { headers: res.headers })` — would otherwise send
|
|
10
|
+
* the decoded bytes framed by the encoded length. On a keep-alive HTTP/1.1
|
|
11
|
+
* connection the surplus bytes are read as the *next* response: a reverse
|
|
12
|
+
* proxy that queues several clients on one backend connection hands an
|
|
13
|
+
* upstream-controlled response to another client, or one client's response
|
|
14
|
+
* to the wrong one.
|
|
15
|
+
*
|
|
16
|
+
* Whether a body was decoded is not observable from the `Response` (the
|
|
17
|
+
* header-copying proxy pattern produces an ordinary constructed `Response`),
|
|
18
|
+
* so the rule is stated on the headers alone:
|
|
19
|
+
*
|
|
20
|
+
* - A response that declares `Content-Encoding` loses it and its
|
|
21
|
+
* `Content-Length`. The length described the encoded bytes; the server
|
|
22
|
+
* frames what it actually writes, and the adapter's compression step may
|
|
23
|
+
* re-encode it. Matches Next.js (vercel/next.js#101108). The cost: a
|
|
24
|
+
* handler cannot hand back a pre-compressed body under its own
|
|
25
|
+
* `Content-Encoding`.
|
|
26
|
+
* - Hop-by-hop headers (RFC 9110 §7.6.1) describe the upstream's
|
|
27
|
+
* connection, not this one, and are owned by the server writing the
|
|
28
|
+
* socket. They are always dropped. A forwarded `Connection: keep-alive` otherwise
|
|
29
|
+
* overrides the client's `Connection: close`, and a forwarded
|
|
30
|
+
* `Transfer-Encoding` claims a framing the server did not choose.
|
|
31
|
+
* The extra headers a `Connection` value names are deliberately not
|
|
32
|
+
* honored: the response's headers here are the upstream's merged with
|
|
33
|
+
* the app's own (cookies, middleware headers), and an upstream sending
|
|
34
|
+
* `Connection: set-cookie` must not be able to delete the app's session.
|
|
35
|
+
* - A `Content-Length` on an unencoded response is the route's own claim
|
|
36
|
+
* and is kept — a HEAD response relies on it (head-requests.test.ts).
|
|
37
|
+
*
|
|
38
|
+
* Applied at the pipeline's single exit, before compression, so every
|
|
39
|
+
* producer is covered — including ones added later. Timber's own renderers
|
|
40
|
+
* set none of these headers inside the pipeline.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
const HOP_BY_HOP = [
|
|
44
|
+
'connection',
|
|
45
|
+
'keep-alive',
|
|
46
|
+
'proxy-connection',
|
|
47
|
+
'te',
|
|
48
|
+
'trailer',
|
|
49
|
+
'transfer-encoding',
|
|
50
|
+
'upgrade',
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
export function dropUpstreamFraming(response: Response): void {
|
|
54
|
+
const { headers } = response;
|
|
55
|
+
for (const name of HOP_BY_HOP) headers.delete(name);
|
|
56
|
+
if (!headers.has('content-encoding')) return;
|
|
57
|
+
headers.delete('content-encoding');
|
|
58
|
+
headers.delete('content-length');
|
|
59
|
+
}
|
package/src/server/ssr-entry.ts
CHANGED
|
@@ -175,12 +175,10 @@ export async function handleSsr(
|
|
|
175
175
|
// real NavigationProvider and PayloadRoot, not no-op mirrors —
|
|
176
176
|
// so hooks resolve identically and useId() matches on both sides.
|
|
177
177
|
// See TIM-532, TIM-1424.
|
|
178
|
-
const hasTopLoader = _runtimeConfig.topLoader?.enabled !== false;
|
|
179
178
|
const wrappedElement = wrapSsrElement(element, {
|
|
180
179
|
pathname: navContext.pathname,
|
|
181
180
|
search: navContext.search,
|
|
182
181
|
paramsSource: params,
|
|
183
|
-
hasTopLoader,
|
|
184
182
|
});
|
|
185
183
|
|
|
186
184
|
// Render to HTML stream (waits for onShellReady).
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*
|
|
10
10
|
* The client tree (`renderTree` in browser-entry/router-init.ts, used by
|
|
11
11
|
* hydrate.ts too) wraps the RSC element with:
|
|
12
|
-
* NavigationRoot → Fragment(TopLoader, ...) →
|
|
12
|
+
* NavigationRoot → Fragment(TopLoader | null, ...) →
|
|
13
13
|
* TimberNuqsAdapter → NuqsAdapterProvider →
|
|
14
14
|
* SlotContentCacheContext.Provider → SegmentUpdateContext.Provider →
|
|
15
15
|
* NavigationProvider → PayloadRoot → ParamsProvider → element
|
|
@@ -21,8 +21,9 @@
|
|
|
21
21
|
* during SSR — the same code path as the browser — with the request's
|
|
22
22
|
* pathname and raw search string. The raw string preserves repeated
|
|
23
23
|
* query keys (`?tag=a&tag=b`) and, since TIM-1428, is the only
|
|
24
|
-
* representation of search state here —
|
|
25
|
-
*
|
|
24
|
+
* representation of search state here — nuqs reads it too, through the
|
|
25
|
+
* same TimberNuqsAdapter the browser renders, so no collapsed record
|
|
26
|
+
* exists to drift from it.
|
|
26
27
|
* 2. `useSegmentParams()` resolves from ParamsContext, published by
|
|
27
28
|
* PayloadRoot from the same payload root row the browser will read —
|
|
28
29
|
* one derivation on both sides instead of two that must agree.
|
|
@@ -46,53 +47,44 @@
|
|
|
46
47
|
*/
|
|
47
48
|
|
|
48
49
|
import { createElement, Fragment, type ReactNode } from 'react';
|
|
49
|
-
import {
|
|
50
|
+
import { TimberNuqsAdapter } from '../client/nuqs-adapter.tsx';
|
|
50
51
|
import { NavigationProvider } from '../client/navigation-context.ts';
|
|
51
52
|
import { PayloadRoot } from '../client/params-context.ts';
|
|
52
53
|
|
|
53
54
|
/**
|
|
54
55
|
* SSR equivalent of NavigationRoot.
|
|
55
56
|
*
|
|
56
|
-
* On the client, the router renders NavigationRoot via `root.render` in a
|
|
57
|
-
* rendering `Fragment(TopLoader, element)
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* context. Both sides must agree on whether it is there, which is why
|
|
62
|
-
* `hasTopLoader` is threaded in from the same config the client reads.
|
|
63
|
-
*/
|
|
64
|
-
function SsrNavigationRoot({
|
|
65
|
-
children,
|
|
66
|
-
hasTopLoader,
|
|
67
|
-
}: {
|
|
68
|
-
children?: ReactNode;
|
|
69
|
-
hasTopLoader: boolean;
|
|
70
|
-
}): ReactNode {
|
|
71
|
-
if (hasTopLoader) {
|
|
72
|
-
return createElement(Fragment, null, createElement(SsrTopLoader, null), children);
|
|
73
|
-
}
|
|
74
|
-
return children as ReactNode;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* SSR equivalent of TopLoader.
|
|
79
|
-
* Exists as a fiber node to match the client tree depth; renders nothing.
|
|
57
|
+
* On the client, the router renders NavigationRoot via `root.render` in a
|
|
58
|
+
* transition, rendering `Fragment(TopLoader | null, element)`. The Fragment is
|
|
59
|
+
* the part that matters: it forks, so it advances React's tree context, and it
|
|
60
|
+
* is unconditional on both sides — the loader's config only decides what fills
|
|
61
|
+
* the first slot.
|
|
80
62
|
*/
|
|
81
|
-
function
|
|
82
|
-
return null;
|
|
63
|
+
function SsrNavigationRoot({ children }: { children?: ReactNode }): ReactNode {
|
|
64
|
+
return createElement(Fragment, null, createElement(DocumentDefaults, null), children);
|
|
83
65
|
}
|
|
84
66
|
|
|
85
67
|
/**
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
68
|
+
* The document's framework defaults (design/16-metadata.md §"No Separate
|
|
69
|
+
* Viewport Type"), in the TopLoader's slot.
|
|
70
|
+
*
|
|
71
|
+
* Server-only: the browser renders TopLoader (or nothing) here, and React
|
|
72
|
+
* leaves head nodes it did not render alone during hydration, so the tags
|
|
73
|
+
* stay for the life of the document — no RSC payload, partial or full,
|
|
74
|
+
* carries them.
|
|
75
|
+
*
|
|
76
|
+
* Rendered before the payload so these are the FIRST charset/viewport tags
|
|
77
|
+
* Fizz collects. React writes every viewport <meta> into the head in render
|
|
78
|
+
* order, and browsers apply the last one, so a viewport <meta> in the app's
|
|
79
|
+
* root layout overrides this default rather than being overridden by it.
|
|
90
80
|
*/
|
|
91
|
-
function
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
81
|
+
function DocumentDefaults(): ReactNode {
|
|
82
|
+
return createElement(
|
|
83
|
+
Fragment,
|
|
84
|
+
null,
|
|
85
|
+
createElement('meta', { charSet: 'utf-8' }),
|
|
86
|
+
createElement('meta', { name: 'viewport', content: 'width=device-width, initial-scale=1' })
|
|
87
|
+
);
|
|
96
88
|
}
|
|
97
89
|
|
|
98
90
|
/** Per-request inputs for the SSR wrapper chain. */
|
|
@@ -101,7 +93,7 @@ export interface SsrWrapOptions {
|
|
|
101
93
|
pathname: string;
|
|
102
94
|
/**
|
|
103
95
|
* Raw app-visible query string (`''` or `?…`) for NavigationProvider AND
|
|
104
|
-
*
|
|
96
|
+
* nuqs (through NavigationContext) — one representation, preserving repeated keys
|
|
105
97
|
* (`?tag=a&tag=b`) on both consumers (TIM-1428).
|
|
106
98
|
*/
|
|
107
99
|
search: string;
|
|
@@ -111,8 +103,6 @@ export interface SsrWrapOptions {
|
|
|
111
103
|
* hands to the same component during hydration.
|
|
112
104
|
*/
|
|
113
105
|
paramsSource: unknown;
|
|
114
|
-
/** Whether the top loader is enabled (must match client config). */
|
|
115
|
-
hasTopLoader: boolean;
|
|
116
106
|
}
|
|
117
107
|
|
|
118
108
|
/**
|
|
@@ -122,7 +112,7 @@ export interface SsrWrapOptions {
|
|
|
122
112
|
*
|
|
123
113
|
* Client tree (browser-entry/hydrate.ts):
|
|
124
114
|
* NavigationRoot
|
|
125
|
-
* → Fragment(TopLoader, element)
|
|
115
|
+
* → Fragment(TopLoader | null, element)
|
|
126
116
|
* → TimberNuqsAdapter
|
|
127
117
|
* → NuqsAdapterProvider (from createAdapterProvider)
|
|
128
118
|
* → SlotContentCacheContext.Provider (client-only, single child)
|
|
@@ -133,9 +123,9 @@ export interface SsrWrapOptions {
|
|
|
133
123
|
*
|
|
134
124
|
* SSR tree (this function):
|
|
135
125
|
* SsrNavigationRoot
|
|
136
|
-
* → Fragment(
|
|
137
|
-
* →
|
|
138
|
-
* → NuqsAdapterProvider
|
|
126
|
+
* → Fragment(DocumentDefaults, element)
|
|
127
|
+
* → TimberNuqsAdapter (real — same component)
|
|
128
|
+
* → NuqsAdapterProvider
|
|
139
129
|
* → NavigationProvider (real — same component)
|
|
140
130
|
* → PayloadRoot → ParamsProvider (real — same components)
|
|
141
131
|
* → [RSC element]
|
|
@@ -154,9 +144,9 @@ export function wrapSsrElement(element: ReactNode, opts: SsrWrapOptions): ReactN
|
|
|
154
144
|
withParams
|
|
155
145
|
);
|
|
156
146
|
|
|
157
|
-
// 3.
|
|
158
|
-
const withNuqs = createElement(
|
|
147
|
+
// 3. The nuqs adapter — the browser's own; it reads search from NavigationContext.
|
|
148
|
+
const withNuqs = createElement(TimberNuqsAdapter, null, withNav);
|
|
159
149
|
|
|
160
150
|
// 4. Outermost: NavigationRoot equivalent (the TopLoader fork)
|
|
161
|
-
return createElement(SsrNavigationRoot,
|
|
151
|
+
return createElement(SsrNavigationRoot, null, withNuqs);
|
|
162
152
|
}
|