Vite
Setting up a static React site (using Vite for modern build tooling) with Netlify is a quick, high-performance solution for single-page applications (SPAs) and static websites.
Here is a detailed, step-by-step guide covering the entire process.
1. Local React (Vite) Setup and Project Initialization
We'll use Vite as the build tool, which is faster and easier to configure for a simple React SPA than the traditional Create React App (CRA). You'll need Node.js and npm (or yarn/pnpm).
1.1. Create a New React Project
- Initialize the project using Vite:
- Navigate into the directory and install dependencies:
1.2. Local Test
Run the development server to verify the default site.
Your site will be running at http://localhost:5173/
. Press Ctrl+C to stop the server.
2. Custom Layouts, Partials, and Static Assets
In a typical static React application, Layouts are implemented as parent components, and Partials are reusable child components.
2.1. Project File/Folder Structure Tree
Your project structure, after customization, will look like this:
my-react-site/
├── node_modules/ # Node.js dependencies
├── dist/ # ⚠️ Generated static site (Publish Directory for Netlify)
├── public/ # Static assets copied directly to the output root
│ ├── favicon.svg
│ └── images/
│ └── logo.png
├── src/ # All source code
│ ├── components/ # Reusable React components (Partials/UI elements)
│ │ ├── Footer.jsx
│ │ └── Header.jsx
│ ├── layouts/ # Layout components (Wrappers for pages/routes)
│ │ └── MainLayout.jsx
│ ├── App.jsx # Main application component, usually uses the Layout
│ ├── main.jsx # Entry file (renders App.jsx)
│ └── index.css # Global application styles
├── index.html # Main HTML entry file
├── package.json # Node.js dependencies and run scripts
└── vite.config.js # Vite build configuration
2.2. Create the Custom Layout
The Layout component wraps the main content and provides consistent structure (header, footer, shared styling).
- Create the Layouts directory:
- Create the Main Layout Component:
- File:
src/layouts/MainLayout.jsx
import React from 'react'; import Header from '../components/Header'; import Footer from '../components/Footer'; const MainLayout = ({ children }) => { return ( <div className="app-container"> <Header /> <main className="content"> {children} {/* Page content is injected via 'children' prop */} </main> <Footer /> </div> ); }; export default MainLayout;
- File:
2.3. Create Components (Partials)
- Create the Components directory:
- Create Header Component:
- File:
src/components/Header.jsx
- File:
- Create Footer Component:
- File:
src/components/Footer.jsx
- File:
2.4. Apply the Layout
Update the main App.jsx
component to use the new layout.
-
Update
src/App.jsx
:import MainLayout from './layouts/MainLayout'; function App() { // In a real app, this would be a router (e.g., React Router) // rendering different components based on the URL. return ( <MainLayout> <h1>Welcome to my React SPA!</h1> <p>This content is wrapped by the custom MainLayout.</p> </MainLayout> ); } export default App;
2.5. Manage Static Assets
Assets in the public/
folder are copied directly to the root of the dist/
folder during the build.
- Add a Custom Asset: Place a logo in
public/images/logo.png
. - Reference the asset: You can reference it directly from the root path in your HTML or JSX:
2.6. Final Local Check
- Build the static site:
This creates the static HTML, CSS, and JS files in the
dist/
directory. - Run the preview server: Verify the fully static site is working before deployment.
3. GitHub Setup
3.1. Verify .gitignore
The generated .gitignore
file should correctly exclude the build output and Node modules. Ensure it contains:
3.2. Commit and Push to GitHub
- Initialize Git (if not already done by Vite):
- Add all files and commit:
- Create a new repository on GitHub (e.g., named
my-react-netlify-site
). - Link your local repo and push: (Replace placeholders)
4. Netlify Setup for Continuous Deployment
4.1. Sign Up and Import Project
- Go to the Netlify website and log in.
- In the Netlify dashboard, click "Add new site" then "Import an existing project".
- Select "GitHub" and choose your repository (
my-react-netlify-site
).
4.2. Configure Build Settings
Netlify often auto-detects React/Vite projects, but you must confirm the settings.
Setting | Value | Note |
---|---|---|
Branch to deploy | main |
The main branch of your repository. |
Build command | npm run build |
Runs the script defined in your package.json to create the static bundle. |
Publish directory | dist |
The default folder where Vite outputs the static files. |
4.3. Configure Netlify Redirects (Crucial for SPAs)
Since React is an SPA (Single Page Application), Netlify needs to know that any path should fall back to the main index.html
file.
-
Create the Netlify configuration file:
-
Add the redirect rule:
- File:
public/_redirects
(Since this is in thepublic
folder, it will be copied todist/_redirects
.)
This rule ensures that any request (
/*
) that doesn't match a static file is served byindex.html
with a 200 OK status, allowing the React Router to handle the URL. - File:
-
Commit and push the redirects file:
4.4. Deploy the Site
Netlify will detect the new commit and automatically start the build and deploy process.
Your static React site will be live and functional on a temporary Netlify URL.
5. Ongoing Workflow
Your Continuous Deployment (CD) pipeline is now fully automated:
- Make changes locally (update components, add new routes).
- Commit your changes:
- Push to GitHub:
- Netlify automatically detects the push, runs the Vite build (
npm run build
), and deploys the new static version live.