@sctg/vite-plugin-github-pages-spa

Vite plugin to enable SPA (Single Page Application) support for GitHub Pages
4,991 17
Install
npm install @sctg/vite-plugin-github-pages-spa
Latest Version:0.2.0
License:MIT
Last Updated:Mar 14, 2026
Links: GitHub  ·  npm
Maintainer: ronanl

Vite Plugin: GitHub Pages SPA Support

A Vite plugin that enables seamless Single Page Application (SPA) support when hosting on GitHub Pages. GitHub Pages doesn't natively support SPAs, which can cause 404 errors when users refresh the page or navigate directly to a route. This plugin solves that problem by:

  1. Creating a 404.html file that redirects all requests to your SPA
  2. Injecting a script in your index.html that handles the redirection

Star the project

If you appreciate my work, please consider giving it a star! 🤩

Installation

npm install @sctg/vite-plugin-github-pages-spa --save-dev
# or
yarn add @sctg/vite-plugin-github-pages-spa --dev
# or
pnpm add @sctg/vite-plugin-github-pages-spa -D

Usage

Add the plugin to your vite.config.ts or vite.config.js:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react'; // or any other framework plugin
import { githubPagesSpa } from "@sctg/vite-plugin-github-pages-spa";

export default defineConfig({
  // Set base if your repo isn't deployed at the root domain
  // For example, if your GitHub Pages URL is 'https://username.github.io/repo-name/'
  base: '/repo-name/',
  
  plugins: [
    react(),
    githubPagesSpa({
      // Options are optional
      verbose: true, // Set to false to disable console logs
    }),
  ],
});

Options

Option Type Default Description
verbose boolean true Whether to show log messages during build
injectScript boolean true Whether to inject the redirection script into index.html
custom404Html string undefined Optional custom HTML content for the 404.html file
title string 404 - Page Not Found Title for the 404.html file (ignored if custom404Html is set)

How It Works

The plugin implements the SPA GitHub Pages technique by:

  1. Adding a script to your index.html that checks for special query parameters and redirects correctly
  2. Creating a 404.html file that handles GitHub Pages 404 errors by redirecting to your index page with those special query parameters

Credits

This plugin is based on the technique described by Rafael Pedicini at spa-github-pages.

License

MIT

Related Packages

nitfixer

Tags React, Vue and Svelte components with stable ids and emits a source manifes...

470
@inkanjs/vite

Your inkan API inside the Vite dev server: one port, reloaded on every save, /do...

421 0
@phoria/phoria

Islands architecture for dotnet powered by Vite.

1,452 9
webpack-vite-serve

为webpack项目提供一键接入Vite的能力

848 15
vite-plugin-dev-inspector

jump to local IDE source code while click the element of browser automatically.

6,946 51

Version History

Version Released License
0.2.0 MIT
0.1.1 MIT
0.1.0 MIT