Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,39 @@ import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
import { vitest } from "vitest";
import svgr from "vite-plugin-svgr";
import { copyFileSync, existsSync } from "node:fs";
import { resolve } from "node:path";

/**
* GitHub Pages serves static files only — it has no rewrite rule to map client-side
* routes back to the app shell. So a direct visit to (or a reload of) a route such as
* /overview or /contributors requests a file that does not exist on disk and Pages
* returns its default "File not found" page instead of the app.
*
* Emitting 404.html as a copy of index.html is the documented workaround: Pages serves
* that file for any unmatched path, the app boots, and React Router resolves the route
* from window.location. The URL is preserved, so there is no redirect and no flash.
*/
function spaDeepLinkFallback() {
let outDir;

return {
name: "spa-deep-link-fallback",
apply: "build",
configResolved(config) {
outDir = resolve(config.root, config.build.outDir);
},
closeBundle() {
const indexHtml = resolve(outDir, "index.html");
if (existsSync(indexHtml)) {
copyFileSync(indexHtml, resolve(outDir, "404.html"));
}
},
};
}

export default defineConfig(({ mode }) => ({
plugins: [react(), tailwindcss(), svgr()],
plugins: [react(), tailwindcss(), svgr(), spaDeepLinkFallback()],
base: "/",
test: {
globals: true,
Expand Down
Loading