chore: serverMiddleware HMR improvements (#6896)

* chore(server): fallback route for resolveMiddleware

* chore(server): support express HMR

[release]
This commit is contained in:
Pooya Parsa 2020-01-20 19:28:52 +01:00 committed by GitHub
parent a375a6c305
commit a84f31d2a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -206,6 +206,12 @@ export default class Server {
}
}
// SubApp (Express)
if (typeof middleware.handle.handle === 'function') {
const server = middleware.handle
middleware.handle = server.handle.bind(server)
}
return middleware
}
@ -222,10 +228,7 @@ export default class Server {
consola.error('ServerMiddleware Error:', error)
// Placeholder for error
middleware = {
route: '#error',
handle: (req, res, next) => { next(error) }
}
middleware = (req, res, next) => { next(error) }
}
// Normalize
@ -237,10 +240,15 @@ export default class Server {
return middleware
}
resolveMiddleware (middleware) {
resolveMiddleware (middleware, fallbackRoute = '/') {
// Ensure middleware is normalized
middleware = this._normalizeMiddleware(middleware)
// Fallback route
if (!middleware.route) {
middleware.route = fallbackRoute
}
// Resolve final route
middleware.route = (
(middleware.prefix !== false ? this.options.router.base : '') +
@ -255,7 +263,7 @@ export default class Server {
useMiddleware (middleware) {
const { route, handle } = this.resolveMiddleware(middleware)
this.app.use(route.includes('#error') ? '/' : route, handle)
this.app.use(route, handle)
}
replaceMiddleware (query, middleware) {
@ -275,7 +283,7 @@ export default class Server {
}
// Resolve middleware
const { route, handle } = this.resolveMiddleware(middleware)
const { route, handle } = this.resolveMiddleware(middleware, serverStackItem.route)
// Update serverStackItem
serverStackItem.handle = handle