Add middleware to handle 404 routes in webpack configuration. Implemented custom response handling to ensure proper 404 status and no-cache headers, along with rewrites for SPA fallback to improve routing behavior.

This commit is contained in:
seb
2025-07-06 23:50:30 +02:00
parent 987de641e4
commit 8698816875

View File

@@ -309,6 +309,36 @@ export default {
next(); next();
}); });
// Add middleware to handle /404 route BEFORE webpack-dev-server processing
middlewares.unshift({
name: 'handle-404-route',
middleware: (req, res, next) => {
if (req.url === '/404') {
// Mark this request as a 404 and intercept the response
res.locals.is404 = true;
// Override writeHead to force 404 status
const originalWriteHead = res.writeHead;
res.writeHead = function(statusCode, statusMessage, headers) {
// Force 404 status and no-cache headers
const newHeaders = {
...headers,
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
};
return originalWriteHead.call(this, 404, statusMessage, newHeaders);
};
// Rewrite the URL to / so historyApiFallback can handle it
req.url = '/';
next();
} else {
next();
}
}
});
return middlewares; return middlewares;
}, },
hot: true, hot: true,
@@ -317,7 +347,18 @@ export default {
historyApiFallback: { historyApiFallback: {
index: '/index.html', index: '/index.html',
disableDotRule: true, disableDotRule: true,
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'] htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
rewrites: [
// Exclude prerendered routes from SPA fallback
{ from: /^\/Kategorie\//, to: function(context) {
return context.parsedUrl.pathname;
}},
{ from: /^\/Artikel\//, to: function(context) {
return context.parsedUrl.pathname;
}},
// All other routes should fallback to React SPA
{ from: /^\/(?!api|socket\.io|assets|js|css|favicon\.ico).*$/, to: '/index.html' }
]
}, },
client: { client: {
logging: 'verbose', logging: 'verbose',