diff --git a/.changeset/hungry-experts-suffer.md b/.changeset/hungry-experts-suffer.md new file mode 100644 index 00000000000..2ae133b745c --- /dev/null +++ b/.changeset/hungry-experts-suffer.md @@ -0,0 +1,5 @@ +--- +"@remix-run/dev": patch +--- + +Properly abort `request.signal` during `vite dev` when the node response is closed diff --git a/packages/remix-dev/vite/cloudflare-proxy-plugin.ts b/packages/remix-dev/vite/cloudflare-proxy-plugin.ts index 314c94cf33e..cdad892b21b 100644 --- a/packages/remix-dev/vite/cloudflare-proxy-plugin.ts +++ b/packages/remix-dev/vite/cloudflare-proxy-plugin.ts @@ -70,7 +70,7 @@ export const cloudflareDevProxyVitePlugin = ({ )) as ServerBuild; let handler = createRequestHandler(build, "development"); - let req = fromNodeRequest(nodeReq); + let req = fromNodeRequest(nodeReq, nodeRes); let loadContext = getLoadContext ? await getLoadContext({ request: req, context }) : context; diff --git a/packages/remix-dev/vite/node-adapter.ts b/packages/remix-dev/vite/node-adapter.ts index ad270a85525..2783da81778 100644 --- a/packages/remix-dev/vite/node-adapter.ts +++ b/packages/remix-dev/vite/node-adapter.ts @@ -32,7 +32,8 @@ function fromNodeHeaders(nodeHeaders: IncomingHttpHeaders): Headers { // Based on `createRemixRequest` in packages/remix-express/server.ts export function fromNodeRequest( - nodeReq: Vite.Connect.IncomingMessage + nodeReq: Vite.Connect.IncomingMessage, + nodeRes: ServerResponse ): Request { let origin = nodeReq.headers.origin && "null" !== nodeReq.headers.origin @@ -44,9 +45,15 @@ export function fromNodeRequest( "Expected `nodeReq.originalUrl` to be defined" ); let url = new URL(nodeReq.originalUrl, origin); + + // Abort action/loaders once we can no longer write a response + let controller = new AbortController(); + nodeRes.on("close", () => controller.abort()); + let init: RequestInit = { method: nodeReq.method, headers: fromNodeHeaders(nodeReq.headers), + signal: controller.signal, }; if (nodeReq.method !== "GET" && nodeReq.method !== "HEAD") { diff --git a/packages/remix-dev/vite/plugin.ts b/packages/remix-dev/vite/plugin.ts index d96d6b547cd..53ce6400b57 100644 --- a/packages/remix-dev/vite/plugin.ts +++ b/packages/remix-dev/vite/plugin.ts @@ -1382,7 +1382,7 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => { nodeReq, nodeRes ) => { - let req = fromNodeRequest(nodeReq); + let req = fromNodeRequest(nodeReq, nodeRes); let res = await handler(req, await remixDevLoadContext(req)); await toNodeRequest(res, nodeRes); };