Fix: Improve static file serving headers for CORS and content types
This commit is contained in:
@@ -134,19 +134,30 @@ const frontendDistPath = path.join(__dirname, '../../frontend/dist');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
if (fs.existsSync(frontendDistPath)) {
|
if (fs.existsSync(frontendDistPath)) {
|
||||||
// Serve static files with proper headers for SPA
|
// Serve static files with proper headers for SPA
|
||||||
|
// Use middleware to set headers with access to request object
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
// Set CORS headers for assets if needed
|
||||||
|
const origin = req.headers.origin;
|
||||||
|
if (origin) {
|
||||||
|
res.setHeader('Access-Control-Allow-Origin', origin);
|
||||||
|
res.setHeader('Access-Control-Allow-Credentials', 'true');
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
app.use(express.static(frontendDistPath, {
|
app.use(express.static(frontendDistPath, {
|
||||||
maxAge: '1y', // Cache static assets
|
maxAge: '1y', // Cache static assets
|
||||||
etag: true,
|
etag: true,
|
||||||
lastModified: true,
|
lastModified: true,
|
||||||
setHeaders: (res, path) => {
|
setHeaders: (res, filePath) => {
|
||||||
// Set proper content type for JS/CSS files
|
// Set proper content type for JS/CSS files
|
||||||
if (path.endsWith('.js')) {
|
if (filePath.endsWith('.js')) {
|
||||||
res.setHeader('Content-Type', 'application/javascript');
|
res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
|
||||||
} else if (path.endsWith('.css')) {
|
} else if (filePath.endsWith('.css')) {
|
||||||
res.setHeader('Content-Type', 'text/css');
|
res.setHeader('Content-Type', 'text/css; charset=utf-8');
|
||||||
}
|
}
|
||||||
// Allow CORS for assets (if needed)
|
// Security headers for assets
|
||||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
res.setHeader('X-Content-Type-Options', 'nosniff');
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user