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

Fix loader request on document POST requests #9721

Merged
merged 6 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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/kind-dodos-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Persist method/headers on loader requests after SSR document action request
16 changes: 13 additions & 3 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10935,7 +10935,13 @@ describe("a router", () => {
],
},
]);
await query(createSubmitRequest("/child"));
await query(
createSubmitRequest("/child", {
headers: {
test: "value",
},
})
);

// @ts-expect-error
let actionRequest = actionStub.mock.calls[0][0]?.request;
Expand All @@ -10950,10 +10956,14 @@ describe("a router", () => {
let rootLoaderRequest = rootLoaderStub.mock.calls[0][0]?.request;
// @ts-expect-error
let childLoaderRequest = childLoaderStub.mock.calls[0][0]?.request;
expect(rootLoaderRequest.method).toBe("GET");
expect(rootLoaderRequest.method).toBe("POST");
expect(rootLoaderRequest.url).toBe("http://localhost/child");
expect(childLoaderRequest.method).toBe("GET");
expect(rootLoaderRequest.headers.get("test")).toBe("value");
expect(await rootLoaderRequest.text()).toBe("");
expect(childLoaderRequest.method).toBe("POST");
expect(childLoaderRequest.url).toBe("http://localhost/child");
expect(childLoaderRequest.headers.get("test")).toBe("value");
// Can't re-read body here since it's the same request as the root
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because query is a single document-level POST request the loaders reflect POST as well, but we don't proxy along the body

});

it("should support a requestContext passed to loaders and actions", async () => {
Expand Down
10 changes: 8 additions & 2 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2256,8 +2256,14 @@ export function unstable_createStaticHandler(
};
}

// Create a GET request for the loaders
let loaderRequest = new Request(request.url, { signal: request.signal });
// Create a request for the loaders
let loaderRequest = new Request(request.url, {
body: null,
headers: request.headers,
method: request.method,
redirect: request.redirect,
signal: request.signal,
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the moment we are not persisting method:'POST' as Remix did since we think that should be changed in Remix

let context = await loadRouteData(loaderRequest, matches, requestContext);

return {
Expand Down