Skip to content

Commit

Permalink
Proxy request.signal through in vite dev (#9976)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 authored Sep 13, 2024
1 parent d9a6076 commit a49b4c6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-experts-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Properly abort `request.signal` during `vite dev` when the node response is closed
2 changes: 1 addition & 1 deletion packages/remix-dev/vite/cloudflare-proxy-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const cloudflareDevProxyVitePlugin = <Env, Cf extends CfProperties>({
)) 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;
Expand Down
9 changes: 8 additions & 1 deletion packages/remix-dev/vite/node-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vite.Connect.IncomingMessage>
): Request {
let origin =
nodeReq.headers.origin && "null" !== nodeReq.headers.origin
Expand All @@ -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") {
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down

0 comments on commit a49b4c6

Please sign in to comment.