Skip to content

Commit 44991a9

Browse files
authored
fix: clear timeout when turbo-stream encoding completes (#14735)
1 parent 18ae5d1 commit 44991a9

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

.changeset/clear-stream-timeout.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
fix: clear timeout when turbo-stream encoding completes

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
- maximevtush
278278
- maxpou
279279
- mcansh
280+
- mcollina
280281
- MeatSim
281282
- MenouerBetty
282283
- Methuselah96

packages/react-router/lib/server-runtime/single-fetch.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,14 @@ export function encodeViaTurboStream(
385385
() => controller.abort(new Error("Server Timeout")),
386386
typeof streamTimeout === "number" ? streamTimeout : 4950,
387387
);
388-
requestSignal.addEventListener("abort", () => clearTimeout(timeoutId));
388+
389+
let clearStreamTimeout = () => clearTimeout(timeoutId);
390+
391+
requestSignal.addEventListener("abort", clearStreamTimeout);
389392

390393
return encode(data, {
391394
signal: controller.signal,
395+
onComplete: clearStreamTimeout,
392396
plugins: [
393397
(value) => {
394398
// Even though we sanitized errors on context.errors prior to responding,

packages/react-router/vendor/turbo-stream-v2/turbo-stream.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ export function encode(
138138
plugins?: EncodePlugin[];
139139
postPlugins?: EncodePlugin[];
140140
signal?: AbortSignal;
141+
onComplete?: () => void;
141142
},
142143
) {
143-
const { plugins, postPlugins, signal } = options ?? {};
144+
const { plugins, postPlugins, signal, onComplete } = options ?? {};
144145

145146
const encoder: ThisEncode = {
146147
deferred: {},
@@ -274,6 +275,7 @@ export function encode(
274275
}
275276
await Promise.all(Object.values(encoder.deferred));
276277

278+
onComplete?.();
277279
controller.close();
278280
},
279281
});

0 commit comments

Comments
 (0)