From 1405968d898bc67d366d6d248901f5e83617c34a Mon Sep 17 00:00:00 2001 From: LKHO Date: Tue, 12 Jan 2021 20:17:20 +0800 Subject: [PATCH] fix(server): fix HMR issue with serverMiddleware when `router.base` is set (#8593) --- packages/server/src/server.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/server/src/server.js b/packages/server/src/server.js index 99be2a6805..7553e0b602 100644 --- a/packages/server/src/server.js +++ b/packages/server/src/server.js @@ -199,6 +199,11 @@ export default class Server { middleware = this._requireMiddleware(middleware) } + // #8584 + // shallow clone the middleware before any change is made, + // in case any following mutation breaks when applied repeatedly. + middleware = Object.assign({}, middleware) + // Normalize handler to handle (backward compatibility) if (middleware.handler && !middleware.handle) { middleware.handle = middleware.handler @@ -271,6 +276,10 @@ export default class Server { middleware.route = fallbackRoute } + // #8584 + // save the original route before applying defaults + middleware._originalRoute = middleware.route + // Resolve final route middleware.route = ( (middleware.prefix !== false ? this.options.router.base : '') + @@ -313,7 +322,13 @@ export default class Server { this.unloadMiddleware(serverStackItem) // Resolve middleware - const { route, handle } = this.resolveMiddleware(middleware, serverStackItem.route) + const { route, handle } = this.resolveMiddleware( + middleware, + // #8584 pass the original route as fallback + serverStackItem.handle._middleware + ? serverStackItem.handle._middleware._originalRoute + : serverStackItem.route + ) // Update serverStackItem serverStackItem.handle = handle