Skip to content

Commit 980ca32

Browse files
JounQinyyx990803
authored andcommitted
enable redirecting on server, fix #203 (#205)
* 1. response 302 on redirecting in router 2. heartbeat: 5000 for Node.js 8, via webpack-contrib/webpack-hot-middleware#210 (comment) * mistake * Create setup-dev-server.js * Create entry-server.js
1 parent 5b9d7de commit 980ca32

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

build/setup-dev-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module.exports = function setupDevServer (app, cb) {
5252
})
5353

5454
// hot middleware
55-
app.use(require('webpack-hot-middleware')(clientCompiler))
55+
app.use(require('webpack-hot-middleware')(clientCompiler, { heartbeat: 5000 }))
5656

5757
// watch and update server renderer
5858
const serverCompiler = webpack(serverConfig)

server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ function render (req, res) {
8585
res.setHeader("Server", serverInfo)
8686

8787
const handleError = err => {
88-
if (err && err.code === 404) {
88+
if (err.url) {
89+
res.redirect(err.url)
90+
} else if(err.code === 404) {
8991
res.status(404).end('404 | Page Not Found')
9092
} else {
9193
// Render Error Page or Redirect

src/entry-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ router.onReady(() => {
6363
})
6464

6565
// service worker
66-
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
66+
if ('https:' === location.protocol && navigator.serviceWorker) {
6767
navigator.serviceWorker.register('/service-worker.js')
6868
}

src/entry-server.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ export default context => {
1212
const s = isDev && Date.now()
1313
const { app, router, store } = createApp()
1414

15+
const { url } = context
16+
const fullPath = router.resolve(url).route.fullPath
17+
18+
if (fullPath !== url) {
19+
reject({ url: fullPath })
20+
}
21+
1522
// set router's location
16-
router.push(context.url)
23+
router.push(url)
1724

1825
// wait until router has resolved possible async hooks
1926
router.onReady(() => {
@@ -26,12 +33,10 @@ export default context => {
2633
// A preFetch hook dispatches a store action and returns a Promise,
2734
// which is resolved when the action is complete and store state has been
2835
// updated.
29-
Promise.all(matchedComponents.map(component => {
30-
return component.asyncData && component.asyncData({
31-
store,
32-
route: router.currentRoute
33-
})
34-
})).then(() => {
36+
Promise.all(matchedComponents.map(({ asyncData }) => asyncData && asyncData({
37+
store,
38+
route: router.currentRoute
39+
}))).then(() => {
3540
isDev && console.log(`data pre-fetch: ${Date.now() - s}ms`)
3641
// After all preFetch hooks are resolved, our store is now
3742
// filled with the state needed to render the app.

0 commit comments

Comments
 (0)