From 9b29124d319fb0fd668a5c7129315325d445c566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Wed, 13 May 2020 17:16:09 +0200 Subject: [PATCH] feat(server): add unload method support for serverMiddleware (#7358) * feat: add unload method support for serverMiddleware * chore: update test Co-authored-by: Pooya Parsa --- packages/server/src/server.js | 10 ++++++++++ packages/server/test/server.test.js | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/server/src/server.js b/packages/server/src/server.js index ccd8cc3057..e330d86510 100644 --- a/packages/server/src/server.js +++ b/packages/server/src/server.js @@ -292,6 +292,9 @@ export default class Server { return } + // unload middleware + this.unloadMiddleware(serverStackItem) + // Resolve middleware const { route, handle } = this.resolveMiddleware(middleware, serverStackItem.route) @@ -305,6 +308,12 @@ export default class Server { return serverStackItem } + unloadMiddleware ({ handle }) { + if (handle._middleware && typeof handle._middleware.unload === 'function') { + handle._middleware.unload() + } + } + serverMiddlewarePaths () { return this.app.stack.map(({ handle }) => handle._middleware && handle._middleware.entry).filter(Boolean) } @@ -369,6 +378,7 @@ export default class Server { await this.renderer.close() } + this.app.stack.forEach(this.unloadMiddleware) this.app.removeAllListeners() this.app = null diff --git a/packages/server/test/server.test.js b/packages/server/test/server.test.js index 798e23f20d..e7403449d4 100644 --- a/packages/server/test/server.test.js +++ b/packages/server/test/server.test.js @@ -504,7 +504,7 @@ describe('server: server', () => { test('should close server', async () => { const removeAllListeners = jest.fn() - connect.mockReturnValueOnce({ use: jest.fn(), removeAllListeners }) + connect.mockReturnValueOnce({ use: jest.fn(), stack: [], removeAllListeners }) const nuxt = createNuxt() const server = new Server(nuxt) const listener = { close: jest.fn() } @@ -525,7 +525,7 @@ describe('server: server', () => { test('should prevent closing server multiple times', async () => { const removeAllListeners = jest.fn() - connect.mockReturnValueOnce({ use: jest.fn(), removeAllListeners }) + connect.mockReturnValueOnce({ use: jest.fn(), stack: [], removeAllListeners }) const nuxt = createNuxt() const server = new Server(nuxt) server.renderer = {}