• Home
  • Blog
  • Contact
  • Book a call

© Copyright 2026 | Vladyslav Dihtiarenko | All Rights Reserved

Fixing Empty Pages & Missing Assets in Vite React + React Router (GitHub Pages)

Jun 9, 2024

•

#vite

#react

#github

•

3 mins read

Are you facing a frustrating blank screen or 404 errors for your assets after deploying your awesome Vite React app with React Router to GitHub Pages? Fret no more! This comprehensive guide tackles those common deployment hurdles and equips you with solutions for a smooth transition from local development to GitHub Pages.

Introduction

Following a previous video on deploying Vite React apps, many viewers encountered issues like empty pages and missing assets. This blog post addresses those specific concerns and provides clear explanations and solutions. We'll delve into the reasons behind these problems and guide you through the steps to fix them, ensuring your deployed app functions flawlessly.

The Culprits Behind Deployment Headaches

Let's imagine we're working with a basic Vite React project with React Router in our trusty VSCode editor. We have various pages – Home, About, and a Gallery showcasing some beautiful cityscapes.

Empty Page After Deployment (The Base URL Mismatch):

Deploying to GitHub Pages often involves setting a base URL in your Vite configuration. While this might seem straightforward, running a dev server after this change can result in a blank screen. The same can happen to your deployed version.

Why does this occur?

By default, React Router assumes the root URL (/) for routing. However, when you deploy your app to GitHub Pages, it resides within a subfolder (e.g., username.github.io/repo-name). This mismatch between the assumed root URL and the actual deployment location throws React Router off, leading to an empty page.

The Solution: The basename Attribute

Fortunately, there's a simple fix! We can add the basename attribute to the BrowserRouter component in your App.js (or your main entry point). This attribute informs React Router of the base URL it should use for routing, ensuring your links function correctly on the deployed version.

import { BrowserRouter, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter basename="/your-subfolder-name">  {/* Replace with your actual subfolder path */}
      <Routes>
        {/* Your routes here */}
      </Routes>
    </BrowserRouter>
  );
}

export default App;

By the way, the base URL set in your Vite configuration also becomes available as an environment variable – import.meta.env.BASE_URL. This variable resolves to the public base path you defined earlier.

Missing Assets (Broken Images and Fonts):

Navigating to the Gallery page might reveal a collection of broken images and 404 errors flooding your console. Why? Let's explore the cause.

The Reason: String-Based Asset Paths in JSX

If you're trying to access assets within the public folder using a string directly in your JSX, that's likely the culprit.

The Solution: Two Options for Handling Asset Paths

We have two options to ensure proper asset path handling:

Option 1: Static Import (For Static Assets)

For static assets like logos or icons, you can directly import them within your components:

// MyComponent.js
import logo from './logo.png';

function MyComponent() {
  return (
    <img src={logo} alt="Logo" />
  );
}

export default MyComponent;

Vite automatically resolves the correct URL for statically imported assets, taking into account the base URL configuration you have in place.

Option 2: Dynamic Asset Path (For Dynamic URLs)

If you need to dynamically construct asset paths based on data (like image URLs), leverage import.meta.env.BASE_URL:

// MyComponent.js
import { import } from 'react-router-dom';

function MyComponent({ imageUrl }) {
  const baseUrl = import.meta.env.BASE_URL;  // Static replacement during build
  return (
    <img src={`${baseUrl}${imageUrl}`} alt="Dynamic Image" />
  );
}

export default MyComponent;

Conclusion: Deployment Made Easy

By implementing these solutions – fixing the basename for React Router and handling asset paths appropriately – you can successfully deploy your Vite React apps to GitHub Pages and eliminate those common deployment roadblocks. Now you can confidently showcase your stunning creations to the world!

Related resources:

How to deploy Vite React App to GitHub Pages (Video)

basename attribute in React Router library

Vite’s BASE_URL