From 1ca5739a2f58857cecf4f7ce96f1603a0f0b51ae Mon Sep 17 00:00:00 2001
From: Pooya Parsa <pooya@pi0.ir>
Date: Fri, 25 Aug 2017 16:31:16 +0430
Subject: [PATCH 1/5] 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()

From f933dd1f32f36b488230a03d500de4d8282e2f3f Mon Sep 17 00:00:00 2001
From: Pooya Parsa <pooya@pi0.ir>
Date: Fri, 25 Aug 2017 17:37:45 +0430
Subject: [PATCH 2/5] improve useMiddleware

---
 lib/core/renderer.js | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/lib/core/renderer.js b/lib/core/renderer.js
index 6a15f61ffd..4d103cb9cf 100644
--- a/lib/core/renderer.js
+++ b/lib/core/renderer.js
@@ -171,16 +171,25 @@ export default class Renderer extends Tapable {
 
   useMiddleware (m) {
     // Resolve
+    const $m = m
+    let src
     if (typeof m === 'string') {
-      m = require(this.nuxt.resolvePath(m))
+      src = this.nuxt.resolvePath(m)
+      m = require(src)
     }
-    // Handler
-    if (m && typeof m.handler === 'string') {
-      m.handler = require(this.nuxt.resolvePath(m.handler))
+    if (typeof m.handler === 'string') {
+      src = this.nuxt.resolvePath(m.handler)
+      m.handler = require(src)
     }
-    // Use middleware
+
     const handler = m.handler || m
     const path = (this.options.router.base + (m.path ? m.path : '')).replace(/\/\//g, '/')
+
+    // Inject $src and $m to final handler
+    if (src) handler.$src = src
+    handler.$m = $m
+
+    // Use middleware
     this.app.use(path, handler)
   }
 

From 192c25e3e8061284cb6f09c2c0a18fbac092edb2 Mon Sep 17 00:00:00 2001
From: Pooya Parsa <pooya@pi0.ir>
Date: Fri, 25 Aug 2017 17:41:13 +0430
Subject: [PATCH 3/5] fix tests

---
 lib/common/utils.js | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/common/utils.js b/lib/common/utils.js
index bd98117dca..ad0b90bef2 100644
--- a/lib/common/utils.js
+++ b/lib/common/utils.js
@@ -96,7 +96,7 @@ export function isPureObject (o) {
 
 export const isWindows = /^win/.test(process.platform)
 
-export function wp (p) {
+export function wp (p = '') {
   /* istanbul ignore if */
   if (isWindows) {
     return p.replace(/\\/g, '\\\\')
@@ -104,7 +104,7 @@ export function wp (p) {
   return p
 }
 
-export function wChunk (p) {
+export function wChunk (p = '') {
   /* istanbul ignore if */
   if (isWindows) {
     return p.replace(/\//g, '\\\\')

From 02f9454b660d526958e71768b1c51face88b75e3 Mon Sep 17 00:00:00 2001
From: patmood <patrick.n.moody@gmail.com>
Date: Sat, 26 Aug 2017 18:16:30 -0700
Subject: [PATCH 4/5] rename progress class to prevent conflicts

---
 lib/app/components/nuxt-loading.vue | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/app/components/nuxt-loading.vue b/lib/app/components/nuxt-loading.vue
index f7d0e91cae..6ccc666b59 100644
--- a/lib/app/components/nuxt-loading.vue
+++ b/lib/app/components/nuxt-loading.vue
@@ -1,5 +1,5 @@
 <template>
-  <div class="progress" :style="{
+  <div class="nuxt-progress" :style="{
     'width': percent+'%',
     'height': height,
     'background-color': canSuccess? color : failedColor,
@@ -88,7 +88,7 @@ export default {
 </script>
 
 <style scoped>
-.progress {
+.nuxt-progress {
   position: fixed;
   top: 0px;
   left: 0px;

From 971095af56e716390ca10698f86f26a283476bf8 Mon Sep 17 00:00:00 2001
From: patmood <patrick.n.moody@gmail.com>
Date: Sun, 27 Aug 2017 10:52:27 -0700
Subject: [PATCH 5/5] removed scoped css

---
 lib/app/components/nuxt-loading.vue | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/app/components/nuxt-loading.vue b/lib/app/components/nuxt-loading.vue
index 6ccc666b59..ed6426e51e 100644
--- a/lib/app/components/nuxt-loading.vue
+++ b/lib/app/components/nuxt-loading.vue
@@ -87,7 +87,7 @@ export default {
 }
 </script>
 
-<style scoped>
+<style>
 .nuxt-progress {
   position: fixed;
   top: 0px;