mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
feat(server): add unload method support for serverMiddleware (#7358)
* feat: add unload method support for serverMiddleware * chore: update test Co-authored-by: Pooya Parsa <pyapar@gmail.com>
This commit is contained in:
parent
2e5184688f
commit
9b29124d31
@ -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
|
||||
|
||||
|
@ -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 = {}
|
||||
|
Loading…
Reference in New Issue
Block a user