Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable redirecting on server, fix #203 #205

Merged
merged 4 commits into from
Jun 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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