4

I am currently trying to deploy the default react web app to Azure and I am encountering an issue where though I deploy the contents of my build folder to the azure hosted /site/wwwroot folder I end up on the following page when going to my hosted address: https://[project_name].azurewebsites.net/

Landing Page : enter image description here

I intend to deploy the default create-react-app react application so that I may have the process down for when I deploy my real site. The process I have followed is pretty much exactly what is mentioned in this article https://medium.com/@to_pe/deploying-create-react-app-on-microsoft-azure-c0f6686a4321

  1. Create the default React App with create-react-app
  2. Run "npm run build" to get the build folder
  3. Go into the Azure React Portal and create a new Web App ***
  4. FTP / Git deploy the contents of the local build folder into the Azure website's /site/wwwroot/ folder
  5. For overkill I added the below web.config file to handle future routes, but have also tried without this step

In the end my Azure site's contents look like this

Folder contents : enter image description here

At this point when I try to access the Azure site I get the "Hey, Node developers!" page which implies my code is not deployed. Any thoughts as to why this might be the case?

*** I have a hunch that during the configuring of the Azure Web Api something is not set up correctly perhaps because I select Node 10.14 as my Runtime stack simply because that is the version of Node that I have installed and am using with my local React app.

Thank you folks for your time.

3 Answers 3

16

Another approach is to configure Azure Linux Web App Service to run a startup command to serve your React app in "Settings > General settings > Startup Command":

pm2 serve /home/site/wwwroot/ --no-daemon

Remember to change the path to your build path (The path to your index.html file).

If you use react-router and wants to make any direct access on custom routes be handled by index.html you need to add --spa option on the same command.

pm2 serve /home/site/wwwroot/ --no-daemon --spa

Using --spa option pm2 will automatically redirect all queries to the index.html and then react router will do its magic.

You can find more information about it in pm2 documentation: https://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/#serving-spa-redirect-all-to-indexhtml

I've coped with the same problem recently: React router direct links not working on Azure Web App Linux

6
  • Does deploying like this allow React to pick up environment variables?
    – Rob
    Jul 27, 2020 at 5:49
  • Yes, you can configure your environment variables on azure webs > settings Jul 29, 2020 at 17:29
  • @LuttiCoelho - It doesn't seem to work - my app is NOT picking up the env variables I am setting in the configuration. The only settings I see in the deployed App are those defined in the .env-file... Nov 4, 2021 at 8:51
  • Hi @MartinFrankMoesbyPetersen. A React application can't get information from env variables setted on the server. Are you deploying a bff together with the react app? If your are not deploying any backend application this behavior is expected Nov 6, 2021 at 0:41
  • @LuttiCoelho - Damn... thank you for the reply (even if it was not what I was hoping for). Can I ask if you could point me to a guide on, how to do that? I just deploy my optimized build to the Azure Web App and serve it up with "pm2 serve"... Nov 6, 2021 at 6:58
3

You have created a Linux App Service - your web.config won't work because there is no IIS.

If you don't select node as the runtime stack, your app will work for the most part because it serves the files like a static web host. However I would suggest to keep the runtime stack as node and add the following file to your deployment in the wwwroot folder:

ecosystem.config.js

module.exports = {
    apps: [
        {
            script: "npx serve -s"
        }
    ]
};

https://burkeknowswords.com/this-is-how-to-easily-deploy-a-static-site-to-azure-96c77f0301ff

1
-1

There's an extremely simple way to overcome this problem, and although it is not perfect, it works both on AWS, Microsoft Azure, and probably any other hosting:

  • Just point the error document to: index.html

I found it out here: https://stackoverflow.com/a/52343542/3231884

Disclaimer: This solution is not perfect and impacts SEO. Google doesn't rank well sites that throw 404s.

6
  • This is terrible for SEO and should not be used as there are other solutions just as simple as yours. Sep 4, 2020 at 2:01
  • @LuttiCoelho you probably didn't take the time to read my answer until the end. There's a Disclaimer that says just that. Sep 4, 2020 at 6:33
  • 1
    @LuttiCoelho, 89 people don't agree with you on the original answer. You can check it yourself by clicking the link. It is indeed a gambiarra, I agree. In Portugal we call it "um remendo". I ended up doing something better myself (1 or 2 weeks later) but it was nice to implement this quickly to avoid having the website partially down. Sep 4, 2020 at 12:22
  • 1
    the original answer is about handle the route problem in amazon S3. AFAIK there is no way to handle this problem without having a a reverse proxy between the browser and the app hosted on S3, so the 404 redirecting to index is acceptable to solve it while you don't have a reverse proxy to do this. If this question was about handle react router in azure blob storage, probably your answer will be very well voted too. But on Azure Web Apps (Linux or Windows) we have better ways to cope with it. Thats why I down vote your answer. Sep 29, 2020 at 11:16
  • 1
    But please be aware that I'm NOT the owner of truth. Let's wait and see if anyone agree with you. You have a very good reputation on SO, one down vote is not the end of the world. Sep 29, 2020 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.