First of all, create-react-app is an amazing kit. It's a zero configuration bundle that gives you a react app boilerplate with a dev server, linting and a deployment tool. All are awesome but not perfect.

I could go on giving this project praise but if you're here reading this you might be convinced already.

Anyway, the way you deploy a create-react-app project is actually stunningly simple, but there is one major caveat to look out for. Basically running yarn run build will first delete existing files in the ./build/ directory. Files that it indents to replace. For example your ./build/index.html or your ./build/static/js/main.94a86fe3.js.

So, what I suggest is that you deploy it like this:

#!/bin/bash

# Go into the project where the package.json exists
cd myproject
# Upgrade any libraries
yarn
# Use 
yarn run build
mv build build_final

Note! This tip is only applicable if you deploy "in place" as opposed to building a whole new container/image and swapping an old container/image for a new one.

Now, for your Nginx point to the ./build_final directory instead. For example:

# /etc/nginx/sites-enabled/mysite.conf
server {
    server_name mydomain.example.com;
    root /full/path/to/myproject/build_final;

    location / {
        try_files $uri /index.html;
        add_header   Cache-Control public;
        expires      1d;
    }
}

The whole point of this tip is that it's a good idea to not point Nginx to the ./build directory (but to a copy of it instead) because otherwise, during the seconds that yarn run build runs (1-5 seconds) a bunch of files will be missing and Nginx will send 404 errors to the clients unlucky enought to connect during the deployment.

Comments

Anonymous

Thank you so much for this post.

Anonymous

I tried the same thing but actually I configured many servers in Nginx so I cont specify the root inside the location like
server{
server_name example.com;
   location /reactApp/{
            proxy_pass http://127.0.0.1:3000/;
            root /home/tech/React_App/elasticsearch/build/;
            index /home/tech/React_App/elasticsearch/public/index.
      
}
}
but I am getting white screen in the browser while opening on URL("https://example.com/reactApp/") the problem is it was not rendering the root

Your email will never ever be published.

Previous:
Optimization of QuerySet.get() with or without select_related November 3, 2016 Python, Django, PostgreSQL
Next:
Cope with JSONDecodeError in requests.get().json() in Python 2 and 3 November 16, 2016 Python
Related by category:
How to SSG a Vite SPA April 26, 2025 JavaScript, React
Switching from Next.js to Vite + wouter July 28, 2023 JavaScript, React
An ideal pattern to combine React Router with TanStack Query November 18, 2024 JavaScript, React
get in JavaScript is the same as property in Python February 13, 2025 JavaScript
Related by keyword:
Be very careful with your add_header in Nginx! You might make your site insecure February 11, 2018 Linux, Web development, Nginx
Benchmarking npm install with or without audit February 23, 2023 Node, JavaScript
ssl_session_cache in Nginx and the ab benchmark December 31, 2010 Linux, DoneCal
How I simulate a CDN with Nginx May 15, 2019 Python, Nginx