Deploy React
This guide will help you deploy a React application to Deployxa.
Overview
React is a JavaScript library for building user interfaces. Deployxa provides optimized deployments for React applications with automatic configuration.
Features:
- Automatic framework detection
- Vite and Create React App support
- Static site generation
- Automatic HTTPS
- Global CDN
- Zero configuration
Prerequisites
- A React application (Vite or Create React App)
- A GitHub repository with your code
- A Deployxa account
Quick Start
1. Create Your React App
Using Vite (Recommended):
npm create vite@latest my-react-app -- --template react
cd my-react-app
npm install
Using Create React App:
npx create-react-app my-react-app
cd my-react-app
2. Push to GitHub
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/yourusername/my-react-app.git
git push -u origin main
3. Deploy to Deployxa
- Go to Deployxa Dashboard
- Click "New Project"
- Select your GitHub repository
- Deployxa will automatically detect React
- Click "Deploy"
Your app will be live in under 30 seconds!
Configuration
Detected Settings
Deployxa automatically detects these settings:
| Setting | Value |
|---|---|
| Framework | React |
| Build Command | npm run build |
| Output Directory | dist (Vite) or build (CRA) |
| Start Command | Static site (no server needed) |
| Port | 80 |
Custom Configuration
If you need to customize settings:
- Go to Project → Settings → Build
- Modify settings as needed
- Save changes
Example Custom Settings:
{
"buildCommand": "npm run build",
"outputDirectory": "dist",
"installCommand": "npm ci"
}
Environment Variables
Adding Environment Variables
- Go to Project → Environment
- Click "Add Variable"
- Enter key and value
- Click "Save"
Common React Environment Variables:
VITE_API_URL=https://api.example.com
VITE_APP_TITLE=My React App
REACT_APP_API_URL=https://api.example.com
REACT_APP_TITLE=My React App
Note:
- Vite uses
VITE_prefix - Create React App uses
REACT_APP_prefix - Variables are embedded at build time
Using Environment Variables
In Vite:
const apiUrl = import.meta.env.VITE_API_URL;
const appTitle = import.meta.env.VITE_APP_TITLE;
In Create React App:
const apiUrl = process.env.REACT_APP_API_URL;
const appTitle = process.env.REACT_APP_TITLE;
Build Process
What Happens During Build
- Install Dependencies:
npm installornpm ci - Run Build:
npm run build - Optimize Assets: Minify JS, CSS, images
- Generate Static Files: Create optimized HTML, JS, CSS
- Deploy to CDN: Distribute to global edge network
Build Output
React generates:
dist/orbuild/- Build output directoryindex.html- Main HTML fileassets/- JavaScript and CSS bundles- Static assets (images, fonts)
Routing
Client-Side Routing
For single-page applications with routing:
React Router:
import { BrowserRouter, Routes, Route } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</BrowserRouter>
);
}
Handling 404s
Configure your deployment to handle client-side routing:
Option 1: Redirect to index.html
Deployxa automatically handles this for React apps.
Option 2: Custom 404 page
Create a 404.html file in your public directory.
Custom Domains
Adding a Custom Domain
- Go to Project → Domains
- Click "Add Domain"
- Enter your domain (e.g.,
example.com) - Configure DNS records
- Wait for verification
DNS Configuration
For Apex Domain (example.com):
Type: A
Name: @
Value: 76.76.21.21
For Subdomain (www.example.com):
Type: CNAME
Name: www
Value: cname.deployxa.com
SSL Certificate
Deployxa automatically provisions SSL certificates:
- Free Let's Encrypt certificates
- Automatic renewal
- HTTPS enforced
- Wildcard support
Performance Optimization
Code Splitting
Split your code for faster initial loads:
import { lazy, Suspense } from 'react';
const About = lazy(() => import('./About'));
function App() {
return (
<Suspense fallback={<div>Loading...</div>}>
<About />
</Suspense>
);
}
Image Optimization
Use optimized images:
import optimizedImage from './image.jpg?w=500&h=500&format=webp';
Bundle Analysis
Analyze your bundle size:
npm install --save-dev rollup-plugin-visualizer
Add to vite.config.js:
import { visualizer } from 'rollup-plugin-visualizer';
export default {
plugins: [
visualizer({ open: true })
]
};
Enable Compression
Deployxa automatically enables gzip and brotli compression.
Monitoring
Viewing Logs
- Go to Project → Logs
- Select log type:
- Build Logs: Deployment process
- Runtime Logs: Client-side errors (if configured)
Monitoring Metrics
- Go to Project → Analytics
- View metrics:
- Page views
- Unique visitors
- Bandwidth usage
- Response times
Error Tracking
Implement error tracking:
Using Sentry:
npm install @sentry/react
import * as Sentry from "@sentry/react";
Sentry.init({
dsn: "YOUR_SENTRY_DSN",
});
Troubleshooting
Build Failed: Module Not Found
Problem: Module not found: Can't resolve 'package-name'
Solution:
- Check
package.jsonhas the dependency - Run
npm install package-namelocally - Commit and push changes
- Redeploy
Blank Page After Deployment
Problem: Application shows blank page
Solution:
- Check browser console for errors
- Verify build output directory is correct
- Ensure
index.htmlis in the root of output directory - Check environment variables are set correctly
Routing Not Working
Problem: Direct URL access returns 404
Solution:
- Deployxa automatically handles client-side routing
- If issue persists, check routing configuration
- Ensure you're using BrowserRouter (not HashRouter)
Environment Variables Not Working
Problem: Environment variables are undefined
Solution:
- Use correct prefix (
VITE_orREACT_APP_) - Redeploy after adding variables
- Check variable names match exactly
- Verify variables are set in correct environment
Slow Initial Load
Problem: Application takes too long to load
Solution:
- Enable code splitting
- Optimize images
- Remove unused dependencies
- Enable lazy loading
- Use React.memo for expensive components
Best Practices
Project Structure
my-react-app/
├── public/
│ └── favicon.ico
├── src/
│ ├── components/
│ ├── pages/
│ ├── hooks/
│ ├── utils/
│ ├── App.jsx
│ └── main.jsx
├── .env
├── .env.example
├── vite.config.js
├── package.json
└── README.md
Environment Variables
- Use
.env.localfor local development - Never commit
.envfiles - Use correct prefix for your build tool
- Document required variables in README
Performance
- Enable code splitting
- Use React.lazy for route-based splitting
- Optimize images
- Minimize bundle size
- Use React.memo appropriately
- Implement virtualization for long lists
Security
- Never expose secrets in client code
- Validate all inputs
- Use HTTPS only
- Keep dependencies updated
- Use Content Security Policy
Advanced Configuration
Vite Configuration
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
build: {
outDir: 'dist',
sourcemap: false,
minify: 'terser',
rollupOptions: {
output: {
manualChunks: {
vendor: ['react', 'react-dom'],
},
},
},
},
server: {
port: 3000,
},
});
Create React App Configuration
// craco.config.js
module.exports = {
webpack: {
configure: (webpackConfig) => {
// Custom webpack configuration
return webpackConfig;
},
},
};
Custom Build Script
{
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"analyze": "vite-bundle-visualizer"
}
}
Examples
Basic React App
// src/App.jsx
import { useState } from 'react';
function App() {
const [count, setCount] = useState(0);
return (
<div>
<h1>React on Deployxa</h1>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
</div>
);
}
export default App;
With API Integration
// src/App.jsx
import { useState, useEffect } from 'react';
function App() {
const [data, setData] = useState(null);
useEffect(() => {
fetch(import.meta.env.VITE_API_URL + '/data')
.then(res => res.json())
.then(data => setData(data));
}, []);
return (
<div>
<h1>Data from API</h1>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
);
}
export default App;
With Routing
// src/App.jsx
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
import Home from './pages/Home';
import About from './pages/About';
function App() {
return (
<BrowserRouter>
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</BrowserRouter>
);
}
export default App;
Related Topics
Resources
- React Documentation
- Vite Documentation
- Create React App
- Deployxa CLI
- API Reference
- Community Discord
Need Help? Contact support@deployxa.com or join our community Discord.