fix: Prioritize dist directory for production builds and correct MIME types
- Serve dist/index.html first for production builds - Reorder static middleware to prioritize dist over dev files - Add proper fallback to dev files if dist doesn't exist - Should resolve JavaScript module MIME type errors
This commit is contained in:
@@ -156,13 +156,22 @@ app.post('/api/admin/cancel-game', async (req, res) => {
|
|||||||
// Configure MIME types for modules
|
// Configure MIME types for modules
|
||||||
express.static.mime.define({'application/javascript': ['js', 'mjs']});
|
express.static.mime.define({'application/javascript': ['js', 'mjs']});
|
||||||
|
|
||||||
// Serve static files from current directory and dist (AFTER API routes)
|
// Serve static files (prioritize dist for production, AFTER API routes)
|
||||||
app.use(express.static('.'));
|
|
||||||
app.use(express.static('dist'));
|
app.use(express.static('dist'));
|
||||||
|
app.use(express.static('.'));
|
||||||
|
|
||||||
// Serve main HTML file for SPA routes
|
// Serve main HTML file for SPA routes (prioritize dist for production)
|
||||||
app.get('*', (req, res) => {
|
app.get('*', (req, res) => {
|
||||||
res.sendFile(path.join(__dirname, 'index.html'));
|
const distIndexPath = path.join(__dirname, 'dist', 'index.html');
|
||||||
|
const devIndexPath = path.join(__dirname, 'index.html');
|
||||||
|
|
||||||
|
// Try to serve from dist first (production build), fallback to dev
|
||||||
|
const fs = require('fs');
|
||||||
|
if (fs.existsSync(distIndexPath)) {
|
||||||
|
res.sendFile(distIndexPath);
|
||||||
|
} else {
|
||||||
|
res.sendFile(devIndexPath);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
|
|||||||
@@ -12,13 +12,22 @@ const PORT = process.env.PORT || 3000;
|
|||||||
// Configure MIME types for modules
|
// Configure MIME types for modules
|
||||||
express.static.mime.define({'application/javascript': ['js', 'mjs']});
|
express.static.mime.define({'application/javascript': ['js', 'mjs']});
|
||||||
|
|
||||||
// Serve static files from current directory and dist
|
// Serve static files (prioritize dist for production)
|
||||||
app.use(express.static('.'));
|
|
||||||
app.use(express.static('dist'));
|
app.use(express.static('dist'));
|
||||||
|
app.use(express.static('.'));
|
||||||
|
|
||||||
// Serve main HTML file
|
// Serve main HTML file (prioritize dist for production)
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
res.sendFile(path.join(__dirname, 'index.html'));
|
const distIndexPath = path.join(__dirname, 'dist', 'index.html');
|
||||||
|
const devIndexPath = path.join(__dirname, 'index.html');
|
||||||
|
|
||||||
|
// Try to serve from dist first (production build), fallback to dev
|
||||||
|
const fs = require('fs');
|
||||||
|
if (fs.existsSync(distIndexPath)) {
|
||||||
|
res.sendFile(distIndexPath);
|
||||||
|
} else {
|
||||||
|
res.sendFile(devIndexPath);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Health check endpoint
|
// Health check endpoint
|
||||||
|
|||||||
Reference in New Issue
Block a user