Skip to content

Commit 0ef1896

Browse files
authored
Do not attempt to deserialize empty json responses (#11164)
* Do not attempt to deserialize empty json responses * Check for null body instead of content length * Update test
1 parent e0d106b commit 0ef1896

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

.changeset/unlucky-planets-repeat.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+
Do not attempt to deserialize empty JSON responses

packages/router/__tests__/navigation-test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@ describe("navigations", () => {
114114
});
115115
});
116116

117+
// See: https://github.com/remix-run/react-router/issues/11145
118+
it("does not attempt to deserialize empty json responses", async () => {
119+
let t = initializeTest();
120+
let A = await t.navigate("/foo");
121+
await A.loaders.foo.resolve(
122+
new Response(null, {
123+
headers: {
124+
"Content-Type": "application/json",
125+
},
126+
})
127+
);
128+
expect(t.router.state.errors).toBeNull();
129+
expect(t.router.state.loaderData).toMatchObject({
130+
root: "ROOT",
131+
foo: null,
132+
});
133+
});
134+
117135
it("unwraps non-redirect text Responses", async () => {
118136
let t = initializeTest();
119137
let A = await t.navigate("/foo");

packages/router/router.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4098,7 +4098,11 @@ async function callLoaderOrAction(
40984098
// Check between word boundaries instead of startsWith() due to the last
40994099
// paragraph of https://httpwg.org/specs/rfc9110.html#field.content-type
41004100
if (contentType && /\bapplication\/json\b/.test(contentType)) {
4101-
data = await result.json();
4101+
if (result.body == null) {
4102+
data = null;
4103+
} else {
4104+
data = await result.json();
4105+
}
41024106
} else {
41034107
data = await result.text();
41044108
}

0 commit comments

Comments
 (0)