forked from PiaJMehta/the-last-broadcast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
54 lines (49 loc) · 2.19 KB
/
Copy pathvite.config.js
File metadata and controls
54 lines (49 loc) · 2.19 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
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import fs from 'fs';
import path from 'path';
export default defineConfig({
appType: 'custom',
plugins: [
{
name: 'custom-routing',
configureServer(server) {
server.middlewares.use((req, res, next) => {
const url = (req.url || '').split('?')[0];
const tryIndex = path.join('public', url, 'index.html');
if (fs.existsSync(tryIndex)) {
req.url = url + '/index.html';
return next();
}
next();
});
return () => {
server.middlewares.use(async (req, res, next) => {
const accept = req.headers.accept || '';
if (req.method !== 'GET' || !accept.includes('text/html')) {
return next();
}
const url = (req.url || '').split('?')[0];
const knownPaths = ['/', '/index.html', '/logs', '/rules', '/help', '/resources', '/signal'];
const isKnown = knownPaths.includes(url)
|| fs.existsSync(path.join('public', url))
|| fs.existsSync(path.join('.', url));
if (isKnown) {
const indexHtml = fs.readFileSync(path.resolve('index.html'), 'utf-8');
const transformed = await server.transformIndexHtml(url, indexHtml);
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end(transformed);
} else {
const notFoundPath = path.resolve('404.html');
res.statusCode = 404;
res.setHeader('Content-Type', 'text/html');
res.end(fs.readFileSync(notFoundPath, 'utf-8'));
}
});
};
},
},
react(),
],
});