Back to blog

Building My First React Site Using Vite

October 12, 20245 min read

In this guide, we will walk you through the process of building and deploying a React site using Vite and GitHub Pages. We'll cover setup, deployment, and troubleshooting common issues like CORS.

AI & Machine Learning Series — 25 articles
  1. Using ChatGPT for C# Development
  2. Trivia Spark: Building a Trivia App with ChatGPT
  3. Creating a Key Press Counter with Chat GPT
  4. Using Large Language Models to Generate Structured Data
  5. Prompt Spark: Revolutionizing LLM System Prompt Management
  6. Integrating Chat Completion into Prompt Spark
  7. WebSpark: Transforming Web Project Mechanics
  8. Accelerate Azure DevOps Wiki Writing
  9. The Brain Behind JShow Trivia Demo
  10. Building My First React Site Using Vite
  11. Adding Weather Component: A TypeScript Learning Journey
  12. Interactive Chat in PromptSpark With SignalR
  13. Building Real-Time Chat with React and SignalR
  14. Workflow-Driven Chat Applications Powered by Adaptive Cards
  15. Creating a Law & Order Episode Generator
  16. The Transformative Power of MCP
  17. The Impact of Input Case on LLM Categorization
  18. The New Era of Individual Agency: How AI Tools Empower Self-Starters
  19. AI Observability Is No Joke
  20. ChatGPT Meets Jeopardy: C# Solution for Trivia Aficionados
  21. Mastering LLM Prompt Engineering
  22. English: The New Programming Language of Choice
  23. Mountains of Misunderstanding: The AI Confidence Trap
  24. Measuring AI's Contribution to Code
  25. Building MuseumSpark - Why Context Matters More Than the Latest LLM

Building My First React Site Using Vite

Introduction

Creating a React site can be a daunting task, especially for beginners. However, with the right tools and guidance, it becomes a manageable and rewarding experience. The path I took uses Vite as a fast modern build tool and GitHub Pages for hosting — along with the inevitable detour through CORS issues that anyone deploying their first SPA will recognize.

Why Choose Vite?

Vite is a build tool that aims to provide a faster and leaner development experience for modern web projects. It offers several advantages:

  • Fast Development: Vite uses native ES modules in the browser, providing instant server start and lightning-fast HMR (Hot Module Replacement).
  • Optimized Build: It uses Rollup for production builds, ensuring optimized and efficient output.
  • Rich Features: Vite supports TypeScript, JSX, CSS, and more out of the box.

Setting Up Your React Project

Step 1: Install Vite

First, ensure you have Node.js installed. Then, run the following command to create a new Vite project:

npm create vite@latest my-react-app --template react

This command sets up a new React project using Vite.

Step 2: Navigate and Start the Development Server

Navigate to your project directory and start the development server:

cd my-react-app
npm install
npm run dev

You should see your React app running at http://localhost:3000.

Deploying to GitHub Pages

Step 1: Configure Your Repository

Create a new repository on GitHub and push your local project to it:

git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin <your-repo-url>
git push -u origin main

Step 2: Deploy with GitHub Actions

Create a .github/workflows/deploy.yml file in your project with the following content:

name: Deploy to GitHub Pages

on:
    push:
        branches:
            - main

jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v2
            - uses: actions/setup-node@v2
              with:
                  node-version: "14"
            - run: npm install
            - run: npm run build
            - uses: peaceiris/actions-gh-pages@v3
              with:
                  github_token: ${{ secrets.GITHUB_TOKEN }}
                  publish_dir: ./dist

This workflow will automatically build and deploy your site to GitHub Pages whenever you push to the main branch.

Troubleshooting Common Issues

Handling CORS

Cross-Origin Resource Sharing (CORS) can be a common issue when deploying web applications. Here are some tips to handle CORS:

  • Use Proxy: Configure a proxy in your vite.config.js to handle API requests during development.
  • Server Configuration: Ensure your server is configured to allow CORS requests.

Conclusion

Building a React site using Vite and deploying it to GitHub Pages is a straightforward process that offers speed and efficiency. By following the steps outlined above, you can quickly get your site up and running while addressing common issues like CORS.

Additional Resources

Start building your React site today and enjoy the seamless experience that Vite provides!