My client app is built with Create React App, which comes which lots of conveniences built-in. However it's not possible to do server side rendering out of the box. As a result, once the build command is run, all of the output is effectively static.
I have a use case where I do NOT want to switch to complete SSR setup, but would like to dynamically add some data to index.html file so that it is immediately available in javascript when client first loads file.
I worked out the following solution:
- React app runs as a docker container, using the
servelib to serve static build content - A separate node service runs in a different docker container and has access to the build content from the react app via a shared volume
- The node service runs a function every few minutes that reads the contents of
index.htmlfile usingfs, inserts some additional data into a script tag (e.g.window.myData={someKey: 'someValue'}), and writes the updated string toindex.html.
Locally using docker-compose, this works great. However, I'm wondering about possible ramifications of this approach, especially cases where an incoming request for the react app will fail because of some kind of file lock on index.html as it's being read / written by the node service.
I don't think this would be an issue, but I had enough doubt to post this question. The last thing I can afford are failed requests in my production app because of some unforeseen issue.
Any advice, suggestions, anecdotes, etc. are appreciated!