From f33cc71bb05198c8e2c9cfbdc2f2587d80d3ab16 Mon Sep 17 00:00:00 2001 From: josedario87 Date: Sat, 5 Jul 2025 15:59:37 -0600 Subject: [PATCH] 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 --- admin/server.js | 17 +++++++++++++---- client/server.js | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/admin/server.js b/admin/server.js index 26dd8e5..5677969 100644 --- a/admin/server.js +++ b/admin/server.js @@ -156,13 +156,22 @@ app.post('/api/admin/cancel-game', async (req, res) => { // Configure MIME types for modules express.static.mime.define({'application/javascript': ['js', 'mjs']}); -// Serve static files from current directory and dist (AFTER API routes) -app.use(express.static('.')); +// Serve static files (prioritize dist for production, AFTER API routes) 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) => { - 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, () => { diff --git a/client/server.js b/client/server.js index 62b7688..43d64f5 100644 --- a/client/server.js +++ b/client/server.js @@ -12,13 +12,22 @@ const PORT = process.env.PORT || 3000; // Configure MIME types for modules express.static.mime.define({'application/javascript': ['js', 'mjs']}); -// Serve static files from current directory and dist -app.use(express.static('.')); +// Serve static files (prioritize dist for production) 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) => { - 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