Skip to content

Commit ef6bce6

Browse files
authored
Fix loader request method (#9660) (#9680)
* Fix loader request method * add changeset
1 parent 6d6b277 commit ef6bce6

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

.changeset/pretty-kiwis-study.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/router": patch
3+
---
4+
5+
Fix requests sent to revalidating loaders so they reflect a GET request

packages/router/__tests__/router-test.ts

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5052,12 +5052,21 @@ describe("a router", () => {
50525052
// Assert request internals, cannot do a deep comparison above since some
50535053
// internals aren't the same on separate creations
50545054
let request = nav.actions.tasks.stub.mock.calls[0][0].request;
5055-
expect(request.url).toBe("http://localhost/tasks");
50565055
expect(request.method).toBe("POST");
5056+
expect(request.url).toBe("http://localhost/tasks");
50575057
expect(request.headers.get("Content-Type")).toBe(
50585058
"application/x-www-form-urlencoded;charset=UTF-8"
50595059
);
50605060
expect((await request.formData()).get("query")).toBe("params");
5061+
5062+
await nav.actions.tasks.resolve("TASKS ACTION");
5063+
let rootLoaderRequest = nav.loaders.root.stub.mock.calls[0][0].request;
5064+
expect(rootLoaderRequest.method).toBe("GET");
5065+
expect(rootLoaderRequest.url).toBe("http://localhost/tasks");
5066+
5067+
let tasksLoaderRequest = nav.loaders.tasks.stub.mock.calls[0][0].request;
5068+
expect(tasksLoaderRequest.method).toBe("GET");
5069+
expect(tasksLoaderRequest.url).toBe("http://localhost/tasks");
50615070
});
50625071

50635072
it("sends proper arguments to actions (using query string)", async () => {
@@ -10379,9 +10388,12 @@ describe("a router", () => {
1037910388
}
1038010389

1038110390
function createSubmitRequest(path: string, opts?: RequestInit) {
10391+
let searchParams = new URLSearchParams();
10392+
searchParams.append("key", "value");
10393+
1038210394
return createRequest(path, {
1038310395
method: "post",
10384-
body: createFormData({ key: "value" }),
10396+
body: searchParams,
1038510397
...opts,
1038610398
});
1038710399
}
@@ -10851,6 +10863,75 @@ describe("a router", () => {
1085110863
});
1085210864
});
1085310865

10866+
it("should send proper arguments to loaders", async () => {
10867+
let rootLoaderStub = jest.fn(() => "ROOT");
10868+
let childLoaderStub = jest.fn(() => "CHILD");
10869+
let { query } = createStaticHandler([
10870+
{
10871+
id: "root",
10872+
path: "/",
10873+
loader: rootLoaderStub,
10874+
children: [
10875+
{
10876+
id: "child",
10877+
path: "child",
10878+
loader: childLoaderStub,
10879+
},
10880+
],
10881+
},
10882+
]);
10883+
await query(createRequest("/child"));
10884+
10885+
// @ts-expect-error
10886+
let rootLoaderRequest = rootLoaderStub.mock.calls[0][0]?.request;
10887+
// @ts-expect-error
10888+
let childLoaderRequest = childLoaderStub.mock.calls[0][0]?.request;
10889+
expect(rootLoaderRequest.method).toBe("GET");
10890+
expect(rootLoaderRequest.url).toBe("http://localhost/child");
10891+
expect(childLoaderRequest.method).toBe("GET");
10892+
expect(childLoaderRequest.url).toBe("http://localhost/child");
10893+
});
10894+
10895+
it("should send proper arguments to actions", async () => {
10896+
let actionStub = jest.fn(() => "ACTION");
10897+
let rootLoaderStub = jest.fn(() => "ROOT");
10898+
let childLoaderStub = jest.fn(() => "CHILD");
10899+
let { query } = createStaticHandler([
10900+
{
10901+
id: "root",
10902+
path: "/",
10903+
loader: rootLoaderStub,
10904+
children: [
10905+
{
10906+
id: "child",
10907+
path: "child",
10908+
action: actionStub,
10909+
loader: childLoaderStub,
10910+
},
10911+
],
10912+
},
10913+
]);
10914+
await query(createSubmitRequest("/child"));
10915+
10916+
// @ts-expect-error
10917+
let actionRequest = actionStub.mock.calls[0][0]?.request;
10918+
expect(actionRequest.method).toBe("POST");
10919+
expect(actionRequest.url).toBe("http://localhost/child");
10920+
expect(actionRequest.headers.get("Content-Type")).toBe(
10921+
"application/x-www-form-urlencoded;charset=UTF-8"
10922+
);
10923+
expect((await actionRequest.formData()).get("key")).toBe("value");
10924+
10925+
// @ts-expect-error
10926+
let rootLoaderRequest = rootLoaderStub.mock.calls[0][0]?.request;
10927+
// @ts-expect-error
10928+
let childLoaderRequest = childLoaderStub.mock.calls[0][0]?.request;
10929+
expect(rootLoaderRequest.method).toBe("GET");
10930+
expect(rootLoaderRequest.url).toBe("http://localhost/child");
10931+
expect(childLoaderRequest.method).toBe("GET");
10932+
expect(childLoaderRequest.url).toBe("http://localhost/child");
10933+
});
10934+
1085410935
describe("statusCode", () => {
1085510936
it("should expose a 200 status code by default", async () => {
1085610937
let { query } = createStaticHandler([

packages/router/router.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,9 @@ export function createRouter(init: RouterInit): Router {
952952
...opts.submission,
953953
};
954954
loadingNavigation = navigation;
955+
956+
// Create a GET request for the loaders
957+
request = createRequest(request.url, request.signal);
955958
}
956959

957960
// Call loaders
@@ -2202,7 +2205,9 @@ export function unstable_createStaticHandler(
22022205
};
22032206
}
22042207

2205-
let context = await loadRouteData(request, matches);
2208+
// Create a GET request for the loaders
2209+
let loaderRequest = createRequest(request.url, request.signal);
2210+
let context = await loadRouteData(loaderRequest, matches);
22062211

22072212
return {
22082213
...context,

0 commit comments

Comments
 (0)