title | permalink | excerpt | last_modified_at |
---|---|---|---|
Injecting Data from the Server into the Page |
/create-react-app/inject-data-from-the-server-into-the-page |
2018-05-18 15:58:49 -0400 |
Similarly to the next section, you can leave some placeholders in the HTML that inject global variables, for example:
<!doctype html>
<html lang="en">
<head>
<script>
window.SERVER_DATA = __SERVER_DATA__;
</script>
Then, on the server, you can replace __SERVER_DATA__
with a JSON of real data right before sending the response. The client code can then read window.SERVER_DATA
to use it. Make sure to sanitize the JSON before sending it to the client as it makes your app vulnerable to XSS attacks.