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

Do not attempt to deserialize empty json responses #11164

Merged
merged 3 commits into from
Jan 9, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/unlucky-planets-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Do not attempt to deserialize empty JSON responses
18 changes: 18 additions & 0 deletions packages/router/__tests__/navigation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ describe("navigations", () => {
});
});

// See: https://github.com/remix-run/react-router/issues/11145
it("does not attempt to deserialize empty json responses", async () => {
let t = initializeTest();
let A = await t.navigate("/foo");
await A.loaders.foo.resolve(
new Response(null, {
headers: {
"Content-Type": "application/json",
},
})
);
expect(t.router.state.errors).toBeNull();
expect(t.router.state.loaderData).toMatchObject({
root: "ROOT",
foo: null,
});
});

it("unwraps non-redirect text Responses", async () => {
let t = initializeTest();
let A = await t.navigate("/foo");
Expand Down
6 changes: 5 additions & 1 deletion packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4098,7 +4098,11 @@ async function callLoaderOrAction(
// Check between word boundaries instead of startsWith() due to the last
// paragraph of https://httpwg.org/specs/rfc9110.html#field.content-type
if (contentType && /\bapplication\/json\b/.test(contentType)) {
data = await result.json();
if (result.body == null) {
data = null;
} else {
data = await result.json();
}
} else {
data = await result.text();
}
Expand Down