Skip to content

Commit 9699109

Browse files
committed
fix(browser): skip inflight prefetch for navigation, use fresh fetch
Inflight prefetches buffer the entire response body (arrayBuffer) before resolving. For routes with slow streaming loaders (e.g., parallel sidebar with 5.5s loader), this blocks SPA navigation for the full loader duration even though createFromFetch can consume streams incrementally. When an inflight prefetch exists, skip it and do a fresh fetch instead. The fresh fetch gives createFromFetch a streaming Response for incremental consumption. The inflight prefetch completes in the background and populates the cache for future navigations.
1 parent 6e127d4 commit 9699109

2 files changed

Lines changed: 20 additions & 37 deletions

File tree

‎packages/rangojs-router/src/__tests__/navigation-client.test.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,20 @@ describe("navigation-client", () => {
197197
expect(consumePrefetchMock).toHaveBeenCalledTimes(1);
198198
});
199199

200-
it("uses in-flight prefetch promise when cache misses", async () => {
201-
const inflightBody = "inflight-rsc-payload";
200+
it("does a fresh fetch when prefetch is still inflight (for streaming)", async () => {
202201
consumeInflightPrefetchMock.mockReturnValue(
203-
Promise.resolve(new Response(inflightBody)),
202+
Promise.resolve(new Response("inflight-rsc-payload")),
204203
);
205204

206-
const fetchMock = vi.fn();
205+
const fetchMock = vi.fn(
206+
async () => new Response("fresh-payload", { status: 200 }),
207+
);
207208
vi.stubGlobal("fetch", fetchMock as unknown as typeof fetch);
208209

209210
const client = createNavigationClient({
210211
createFromFetch: async (responsePromise: Promise<Response>) => {
211-
const response = await responsePromise;
212-
const text = await response.clone().text();
213-
return { metadata: { matched: [], diff: [], body: text } };
212+
await responsePromise;
213+
return { metadata: { matched: [], diff: [] } };
214214
},
215215
} as any);
216216

@@ -220,7 +220,8 @@ describe("navigation-client", () => {
220220
segmentIds: ["root"],
221221
});
222222

223-
expect(fetchMock).not.toHaveBeenCalled();
223+
// Inflight prefetch is skipped — fresh fetch is used for streaming
224+
expect(fetchMock).toHaveBeenCalledTimes(1);
224225
expect(consumePrefetchMock).toHaveBeenCalledTimes(1);
225226
expect(consumeInflightPrefetchMock).toHaveBeenCalledTimes(1);
226227
expect(result.payload.metadata).toMatchObject({ matched: [], diff: [] });

‎packages/rangojs-router/src/browser/navigation-client.ts‎

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -196,37 +196,19 @@ export function createNavigationClient(
196196
);
197197
});
198198
} else if (inflightPrefetch) {
199+
// Inflight prefetch is still buffering (arrayBuffer). Don't await it —
200+
// the full buffer may take seconds for streaming responses (e.g., slow
201+
// parallel loaders). Instead, do a fresh fetch so createFromFetch can
202+
// consume the response stream incrementally. The inflight prefetch
203+
// completes in the background and populates the cache for future use.
199204
if (tx) {
200-
browserDebugLog(tx, "reusing inflight prefetch", { key: cacheKey });
201-
}
202-
// Await the in-flight prefetch. If it resolves with a Response,
203-
// use it like a cache hit. If it fails (null), fall back to
204-
// a fresh navigation fetch.
205-
responsePromise = inflightPrefetch.then((prefetchResponse) => {
206-
if (!prefetchResponse) {
207-
if (tx) {
208-
browserDebugLog(
209-
tx,
210-
"inflight prefetch failed, falling back to fetch",
211-
);
212-
}
213-
return doFreshFetch();
214-
}
215-
if (tx) {
216-
browserDebugLog(tx, "inflight prefetch resolved", {
217-
key: cacheKey,
218-
});
219-
}
220-
return teeWithCompletion(
221-
prefetchResponse,
222-
() => {
223-
if (tx)
224-
browserDebugLog(tx, "stream complete (from inflight prefetch)");
225-
resolveStreamComplete();
226-
},
227-
signal,
205+
browserDebugLog(
206+
tx,
207+
"inflight prefetch pending, using fresh fetch for streaming",
208+
{ key: cacheKey },
228209
);
229-
});
210+
}
211+
responsePromise = doFreshFetch();
230212
} else {
231213
responsePromise = doFreshFetch();
232214
}

0 commit comments

Comments
 (0)