Monday, 17 May 2021

Getting domain in React application

How to host React app in a way, that it uses the domain of the husk HTML instead of having it configured in some config.js as suggested in another answer ( Dynamic configuration variables in Javascript / React )?

The react JS is hosted under same domain as the application API. It is assumed root is always used per application, not subdirectory, so just https://some.domain can be baseUrl. Dev setup for React is not of interest for PROD, and can be set up separately.

For example, there can be many domains, which the same JavaScript code should serve.

Why it is so simple with the normal js code, which can be ignorant of domain, and a problem with React?

Backend is not Node.js, but even backend gets it's domain from HTTP headers, so having to configure React seems very odd.

Is there any way better than having some configuration transponder on the backend?

The idea is that React code is stored without any lists of possible domains as is suggested everywhere. At the moment it needs to be specially build for each domain separately, which is not convenient, and in described scenario of same domain it should be avoided. What can be put into src/config/index.js to make it sense the domain dynamically?



from Getting domain in React application

No comments:

Post a Comment