Add debug logging for frontend dist and SPA fallback
This commit is contained in:
@@ -132,7 +132,24 @@ app.use(express.static('src/public'));
|
||||
const path = require('path');
|
||||
const frontendDistPath = path.join(__dirname, '../../frontend/dist');
|
||||
const fs = require('fs');
|
||||
|
||||
if (fs.existsSync(frontendDistPath)) {
|
||||
logger.info(`Frontend dist found at: ${frontendDistPath}`);
|
||||
|
||||
// List files for debugging
|
||||
try {
|
||||
const files = fs.readdirSync(frontendDistPath);
|
||||
logger.info(`Frontend dist files: ${files.join(', ')}`);
|
||||
|
||||
const assetsPath = path.join(frontendDistPath, 'assets');
|
||||
if (fs.existsSync(assetsPath)) {
|
||||
const assetFiles = fs.readdirSync(assetsPath);
|
||||
logger.info(`Frontend assets: ${assetFiles.length} files`);
|
||||
}
|
||||
} catch (err) {
|
||||
logger.warn(`Could not read frontend dist: ${err.message}`);
|
||||
}
|
||||
|
||||
// Serve static files with proper headers for SPA
|
||||
// Use middleware to set headers with access to request object
|
||||
app.use((req, res, next) => {
|
||||
@@ -160,6 +177,8 @@ if (fs.existsSync(frontendDistPath)) {
|
||||
res.setHeader('X-Content-Type-Options', 'nosniff');
|
||||
},
|
||||
}));
|
||||
} else {
|
||||
logger.warn(`Frontend dist NOT found at: ${frontendDistPath}`);
|
||||
}
|
||||
|
||||
// Session middleware
|
||||
@@ -200,6 +219,9 @@ app.use('/t', require('./routes/tracking.routes'));
|
||||
|
||||
// SPA fallback: serve index.html for all non-API routes (must be after all routes)
|
||||
if (fs.existsSync(frontendDistPath)) {
|
||||
const indexHtmlPath = path.join(frontendDistPath, 'index.html');
|
||||
logger.info(`Frontend SPA fallback enabled: ${indexHtmlPath}`);
|
||||
|
||||
app.get('*', (req, res, next) => {
|
||||
// Skip API routes and tracking routes
|
||||
if (req.path.startsWith('/api') || req.path.startsWith('/t/') || req.path.startsWith('/health')) {
|
||||
@@ -208,10 +230,31 @@ if (fs.existsSync(frontendDistPath)) {
|
||||
error: 'Endpoint not found',
|
||||
});
|
||||
}
|
||||
|
||||
// Log SPA fallback
|
||||
logger.info(`SPA fallback: serving index.html for ${req.path}`);
|
||||
|
||||
// Serve frontend SPA
|
||||
res.sendFile(path.join(frontendDistPath, 'index.html'));
|
||||
if (fs.existsSync(indexHtmlPath)) {
|
||||
res.sendFile(indexHtmlPath, (err) => {
|
||||
if (err) {
|
||||
logger.error(`Failed to send index.html: ${err.message}`);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: 'Failed to load frontend',
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
logger.error(`index.html not found at: ${indexHtmlPath}`);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: 'Frontend not found',
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
logger.warn(`Frontend dist not found at: ${frontendDistPath}`);
|
||||
// 404 handler (if frontend not built)
|
||||
app.use((req, res) => {
|
||||
res.status(404).json({
|
||||
|
||||
Reference in New Issue
Block a user