Skip to content

Commit

Permalink
Do not attempt to deserialize empty json responses (#11164)
Browse files Browse the repository at this point in the history
* Do not attempt to deserialize empty json responses

* Check for null body instead of content length

* Update test
  • Loading branch information
brophdawg11 committed Jan 9, 2024
1 parent e0d106b commit 0ef1896
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
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

0 comments on commit 0ef1896

Please sign in to comment.