From df9b32a1c40f2dd18112f3646b111711339467e0 Mon Sep 17 00:00:00 2001 From: "Xin Du (Clark)" Date: Mon, 24 Dec 2018 16:26:39 +0000 Subject: [PATCH] fix: hmr in modern mode (#4623) --- packages/server/src/middleware/modern.js | 24 ++++++++++++++++-------- packages/server/src/server.js | 2 +- packages/vue-renderer/src/renderer.js | 1 + packages/webpack/src/builder.js | 2 +- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/server/src/middleware/modern.js b/packages/server/src/middleware/modern.js index 93bfca5dfa..6b6f41cf54 100644 --- a/packages/server/src/middleware/modern.js +++ b/packages/server/src/middleware/modern.js @@ -34,19 +34,27 @@ const detectModernBuild = ({ options, resources }) => { } } -const detectModernBrowser = (req, options) => { +const detectModernBrowser = ({ socket = {}, headers }) => { + if (socket.isModernBrowser === undefined) { + const ua = headers && headers['user-agent'] + socket.isModernBrowser = isModernBrowser(ua) + } +} + +const setModernMode = (req, options) => { + const { socket = {} } = req + const isModernBrowser = socket.isModernBrowser if (options.modern === 'server') { - const { socket = {}, headers } = req - if (socket.modernMode === undefined) { - const ua = headers && headers['user-agent'] - socket.modernMode = isModernBrowser(ua) - } - req.modernMode = socket.modernMode + req.modernMode = isModernBrowser + } + if (options.dev) { + req.devModernMode = isModernBrowser } } export default ({ context }) => (req, res, next) => { detectModernBuild(context) - detectModernBrowser(req, context.options) + detectModernBrowser(req) + setModernMode(req, context.options) next() } diff --git a/packages/server/src/server.js b/packages/server/src/server.js index 7c894040c5..6e552767f3 100644 --- a/packages/server/src/server.js +++ b/packages/server/src/server.js @@ -83,7 +83,7 @@ export default class Server { if (this.options.dev) { this.useMiddleware(modernMiddleware) this.useMiddleware(async (req, res, next) => { - const name = req.modernMode ? 'modern' : 'client' + const name = req.devModernMode ? 'modern' : 'client' if (this.devMiddleware && this.devMiddleware[name]) { await this.devMiddleware[name](req, res) } diff --git a/packages/vue-renderer/src/renderer.js b/packages/vue-renderer/src/renderer.js index 6d4bce59f1..5b5c96877e 100644 --- a/packages/vue-renderer/src/renderer.js +++ b/packages/vue-renderer/src/renderer.js @@ -150,6 +150,7 @@ export default class VueRenderer { if (isMFS) { // Cleanup MFS as soon as possible to save memory _fs.unlinkSync(fullPath) + delete this._assetsMapping } return contents } catch (err) { diff --git a/packages/webpack/src/builder.js b/packages/webpack/src/builder.js index d296da8afd..f24d6d8cfc 100644 --- a/packages/webpack/src/builder.js +++ b/packages/webpack/src/builder.js @@ -180,7 +180,7 @@ export class WebpackBundler { webpackDev(compiler) { consola.debug('Adding webpack middleware...') - const name = [compiler.options.name] + const name = compiler.options.name const { nuxt: { server }, options } = this.context // Create webpack dev middleware