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(router): fix loader request method #9660

Merged
merged 2 commits into from
Nov 30, 2022
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/pretty-kiwis-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Fix requests sent to revalidating loaders so they reflect a GET request
85 changes: 83 additions & 2 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5052,12 +5052,21 @@ describe("a router", () => {
// Assert request internals, cannot do a deep comparison above since some
// internals aren't the same on separate creations
let request = nav.actions.tasks.stub.mock.calls[0][0].request;
expect(request.url).toBe("http://localhost/tasks");
expect(request.method).toBe("POST");
expect(request.url).toBe("http://localhost/tasks");
expect(request.headers.get("Content-Type")).toBe(
"application/x-www-form-urlencoded;charset=UTF-8"
);
expect((await request.formData()).get("query")).toBe("params");

await nav.actions.tasks.resolve("TASKS ACTION");
let rootLoaderRequest = nav.loaders.root.stub.mock.calls[0][0].request;
expect(rootLoaderRequest.method).toBe("GET");
expect(rootLoaderRequest.url).toBe("http://localhost/tasks");

let tasksLoaderRequest = nav.loaders.tasks.stub.mock.calls[0][0].request;
expect(tasksLoaderRequest.method).toBe("GET");
expect(tasksLoaderRequest.url).toBe("http://localhost/tasks");
});

it("sends proper arguments to actions (using query string)", async () => {
Expand Down Expand Up @@ -10343,9 +10352,12 @@ describe("a router", () => {
}

function createSubmitRequest(path: string, opts?: RequestInit) {
let searchParams = new URLSearchParams();
searchParams.append("key", "value");

return createRequest(path, {
method: "post",
body: createFormData({ key: "value" }),
body: searchParams,
...opts,
});
}
Expand Down Expand Up @@ -10815,6 +10827,75 @@ describe("a router", () => {
});
});

it("should send proper arguments to loaders", async () => {
let rootLoaderStub = jest.fn(() => "ROOT");
let childLoaderStub = jest.fn(() => "CHILD");
let { query } = createStaticHandler([
{
id: "root",
path: "/",
loader: rootLoaderStub,
children: [
{
id: "child",
path: "child",
loader: childLoaderStub,
},
],
},
]);
await query(createRequest("/child"));

// @ts-expect-error
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.url).toBe("http://localhost/child");
expect(childLoaderRequest.method).toBe("GET");
expect(childLoaderRequest.url).toBe("http://localhost/child");
});

it("should send proper arguments to actions", async () => {
let actionStub = jest.fn(() => "ACTION");
let rootLoaderStub = jest.fn(() => "ROOT");
let childLoaderStub = jest.fn(() => "CHILD");
let { query } = createStaticHandler([
{
id: "root",
path: "/",
loader: rootLoaderStub,
children: [
{
id: "child",
path: "child",
action: actionStub,
loader: childLoaderStub,
},
],
},
]);
await query(createSubmitRequest("/child"));

// @ts-expect-error
let actionRequest = actionStub.mock.calls[0][0]?.request;
expect(actionRequest.method).toBe("POST");
expect(actionRequest.url).toBe("http://localhost/child");
expect(actionRequest.headers.get("Content-Type")).toBe(
"application/x-www-form-urlencoded;charset=UTF-8"
);
expect((await actionRequest.formData()).get("key")).toBe("value");

// @ts-expect-error
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.url).toBe("http://localhost/child");
expect(childLoaderRequest.method).toBe("GET");
expect(childLoaderRequest.url).toBe("http://localhost/child");
});

describe("statusCode", () => {
it("should expose a 200 status code by default", async () => {
let { query } = createStaticHandler([
Expand Down
7 changes: 6 additions & 1 deletion packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,9 @@ export function createRouter(init: RouterInit): Router {
...opts.submission,
};
loadingNavigation = navigation;

// Create a GET request for the loaders
request = createRequest(request.url, request.signal);
}

// Call loaders
Expand Down Expand Up @@ -2198,7 +2201,9 @@ export function unstable_createStaticHandler(
};
}

let context = await loadRouteData(request, matches);
// Create a GET request for the loaders
let loaderRequest = createRequest(request.url, request.signal);
let context = await loadRouteData(loaderRequest, matches);

return {
...context,
Expand Down