fix: Use NODE_ENV to properly determine file serving strategy
All checks were successful
build-and-deploy / filter (push) Successful in 2s
build-and-deploy / build (push) Successful in 12s
build-and-deploy / deploy (push) Successful in 9s

- 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
This commit is contained in:
2025-07-05 16:03:59 -06:00
parent f33cc71bb0
commit 4780197577
2 changed files with 40 additions and 24 deletions

View File

@@ -156,21 +156,29 @@ 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 (prioritize dist for production, AFTER API routes) // Serve static files based on environment (AFTER API routes)
app.use(express.static('dist')); if (ENV === 'production') {
app.use(express.static('.')); // 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) => { app.get('*', (req, res) => {
const distIndexPath = path.join(__dirname, 'dist', 'index.html'); if (ENV === 'production') {
const devIndexPath = path.join(__dirname, 'index.html'); const distIndexPath = path.join(__dirname, 'dist', 'index.html');
const fs = require('fs');
// Try to serve from dist first (production build), fallback to dev if (fs.existsSync(distIndexPath)) {
const fs = require('fs'); res.sendFile(distIndexPath);
if (fs.existsSync(distIndexPath)) { } else {
res.sendFile(distIndexPath); res.status(500).send('Production build not found. Run npm run build first.');
}
} else { } else {
res.sendFile(devIndexPath); // Development: serve dev index.html
res.sendFile(path.join(__dirname, 'index.html'));
} }
}); });

View File

@@ -12,21 +12,29 @@ 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 (prioritize dist for production) // Serve static files based on environment
app.use(express.static('dist')); if (ENV === 'production') {
app.use(express.static('.')); // 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) => { app.get('/', (req, res) => {
const distIndexPath = path.join(__dirname, 'dist', 'index.html'); if (ENV === 'production') {
const devIndexPath = path.join(__dirname, 'index.html'); const distIndexPath = path.join(__dirname, 'dist', 'index.html');
const fs = require('fs');
// Try to serve from dist first (production build), fallback to dev if (fs.existsSync(distIndexPath)) {
const fs = require('fs'); res.sendFile(distIndexPath);
if (fs.existsSync(distIndexPath)) { } else {
res.sendFile(distIndexPath); res.status(500).send('Production build not found. Run npm run build first.');
}
} else { } else {
res.sendFile(devIndexPath); // Development: serve dev index.html
res.sendFile(path.join(__dirname, 'index.html'));
} }
}); });