0

I have the React app. with 3 versions: for developement, testing and production.

They only differ in the URL that is used for the login (different WordPress site).

How do I make the react app agnostic/configurable at runtime and save the need to generate 3 versions?

Mulli
  • 1,598
  • 2
  • 22
  • 39

3 Answers3

0

Just use

    window.location.host // need to add http/s 

to get the URL.

Many other parameters can be found using: URLSearchParams, see URLSearchParams

Mulli
  • 1,598
  • 2
  • 22
  • 39
0

For those that use a Docker container, it can be done with environment variables.

My situation: I made my react app in Visual Studio with template 'ASP.NET Core with React.js and Redux'. It is placed in a docker container which is deployed in Kubernetes.

It took me almost half a day but I managed to do it :)

First I found this post and especially the comment from Patrick Lee Scott is interesting: https://levelup.gitconnected.com/handling-multiple-environments-in-react-with-docker-543762989783

Comment from Patrick Lee Scott: https://patrickleet.medium.com/another-option-build-with-dummy-values-like-replace-api-url-and-then-use-an-entrypoint-sh-db053a799167

The comment is a good start but doesn't show the complete solution.

First I tested the script (and try to figure out what it was doing). During the testing I found out that the 'cat /proc/self/environ' was not working, I replaced it with xargs -0 -L1 -a /proc/self/environ. Second I had trouble getting the script to run via ENTRYPOINT, I figured out that the script needed to begin with: #!/bin/bash Third, I added the original ENTRYPOINT at the bottom of the script.

Here is the modified script of Patrick Lee Scott:

appEntryPoint.sh:

#!/bin/bash
echo "Inserting env variables"

for file in ./ClientApp/build/static/js/*.js
do
  echo "env sub for $file"
  list="$(xargs -0 -L1 -a /proc/self/environ | awk -F= '{print $1}')"
  echo "$list" | while read -r line; do
    export REPLACE="REPLACE_$line"
    export VALUE=$(eval "echo \"\$$line\"")    
    #echo "replacing ${REPLACE} with ${VALUE} in $file"
    sed -i "s~${REPLACE}~${VALUE}~g" $file
    unset REPLACE
    unset VALUE
  done
done

dotnet My.DotNet.ReactApp.dll

To make the answer complete, I will list here my Dockerfile:

Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app/ClientApp
EXPOSE 80
EXPOSE 443

RUN echo "Acquire::Check-Valid-Until \"false\";\nAcquire::Check-Date \"false\";" | cat > /etc/apt/apt.conf.d/10no--check-valid-until && apt-get update -yq \
&& apt-get install -y curl \
&& apt-get install -y libpng-dev libjpeg-dev curl libxi6 build-essential libgl1-mesa-glx \
&& curl -sL https://deb.nodesource.com/setup_lts.x | bash - \
&& apt-get install -y nodejs

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
RUN echo "Acquire::Check-Valid-Until \"false\";\nAcquire::Check-Date \"false\";" | cat > /etc/apt/apt.conf.d/10no--check-valid-until && apt-get update -yq \
&& apt-get install -y curl \
&& apt-get install -y libpng-dev libjpeg-dev curl libxi6 build-essential libgl1-mesa-glx \
&& curl -sL https://deb.nodesource.com/setup_lts.x | bash - \
&& apt-get install -y nodejs

WORKDIR /app/ClientApp
COPY /My.DotNet.ReactApp/ClientApp/package*.json ./
RUN npm install --silent
COPY /My.DotNet.ReactApp/ClientApp ./
RUN npm run build

WORKDIR /app/publish/ClientApp
RUN cp -r /app/ClientApp/build .

WORKDIR /app
COPY /My.DotNet.ReactApp ./
RUN dotnet restore "My.DotNet.ReactApp.csproj"
RUN dotnet build "My.DotNet.ReactApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "My.DotNet.ReactApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

COPY ./appEntryPoint.sh ./
RUN chmod +x appEntryPoint.sh

ENTRYPOINT ["/app/appEntryPoint.sh"]

What you now have to do is put in your .env file placeholders:

.env.production

REACT_APP_API_ENDPOINT=REPLACE_REACT_APP_API_ENDPOINT
REACT_APP_API_SOME_OTHER_URL=REPLACE_REACT_APP_API_SOME_OTHER_URL

Now you can set the real values for the react variables as environment variables on the container, the script reads the environment variables from the container and will replace all values that begin with "REPLACE_" So in this case we need to set these environment variables on the container used for production:

REACT_APP_API_ENDPOINT=https://prod.endpoint.com
REACT_APP_API_SOME_OTHER_URL=https://prod.url.com

And for the test environment:

REACT_APP_API_ENDPOINT=https://test.endpoint.com
REACT_APP_API_SOME_OTHER_URL=https://test.url.com
Patrick Koorevaar
  • 1,219
  • 15
  • 24
-1

Use .env file. Check out this link for installation. At the end you will have such kind of structure in you app folder

screenshot

Jasur Kurbanov
  • 724
  • 2
  • 9
  • 20