diff --git a/packages/react-fetch/src/ReactFetchNode.js b/packages/react-fetch/src/ReactFetchNode.js index 2b1ef5f200cb1..b44c4c904d9e9 100644 --- a/packages/react-fetch/src/ReactFetchNode.js +++ b/packages/react-fetch/src/ReactFetchNode.js @@ -154,9 +154,12 @@ Response.prototype = { return this._json; } const buffer = readRecordValue(this._bufferRecord); - const json = JSON.parse(buffer.toString()); - this._json = json; - return json; + try { + this._json = JSON.parse(buffer.toString()); + } catch (error) { + return this.text(); + } + return this._json; }, text() { if (this._text !== null) { diff --git a/packages/react-fetch/src/__tests__/ReactFetchNode-test.js b/packages/react-fetch/src/__tests__/ReactFetchNode-test.js index e7c4884c40554..67ef79a11c797 100644 --- a/packages/react-fetch/src/__tests__/ReactFetchNode-test.js +++ b/packages/react-fetch/src/__tests__/ReactFetchNode-test.js @@ -118,4 +118,30 @@ describe('ReactFetchNode', () => { expect(err.message).toEqual('Invalid URL: BOOM'); } }); + + // @gate experimental || www + it('handles different paths with json', async () => { + serverImpl = (req, res) => { + switch (req.url) { + case '/banana': + res.write('banana'); + break; + case '/mango': + res.write('mango'); + break; + case '/orange': + res.write('orange'); + break; + } + res.end(); + }; + const outputs = await waitForSuspense(() => { + return [ + fetch(serverEndpoint + 'banana').json(), + fetch(serverEndpoint + 'mango').json(), + fetch(serverEndpoint + 'orange').json(), + ]; + }); + expect(outputs).toMatchObject(['banana', 'mango', 'orange']); + }); });