From 478019757759cbc0a2e68fe15b148eb06faaaa78 Mon Sep 17 00:00:00 2001 From: josedario87 Date: Sat, 5 Jul 2025 16:03:59 -0600 Subject: [PATCH] fix: Use NODE_ENV to properly determine file serving strategy - Production (NODE_ENV=production): serves dist/ directory first - Development: serves current directory only, ignores dist/ - Prevents conflicts when dist/ exists in development - Clear error message if production build not found --- admin/server.js | 32 ++++++++++++++++++++------------ client/server.js | 32 ++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/admin/server.js b/admin/server.js index 5677969..c50ea8a 100644 --- a/admin/server.js +++ b/admin/server.js @@ -156,21 +156,29 @@ 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 (prioritize dist for production, AFTER API routes) -app.use(express.static('dist')); -app.use(express.static('.')); +// Serve static files based on environment (AFTER API routes) +if (ENV === 'production') { + // Production: serve from dist first + app.use(express.static('dist')); + app.use(express.static('.')); +} else { + // Development: serve from current directory only + app.use(express.static('.')); +} -// Serve main HTML file for SPA routes (prioritize dist for production) +// Serve main HTML file for SPA routes based on environment app.get('*', (req, res) => { - 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); + if (ENV === 'production') { + const distIndexPath = path.join(__dirname, 'dist', 'index.html'); + const fs = require('fs'); + if (fs.existsSync(distIndexPath)) { + res.sendFile(distIndexPath); + } else { + res.status(500).send('Production build not found. Run npm run build first.'); + } } else { - res.sendFile(devIndexPath); + // Development: serve dev index.html + res.sendFile(path.join(__dirname, 'index.html')); } }); diff --git a/client/server.js b/client/server.js index 43d64f5..c0c5576 100644 --- a/client/server.js +++ b/client/server.js @@ -12,21 +12,29 @@ const PORT = process.env.PORT || 3000; // Configure MIME types for modules express.static.mime.define({'application/javascript': ['js', 'mjs']}); -// Serve static files (prioritize dist for production) -app.use(express.static('dist')); -app.use(express.static('.')); +// Serve static files based on environment +if (ENV === 'production') { + // Production: serve from dist first + app.use(express.static('dist')); + app.use(express.static('.')); +} else { + // Development: serve from current directory only + app.use(express.static('.')); +} -// Serve main HTML file (prioritize dist for production) +// Serve main HTML file based on environment app.get('/', (req, res) => { - 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); + if (ENV === 'production') { + const distIndexPath = path.join(__dirname, 'dist', 'index.html'); + const fs = require('fs'); + if (fs.existsSync(distIndexPath)) { + res.sendFile(distIndexPath); + } else { + res.status(500).send('Production build not found. Run npm run build first.'); + } } else { - res.sendFile(devIndexPath); + // Development: serve dev index.html + res.sendFile(path.join(__dirname, 'index.html')); } });