Back to Blog
Lesson 36 of the Intermediate React: Hooks, State & Data Patterns course
ReactJune 26, 20264 min read

Deploying the Application: From Local Build to Production

Master the production build process for your React dashboard. Learn to handle environment variables, optimize assets, and deploy securely to the web.

ReactDeploymentProductionViteWeb Developmentjavascriptfrontend

Previously in this course, we reached finalizing-dashboard-data-flow-ensuring-state-consistency, where we audited our state management and ensured our data flow was robust. Now that our dashboard is feature-complete, it's time to move it from your local machine to the public web.

Deployment is more than just uploading files; it’s about creating a hardened, optimized version of your application that is ready for real users.

Building for Production

When you run npm run dev during development, Vite serves your code with source maps, hot module replacement (HMR), and unminified files. This is great for debugging, but it’s disastrous for performance.

To prepare for production, we need to run a build process. This transforms your JSX, optimizes your imports, and minifies your JavaScript and CSS. Think of it as "tree-shaking"—the process of removing code that isn't actually used, which significantly reduces the bundle size.

To build your dashboard, run:

Bash
npm run build

This command generates a dist/ directory. This folder contains the static assets (HTML, CSS, JS) that you will serve to your users. If you want to preview how this looks locally before pushing it to a host, run npm run preview.

Managing Environment Variables

In our dashboard, we’ve been connecting to various API endpoints and authentication services. You likely have these hardcoded or stored in a .env file.

Crucial rule: Never expose sensitive secrets (like API keys or database credentials) in your client-side code. Since React runs in the user's browser, anything you include in your bundle can be inspected by anyone.

How to use environment variables safely:

  1. Prefixing: Vite requires variables to be prefixed with VITE_ to be exposed to the client (e.g., VITE_API_URL).
  2. The .env file: Create a .env.production file. Vite will automatically prioritize this file when you run the build command.
  3. Git ignore: Always ensure your .env files are in your .gitignore. You should commit a .env.example file instead, containing the keys but not the secret values.

Deployment Best Practices

Once you have a production build, where does it go? For a static React SPA (Single Page Application), you don't need a heavy server. You need a static host.

1. Choose a Static Host

Platforms like Vercel, Netlify, or Cloudflare Pages are designed specifically for this. They listen to your Git repository, trigger the npm run build command automatically, and deploy the resulting dist/ folder to a global Content Delivery Network (CDN).

2. Configure Your Routes

Since we are using React Router, you might encounter the "404 on refresh" bug. This happens because the browser tries to find a file (like /dashboard/settings) on your server that doesn't exist as a physical file.

You must configure your host to "fallback" all requests to index.html. If you are using Vercel or Netlify, this is often handled automatically, but if you are deploying a side project on a single cheap VPS reliably, you’ll need to configure your Nginx or Apache server to redirect requests to index.html.

3. Asset Optimization

You should master a production build pipeline to ensure your assets are compressed. Modern tools like Vite do this out of the box, but you should verify your bundle size using the rollup-plugin-visualizer to ensure you aren't shipping massive third-party libraries unnecessarily.

Hands-on Exercise: Preparing the Dashboard

  1. Create a .env.production file in your root directory.
  2. Add your production API URL: VITE_API_URL=https://api.yourdashboard.com.
  3. Update your API service files to use import.meta.env.VITE_API_URL instead of a hardcoded string.
  4. Run npm run build and inspect the dist/ folder. Ensure your index.html and assets are correctly generated.
  5. Deploy your project to a platform like Netlify or Vercel by connecting your GitHub repository.

Common Pitfalls

  • Hardcoding secrets: As mentioned, never commit your actual API keys. If you accidentally do, revoke them immediately—they are considered compromised.
  • Ignoring the build error: If your build fails locally, it will fail on the server. Don't assume "it works on my machine" means it will work in production.
  • Missing build steps: If you use professional build workflows to handle custom assets, ensure your CI/CD pipeline triggers those tasks before the final production artifact is created.

Recap

We’ve successfully prepared our dashboard for the world. We learned that a production build is a minified, optimized version of our site, that environment variables must be handled with care to keep secrets out of the browser, and that deployment requires server-side configuration to support client-side routing. By following these steps, your dashboard is now ready to handle real-world traffic securely and efficiently.

Up next: We'll dive into Advanced Hook Composition to make our dashboard logic even more modular and maintainable.

Similar Posts