From 1ca5739a2f58857cecf4f7ce96f1603a0f0b51ae Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 25 Aug 2017 16:31:16 +0430 Subject: [PATCH] add router base to all middleware --- lib/builder/builder.js | 2 +- lib/builder/webpack/client.config.js | 6 +++++- lib/core/renderer.js | 12 +++--------- test/with-config.test.js | 9 +++++++-- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/builder/builder.js b/lib/builder/builder.js index ed037e95ef..e8a01c9972 100644 --- a/lib/builder/builder.js +++ b/lib/builder/builder.js @@ -518,7 +518,7 @@ export default class Builder extends Tapable { this.webpackHotMiddleware = pify(webpackHotMiddleware(this.compiler.client, Object.assign({ log: false, - heartbeat: 2500 + heartbeat: 1000 }, this.options.build.hotMiddleware))) // Inject to renderer instance diff --git a/lib/builder/webpack/client.config.js b/lib/builder/webpack/client.config.js index 76888ac38c..bef3a7b08c 100644 --- a/lib/builder/webpack/client.config.js +++ b/lib/builder/webpack/client.config.js @@ -151,7 +151,11 @@ export default function webpackClientConfig () { config.plugins.push(new webpack.NamedModulesPlugin()) // Add HMR support - config.entry.app = ['webpack-hot-middleware/client?name=client&reload=true', config.entry.app] + config.entry.app = [ + // https://github.com/glenjamin/webpack-hot-middleware#config + `webpack-hot-middleware/client?name=client&reload=true&timeout=3000&path=${this.options.router.base}/__webpack_hmr`.replace(/\/\//g, '/'), + config.entry.app + ] config.plugins.push( new webpack.HotModuleReplacementPlugin(), new webpack.NoEmitOnErrorsPlugin() diff --git a/lib/core/renderer.js b/lib/core/renderer.js index a2e1595eb2..6a15f61ffd 100644 --- a/lib/core/renderer.js +++ b/lib/core/renderer.js @@ -179,11 +179,9 @@ export default class Renderer extends Tapable { m.handler = require(this.nuxt.resolvePath(m.handler)) } // Use middleware - if (m instanceof Function) { - this.app.use(m) - } else if (m && m.path && m.handler) { - this.app.use(m.path, m.handler) - } + const handler = m.handler || m + const path = (this.options.router.base + (m.path ? m.path : '')).replace(/\/\//g, '/') + this.app.use(path, handler) } async setupMiddleware () { @@ -197,10 +195,6 @@ export default class Renderer extends Tapable { // Common URL checks this.useMiddleware((req, res, next) => { - // If base in req.url, remove it for the middleware and vue-router - if (this.options.router.base !== '/' && req.url.indexOf(this.options.router.base) === 0) { - req.url = req.url.replace(this.options.router.base, '/') - } // Prevent access to SSR resources if (ssrResourceRegex.test(req.url)) { res.statusCode = 404 diff --git a/test/with-config.test.js b/test/with-config.test.js index 7f84cf9040..2dacdca53f 100644 --- a/test/with-config.test.js +++ b/test/with-config.test.js @@ -90,11 +90,16 @@ test('Check stats.json generated by build.analyze', t => { t.is(stats.assets.length, 28) }) -test('Check /test.txt with custom serve-static options', async t => { - const { headers } = await rp(url('/test.txt'), { resolveWithFullResponse: true }) +test('Check /test/test.txt with custom serve-static options', async t => { + const { headers } = await rp(url('/test/test.txt'), { resolveWithFullResponse: true }) t.is(headers['cache-control'], 'public, max-age=31536000') }) +test('Check /test.txt should return 404', async t => { + const err = await t.throws(rp(url('/test.txt'))) + t.is(err.response.statusCode, 404) +}) + // Close server and ask nuxt to stop listening to file changes test.after('Closing server and nuxt.js', t => { nuxt.close()