fix: Prioritize dist directory for production builds and correct MIME types
All checks were successful
build-and-deploy / filter (push) Successful in 2s
build-and-deploy / deploy (push) Successful in 9s
build-and-deploy / build (push) Successful in 11s

- 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:
2025-07-05 15:59:37 -06:00
parent e3d6195beb
commit f33cc71bb0
2 changed files with 26 additions and 8 deletions

View File

@@ -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, () => {

View File

@@ -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