Skip to content

Commit 53ef0e6

Browse files
brenelzryansolidClaude via Cursorcursoragent
authored
fix(web): settle streamed single-flight slices before delivering them (#3865)
* fix(web): settle streamed single-flight slices before delivering them A collector may fold values that are still pending (the router's collector returns its preloads' query promises as they stand); the codec streams them after the response head. The body decode resolves on the first chunk, so the mutation call resolved and a router action settled (busy cleared, onSettled ran) before the refreshed route data existed. deliverFlightData, the delivery path shared by the plain and frames clients, now waits for every pending slice entry (allSettled) before running the consumers. * refactor(web): settle single-flight slices in one Promise.allSettled Folds settleFlightSlices into one expression in deliverFlightData (no behavior change) and re-wraps its JSDoc. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]> * chore(size): size exception for settling single-flight slices (#3865) frames: eager client consumer 13.98 -> 14.02 KB, recorded minified 44,032 B, from Size run 37605412983. Accepted by the maintainer 2026-10-07. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Ryan Carniato <[email protected]> Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent fa371c3 commit 53ef0e6

4 files changed

Lines changed: 66 additions & 7 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/web": patch
3+
---
4+
5+
Single-flight delivery now settles slice entries the server folded while still pending before handing the envelope to the flight-data consumers. A collector may fold values that are still in flight (the router's collector returns its preloads' `query` promises as they stand), and the codec streams those after the response head; since the body decode resolves on the first chunk, the mutation call resolved, and a router action settled (busy state cleared, `onSettled` ran) before the refreshed route data existed. `deliverFlightData`, shared by the plain client and the frames client, now waits for every pending entry (`allSettled`, so a failed read stays with the cache entry it seeds) before running consumers.

‎packages/web/server-functions/src/shared.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,13 @@ export function hasFlightMetadata(response) {
393393
* envelope context, whose `SINGLE_FLIGHT_HEADER` names the folded sources.
394394
* Each registered consumer receives its own slice, in registration order,
395395
* awaited sequentially so caches are seeded before the caller sees the
396-
* value. A consumer whose source was not folded is skipped — unless the
397-
* response carries integration metadata, in which case it runs with `data`
398-
* `undefined` to apply the metadata. Consumers are looked up per delivery:
399-
* an awaited consumer may unsubscribe another (a provider tearing down
400-
* under a navigation).
396+
* value. Pending entries in the slices (values the collector folded while
397+
* still in flight, streamed after the response head) are settled before
398+
* any consumer runs. A consumer whose source was not folded is skipped —
399+
* unless the response carries integration metadata, in which case it runs
400+
* with `data` `undefined` to apply the metadata. Consumers are looked up
401+
* per delivery: an awaited consumer may unsubscribe another (a provider
402+
* tearing down under a navigation).
401403
*
402404
* Transport building block; not meant for hand-written code.
403405
* @internal
@@ -406,6 +408,14 @@ export function deliverFlightData(response: Response, data: unknown): Promise<vo
406408

407409
/** Delivers each consumer its slice of a single-flight envelope's `data`. */
408410
export async function deliverFlightData(response, data) {
411+
// The body decode resolves on the first chunk, while values a collector
412+
// folded still pending (the router's preload `query` promises) stream
413+
// after it. `allSettled`: a failed read belongs to the cache entry it
414+
// seeds, not to the mutation that carried it.
415+
if (data)
416+
await Promise.allSettled(
417+
Object.values(data).flatMap(s => (s && typeof s == "object" ? Object.values(s) : []))
418+
);
409419
// An absent header splits to [""], which names no source (ids are never
410420
// empty — see assertFlightSource), so no consumer matches it.
411421
const folded = (response.headers.get(SINGLE_FLIGHT_HEADER) || "").split(",");

‎packages/web/test/server/server-functions-single-flight.spec.tsx‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,41 @@ describe("single-flight client bridge (built client bundle)", () => {
299299
}
300300
});
301301

302+
it("settles slice entries the collector folded while still pending before resolving", async () => {
303+
// The router's collector folds its preloads' query promises as they
304+
// stand; the codec streams them after the response head. The call must
305+
// not resolve (and a router action must not settle) on the first chunk.
306+
registerServerFunction("sf-bridge-client-pending", async () => "mutated");
307+
let release!: (value: string[]) => void;
308+
const pendingSlice = new Promise<string[]>(resolve => (release = resolve));
309+
const restore = connectTransport({
310+
collectFlightData: () => ({ "/notes": pendingSlice })
311+
});
312+
const order: string[] = [];
313+
let delivered: any;
314+
const unsubscribe = subscribeFlightDataClient(async data => {
315+
order.push("consumer");
316+
delivered = data;
317+
});
318+
try {
319+
const call = createServerReference("sf-bridge-client-pending")().then(value => {
320+
order.push("resolved");
321+
return value;
322+
});
323+
// the head and the envelope's first chunk are out; the slice is not
324+
await new Promise(resolve => setTimeout(resolve, 20));
325+
expect(order).toEqual([]);
326+
order.push("slice");
327+
release(["fresh"]);
328+
expect(await call).toBe("mutated");
329+
expect(order).toEqual(["slice", "consumer", "resolved"]);
330+
expect(await delivered["/notes"]).toEqual(["fresh"]);
331+
} finally {
332+
unsubscribe();
333+
restore();
334+
}
335+
});
336+
302337
it("routes keyed slices to their subscribed consumers", async () => {
303338
registerServerFunction("sf-bridge-multi-client-0", async () => "mutated");
304339
let requestLeg: string | null = null;

‎scripts/size/scenarios.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3429,8 +3429,17 @@ module.exports = [
34293429
// recorded minified 43,932 B. Accepted by the maintainer (2026-10-07:
34303430
// server-function/frames reader correctness). The cap is frozen again at
34313431
// 13.98 KB.
3432-
limit: "13.98 KB",
3433-
capMinified: 43932,
3432+
// Size-Exception (single-flight slices settle before delivery, #3865,
3433+
// 2026-10-07): 13.98 -> 14.02 KB, measured at 14,003 B by CI (Size run
3434+
// 37605412983) against `next` @ fa371c3e7's 13,969 (+34 B; 23 B over the
3435+
// cap; +100 B minified, 43,932 -> 44,032) — `deliverFlightData` awaits
3436+
// `Promise.allSettled` over every slice entry before running consumers,
3437+
// so a mutation call resolves only after the values its collector folded
3438+
// still pending have streamed in. Folded into one expression (from a
3439+
// helper) before measuring. Cap set at measured + 10 B rounded up to
3440+
// 0.01 KB. Accepted by the maintainer 2026-10-07.
3441+
limit: "14.02 KB",
3442+
capMinified: 44032,
34343443
alias: framesAlias,
34353444
external: framesExternal
34363445
},

0 commit comments

Comments
 (0)