-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
59 lines (51 loc) · 1.45 KB
/
Copy pathvite.config.ts
File metadata and controls
59 lines (51 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { defineConfig, type Plugin } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import { apiPlugin } from './server/vite-plugin.ts'
import { isApiPath } from './server/lib/api-paths.ts'
function spaFallback(): Plugin {
return {
name: 'spa-fallback',
configureServer(server) {
server.middlewares.use((req, _res, next) => {
const url = req.url?.split('?')[0] ?? ''
const accept = req.headers.accept ?? ''
const isAsset =
url.includes('.') || url.startsWith('/@') || url.startsWith('/__')
if (
req.method === 'GET' &&
accept.includes('text/html') &&
!isApiPath(url) &&
!isAsset
) {
req.url = '/index.html'
}
next()
})
},
configurePreviewServer(server) {
server.middlewares.use((req, _res, next) => {
const url = req.url?.split('?')[0] ?? ''
const accept = req.headers.accept ?? ''
const isAsset =
url.includes('.') || url.startsWith('/@') || url.startsWith('/__')
if (
req.method === 'GET' &&
accept.includes('text/html') &&
!isApiPath(url) &&
!isAsset
) {
req.url = '/index.html'
}
next()
})
},
}
}
export default defineConfig({
appType: 'spa',
plugins: [react(), tailwindcss(), apiPlugin(), spaFallback()],
server: {
port: 5173,
},
})