Skip to content

Commit 31ea99f

Browse files
committed
Fix base path handling for Vite and React Router
Updated vite.config.js to set the base path only in production, ensuring local development uses '/'. Modified index.tsx to dynamically set React Router's basename from Vite's BASE_URL, improving routing when deployed to a subpath. Added a type declaration for serviceWorker to resolve TypeScript import issues.
1 parent 823ee82 commit 31ea99f

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

src/index.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@ import React from 'react';
22
import { createRoot } from 'react-dom/client';
33
import './index.css';
44
import App from './App';
5-
import * as serviceWorker from './serviceWorker';
5+
import * as serviceWorker from './serviceWorker.js';
66
import { BrowserRouter as Router } from 'react-router-dom';
77

88

9-
const container = document.getElementById('root');
9+
const container = document.getElementById('root') as HTMLElement | null;
10+
if (!container) {
11+
throw new Error('Root container with id "root" not found');
12+
}
1013
const root = createRoot(container);
14+
// Use Vite's BASE_URL for correct subpath in production (e.g., /svprogresstracker/)
15+
// React Router expects basename without trailing slash
16+
const basename = (import.meta as any).env?.BASE_URL
17+
? (import.meta as any).env.BASE_URL.replace(/\/$/, '')
18+
: '';
19+
1120
root.render(
12-
<Router>
21+
<Router basename={basename}>
1322
<React.StrictMode>
1423
<App />
1524
</React.StrictMode>

src/types/serviceWorker.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module './serviceWorker' {
2+
export function unregister(): void;
3+
}

vite.config.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ import { defineConfig } from 'vite';
22
import react from '@vitejs/plugin-react';
33
import path from 'path';
44

5-
export default defineConfig({
6-
// Ensure assets resolve correctly when hosted at
7-
// https://thecoderaccoons.github.io/svprogresstracker/
8-
// If you later use a custom domain, update/remove this base.
9-
base: '/svprogresstracker/',
5+
// Use base only in production so local dev runs at '/'
6+
export default defineConfig(({ mode }) => ({
7+
base: mode === 'production' ? '/svprogresstracker/' : '/',
108
plugins: [react()],
119
resolve: {
1210
alias: {
13-
'@': path.resolve(__dirname, 'src'),
14-
'@media': path.resolve(__dirname, 'src/Media'),
15-
'@utility': path.resolve(__dirname, 'src/Components/Utility'),
16-
'@hooks': path.resolve(__dirname, 'src/Hooks'),
11+
'@': path.resolve(__dirname, 'src'),
12+
'@media': path.resolve(__dirname, 'src/Media'),
13+
'@utility': path.resolve(__dirname, 'src/Components/Utility'),
14+
'@hooks': path.resolve(__dirname, 'src/Hooks'),
1715
},
1816
},
1917
build: {
@@ -22,4 +20,4 @@ export default defineConfig({
2220
server: {
2321
open: true,
2422
},
25-
});
23+
}));

0 commit comments

Comments
 (0)