react-native: [Android][fetch] app crashes when response contains special character \u2028

fetch('http://someurl'); if the response contains the character \u2028 , the app crashes with the following error SyntaxError: Unexpected EOF (line 1 in the generated bundle)

It looks like a tricky character. Try copy paste the below statement in your browser console, it should give you an error. var invalidString = “s
.” ; The special character is between the “s” and the “.”;

Running on react-native-0.16.0 This error does not appear during chrome debug mode.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 3
  • Comments: 47 (30 by maintainers)

Commits related to this issue

Most upvoted comments

This is fixed by https://github.com/facebook/react-native/commit/6d3c7b8a4c1d1a0552a21cfabe03a8d1f344d1ed which should make it into the next release whether or not the new bridge is turned on by then.

Thanks a lot @astreet

Cherry picking this to 0.29.1 and 0.30

We got the same issue with a list of posts; as far as we can see it has to do with a JSON length limit on Android. The JSON we use is valid, and if I limit the amount of posts we fetch it works as expected.

JSON parse error: Unrecognized token '< ’

It looks like the JSON might be cut off at some point, if it’s too long.

Tested on: HTC One X - Android 4.4.4.

Yeah, if the next release is going to have the fix then there’s nothing to do really. But if we’re not sure we’re going to be releasing the new bridge (or are going to roll another release to fix this), then we should temporarily patch the networking module

@nihgwu haha yeah, it’s pretty easy but you do need to fork react native and then compile from source. there are instructions in their docs on how to do that for android, it’s not too hard.

You don’t actually need to change much code lol: https://github.com/nightingale/react-native/commit/f78138cdc3b9a5f3be0ea24f163f5ee2709d5205

@calebmer we faced this issue and our solution was basically to try and make it never happen. Since we control every server that speaks to our app, we modified the servers to encode every unicode symbol in responses.

Then in the app we decode the unicode symbols like so:

function decodeUnicode(parent, index) {
  const r = /\\u([\d\w]{4})/gi;
  if (typeof parent[index] === 'string') {
    parent[index] = parent[index].replace(r, (match, grp) => {
      return String.fromCharCode(parseInt(grp, 16));
    });
  }
}

On the server this snippet represents what we do to encode unicode and get rid of newlines (which also breaks JSON):

row[i] = row[i].replace(/\r?\n/g, '\n').replace(/[\u0080-\uFFFF]/g, function(match) {
   return `\\u${('0000' + match.charCodeAt(0).toString(16)).slice(-4)}`;
});