diff --git a/lib/Server.js b/lib/Server.js index 5c8632ef34..f9b3429b18 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -552,6 +552,35 @@ class Server { searchParams.set("password", webSocketURL.password); } + // Initialize an array to keep track of applied middleware + const appliedMiddleware = []; + + // Function to apply middleware only once + function applyMiddlewareOnce(middleware) { + // Check if the middleware has already been applied + if (!appliedMiddleware.includes(middleware)) { + // Apply the middleware + this.app.use(middleware); + + // Add the middleware to the applied middleware array + appliedMiddleware.push(middleware); + } + } + + // Apply middleware for each feature only once + if (this.options.proxy) { + applyMiddlewareOnce.call(this, proxyMiddleware); + } + + if (this.options.contentBase !== false) { + applyMiddlewareOnce.call(this, contentBaseMiddleware); + } + + if (this.options.historyApiFallback) { + applyMiddlewareOnce.call(this, historyApiFallbackMiddleware); + } + + /** @type {string} */ let hostname;