Last active
January 6, 2020 23:36
-
-
Save eliseumds/6192135660267e2c64180a8a9cdb7dd1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function serializeServerDataToJsonString(data: Object): string { | |
const jsonString = JSON.stringify(data); | |
return jsonString | |
.replace(/<\/script/gim, '</_escaped_script') | |
.replace(/<!--/gm, '\\u003C!--') | |
.replace(new RegExp('\u2028', 'g'), '\\u2028') | |
.replace(new RegExp('\u2029', 'g'), '\\u2029') | |
.replace(/\\/g, '\\\\') | |
.replace(/\n/g, '\\n') | |
.replace(/\r/g, '\\r') | |
.replace(/\t/g, '\\t') | |
.replace(/\f/g, '\\f') | |
.replace(/"/g, '\\"') | |
.replace(/'/g, "\\'") | |
.replace(/&/g, '\\&'); | |
} | |
export function deserializeServerDataFromJsonString(jsonString: string): Object { | |
try { | |
return JSON.parse(jsonString.replace(/<\/_escaped_script/gm, '</script')); | |
} catch (error) { | |
console.error(error, { errorName: 'Failed to deserialize server data' }); | |
return JSON.parse(jsonString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's how I render my application state:
And then the rehydration on the client: