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
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'));
}
});

View File

@@ -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'));
}
});