Skip to content

Commit

Permalink
enable redirecting on server, fix #203 (#205)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alejandro3899 committed Jun 9, 2017
1 parent 9e4eb26 commit 95ae21e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build/setup-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/entry-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ router.onReady(() => {
})

// service worker
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
if ('https:' === location.protocol && navigator.serviceWorker) {
navigator.serviceWorker.register('/service-worker.js')
}
19 changes: 12 additions & 7 deletions src/entry-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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.
Expand Down

0 comments on commit 95ae21e

Please sign in to comment.