From 1ee169e3186cdf49590aea8229650e953ab97e5a Mon Sep 17 00:00:00 2001 From: JounQin Date: Thu, 8 Jun 2017 19:01:37 +0800 Subject: [PATCH 1/4] 1. response 302 on redirecting in router 2. heartbeat: 5000 for Node.js 8, via https://github.com/glenjamin/webpack-hot-middleware/issues/210#issuecomment-305624051 --- build/setup-dev-server.js | 2 +- server.js | 4 +++- src/entry-client.js | 4 ++-- src/entry-server.js | 19 ++++++++++++------- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/build/setup-dev-server.js b/build/setup-dev-server.js index 53c9fca55..046bc8be0 100644 --- a/build/setup-dev-server.js +++ b/build/setup-dev-server.js @@ -52,7 +52,7 @@ module.exports = function setupDevServer (app, cb) { }) // hot middleware - app.use(require('webpack-hot-middleware')(clientCompiler)) + app.use(require('webpack-hot-middleware')(clientCompiler, {heartbeat: 5000})) // watch and update server renderer const serverCompiler = webpack(serverConfig) diff --git a/server.js b/server.js index 40368dbcd..1b985b5ef 100644 --- a/server.js +++ b/server.js @@ -85,7 +85,9 @@ function render (req, res) { res.setHeader("Server", serverInfo) const handleError = err => { - if (err && err.code === 404) { + if (err.url) { + res.redirect(err.url) + } else if(err.code === 404) { res.status(404).end('404 | Page Not Found') } else { // Render Error Page or Redirect diff --git a/src/entry-client.js b/src/entry-client.js index 23aade21d..f9c05534a 100644 --- a/src/entry-client.js +++ b/src/entry-client.js @@ -63,6 +63,6 @@ router.onReady(() => { }) // service worker -if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { - navigator.serviceWorker.register('/service-worker.js') +if ('https:' === location.protocol && navigator.serviceWorker) { + navigator.serviceWorker.register('/s/ervice-worker.js') } diff --git a/src/entry-server.js b/src/entry-server.js index 2adc7fc9f..8fda2e603 100644 --- a/src/entry-server.js +++ b/src/entry-server.js @@ -12,8 +12,15 @@ export default context => { const s = isDev && Date.now() const { app, router, store } = createApp() + const {url} = context + const fullPath = router.resolve(url).route.fullPath + + if (fullPath !== url) { + reject({ url: fullPath }) + } + // set router's location - router.push(context.url) + router.push(url) // wait until router has resolved possible async hooks router.onReady(() => { @@ -26,12 +33,10 @@ export default context => { // A preFetch hook dispatches a store action and returns a Promise, // which is resolved when the action is complete and store state has been // updated. - Promise.all(matchedComponents.map(component => { - return component.asyncData && component.asyncData({ - store, - route: router.currentRoute - }) - })).then(() => { + Promise.all(matchedComponents.map(({asyncData}) => asyncData && asyncData({ + store, + route: router.currentRoute + }))).then(() => { isDev && console.log(`data pre-fetch: ${Date.now() - s}ms`) // After all preFetch hooks are resolved, our store is now // filled with the state needed to render the app. From d97aaa49939174d3b4c294170ca20e00e477dd5c Mon Sep 17 00:00:00 2001 From: JounQin Date: Thu, 8 Jun 2017 19:44:40 +0800 Subject: [PATCH 2/4] mistake --- src/entry-client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entry-client.js b/src/entry-client.js index f9c05534a..5e730a4ab 100644 --- a/src/entry-client.js +++ b/src/entry-client.js @@ -64,5 +64,5 @@ router.onReady(() => { // service worker if ('https:' === location.protocol && navigator.serviceWorker) { - navigator.serviceWorker.register('/s/ervice-worker.js') + navigator.serviceWorker.register('/service-worker.js') } From 12d6da31f113dcac042bd577292223838543410d Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 9 Jun 2017 09:06:54 +0800 Subject: [PATCH 3/4] Create setup-dev-server.js --- build/setup-dev-server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/setup-dev-server.js b/build/setup-dev-server.js index 046bc8be0..346a14b48 100644 --- a/build/setup-dev-server.js +++ b/build/setup-dev-server.js @@ -52,7 +52,7 @@ module.exports = function setupDevServer (app, cb) { }) // hot middleware - app.use(require('webpack-hot-middleware')(clientCompiler, {heartbeat: 5000})) + app.use(require('webpack-hot-middleware')(clientCompiler, { heartbeat: 5000 })) // watch and update server renderer const serverCompiler = webpack(serverConfig) From e55ef58b696f90e56928dd64114734978a76cc00 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 9 Jun 2017 09:07:20 +0800 Subject: [PATCH 4/4] Create entry-server.js --- src/entry-server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/entry-server.js b/src/entry-server.js index 8fda2e603..f5aa44724 100644 --- a/src/entry-server.js +++ b/src/entry-server.js @@ -12,7 +12,7 @@ export default context => { const s = isDev && Date.now() const { app, router, store } = createApp() - const {url} = context + const { url } = context const fullPath = router.resolve(url).route.fullPath if (fullPath !== url) { @@ -33,7 +33,7 @@ export default context => { // A preFetch hook dispatches a store action and returns a Promise, // which is resolved when the action is complete and store state has been // updated. - Promise.all(matchedComponents.map(({asyncData}) => asyncData && asyncData({ + Promise.all(matchedComponents.map(({ asyncData }) => asyncData && asyncData({ store, route: router.currentRoute }))).then(() => {