Skip to content

Fix setTimeout leaks in default entry.server.tsx #13416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/funny-suns-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-router/dev": patch
---

Cancel aborts timeouts created in the default entry.server.tsx file
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,4 @@
- yuleicul
- zeromask1337
- zheng-chuang
- remorses
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default function handleRequest(
return new Promise((resolve, reject) => {
let shellRendered = false;
let userAgent = request.headers.get("user-agent");
let timeoutId: NodeJS.Timeout;

// Ensure requests from bots and SPA Mode renders wait for all content to load before responding
// https://react.dev/reference/react-dom/server/renderToPipeableStream#waiting-for-all-content-to-load-for-crawlers-and-static-generation
Expand All @@ -45,8 +46,10 @@ export default function handleRequest(
);

pipe(body);
clearTimeout(timeoutId);
},
onShellError(error: unknown) {
clearTimeout(timeoutId);
reject(error);
},
onError(error: unknown) {
Expand All @@ -56,13 +59,14 @@ export default function handleRequest(
// reject and get logged in handleDocumentRequest.
if (shellRendered) {
console.error(error);
clearTimeout(timeoutId);
}
},
}
);

// Abort the rendering stream after the `streamTimeout` so it has time to
// flush down the rejected boundaries
setTimeout(abort, streamTimeout + 1000);
timeoutId = setTimeout(abort, streamTimeout + 1000);
});
}