Skip to content

Commit 9505ea2

Browse files
authored
fix: properly separate static pages from Vite-built React pages (#8)
- Move React HTML entry points (dashboard, admin, pull_request) to root - Remove old root index.html (Vite shell) - landing page is in public/ - Configure Vite with multi-page rollupOptions.input for React pages only - Set publicDir: false so Vite doesn't interfere with public/ folder - Update server to serve static files from public/, React from dist/ - Fix HTML files to use consistent script/css references Architecture: - public/ contains static pages (landing, imprint, privacy) served directly - Root HTML files are Vite entry points, built to dist/ - In production: static from public/, React pages from dist/ - In development: Vite middleware transforms React pages on the fly
1 parent f1b58be commit 9505ea2

6 files changed

Lines changed: 35 additions & 31 deletions

File tree

File renamed without changes.
File renamed without changes.

public/admin.html

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
name="viewport"
1313
content="width=device-width, initial-scale=1, shrink-to-fit=no"
1414
/>
15-
<link href="/static/css/style.css" rel="stylesheet" />
15+
<link href="/style.css" rel="stylesheet" />
1616
</head>
1717

1818
<body>
1919
<div class="bg"></div>
2020
<div class="container">
2121
<div id="app"></div>
2222
</div>
23-
<script src="/static/js/main.js"></script>
23+
<script type="module" src="/src/main.jsx"></script>
2424
</body>
2525
</html>

server/index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async function startServer() {
2727
});
2828
});
2929

30-
// Proxy API requests to backend - MUST be before Vite middleware
30+
// Proxy API requests to backend - MUST be before static/Vite middleware
3131
app.use('/api', proxy);
3232

3333
// Setup Vite middleware (development only)
@@ -41,10 +41,15 @@ async function startServer() {
4141
app.use(vite.middlewares);
4242
}
4343

44-
// Serve static files
44+
// Serve static files (landing page, images, css)
4545
app.use(express.static('./public'));
4646

47-
// Static pages (no React)
47+
// In production, serve built assets from dist/
48+
if (isProduction) {
49+
app.use(express.static('./dist'));
50+
}
51+
52+
// Static pages (no React) - served from public/
4853
app.get('/', (req, res) => {
4954
res.sendFile('index.html', { root: './public' });
5055
});
@@ -57,15 +62,17 @@ async function startServer() {
5762
res.sendFile('privacyPolicy.html', { root: './public' });
5863
});
5964

60-
// React pages - use Vite in development
65+
// React pages - serve from dist/ in production, transform with Vite in development
6166
const serveReactPage = async (req, res, htmlFile) => {
6267
if (isProduction) {
63-
res.sendFile(htmlFile, { root: './public' });
68+
// Serve pre-built HTML from dist/
69+
res.sendFile(htmlFile, { root: './dist' });
6470
} else {
71+
// Transform HTML with Vite (handles JSX imports)
6572
try {
6673
const template = await vite.transformIndexHtml(
6774
req.originalUrl,
68-
fs.readFileSync(path.join('./public', htmlFile), 'utf-8')
75+
fs.readFileSync(path.join('.', htmlFile), 'utf-8')
6976
);
7077
res.setHeader('Content-Type', 'text/html');
7178
res.send(template);
@@ -80,7 +87,8 @@ async function startServer() {
8087
app.get('/dashboard', (req, res) =>
8188
serveReactPage(req, res, 'dashboard.html')
8289
);
83-
app.get('/admin', (req, res) => serveReactPage(req, res, 'dashboard.html'));
90+
91+
app.get('/admin', (req, res) => serveReactPage(req, res, 'admin.html'));
8492

8593
// PR pages: /:owner/:repo/pull/:number
8694
app.get('/:owner/:repo/pull/:number', (req, res) => {

vite.config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
import { defineConfig } from 'vite';
22
import react from '@vitejs/plugin-react';
3+
import { resolve } from 'path';
34

45
export default defineConfig({
56
plugins: [react()],
7+
8+
// Don't copy public/ to dist/ - we serve static files separately
9+
publicDir: false,
10+
11+
build: {
12+
outDir: 'dist',
13+
emptyOutDir: true,
14+
rollupOptions: {
15+
input: {
16+
dashboard: resolve(__dirname, 'dashboard.html'),
17+
pullRequest: resolve(__dirname, 'pull_request.html'),
18+
admin: resolve(__dirname, 'admin.html'),
19+
},
20+
},
21+
},
22+
623
server: {
724
host: '0.0.0.0',
825
port: 3001,
926
},
27+
1028
css: {
1129
modules: {
1230
localsConvention: 'camelCaseOnly',

0 commit comments

Comments
 (0)