I have a project that uses React
but server rendered. When a client requests an initial page of the website by writing the URL in the search field, the server.js
uses renderToString()
to stringify the react App.js
(with some initial data added) and send it to the client along with the bundle.js
and css
. From this point onwards, React
takes over, the client will navigate through the app without needing an initial data anymore. Whenever they navigate to a different component, componentDidMount()
will request for the corresponding data from the same server.js
.
The problem is that I cannot distinguish between a GET
request from a componentDidMount()
and from the user typing the URL in the search bar. This is crucial for knowing when to send a new markup with an initial data and when to just send a response object.
Right now I am using a very crude method of attaching a querystring
in the GET
request from the componentDidMount()
to identify that it requires a non-initial data/that the request is not an initial request on entering the website.
This method is very messy as in one instance for example, upon refreshing, the non-initial query stays in the url but since its refreshed, it throws all the cached react app, and it then receives the non-initial data displayed nakedly in the browser.
Is there a better way of doing this? Maybe there is an attached information in the fetch get request that shows where the GET
request is generated (whether from a clicked link or a typed URL)?