Skip to content
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

Proxy request.signal through in vite dev #9976

Merged
merged 4 commits into from
Sep 13, 2024
Merged
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/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
Loading