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
JounQin authored and yyx990803 committed Jun 9, 2017
1 parent 5b9d7de commit 980ca32
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) {

This comment has been minimized.

Copy link
@Everettss

Everettss Dec 3, 2017

@JounQin How to now use service worker on localhost? Using Build Setup from this repo is not enough. Setting up localhost server with https introduce unnecessary complexity.

This comment has been minimized.

Copy link
@JounQin

JounQin Dec 4, 2017

Author Contributor

It seems browser will ignore localhost or 127.0.0.1 without https, so maybe we can add some check for that.

I add this code because we are only using service worker on production here, which will not only run on localhost.

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) {

This comment has been minimized.

Copy link
@remoe

remoe Jun 13, 2017

when you define a redirect on the router, then fullPath is not url and it would rejected by this code. Correct? When yes, this code is wrong?

This comment has been minimized.

Copy link
@JounQin

JounQin Jun 14, 2017

Author Contributor

Sorry, but I can't get your point. What's wrong?

This comment has been minimized.

Copy link
@remoe

remoe Jun 14, 2017

Sorry, i've tested this on similar environment, but not exactly on this project.. It works on this :)

This comment has been minimized.

Copy link
@dohkoxiaozu

dohkoxiaozu May 7, 2020

@JounQin @yyx990803 有一种场景会因为这个判断而无法访问页面,就是当 url = /routerName? ,多了个问号。
是否是因为严谨性而加的判断,认为有 ? 通配符就应该要有键值对吗?

This comment has been minimized.

Copy link
@JounQin

JounQin May 7, 2020

Author Contributor

@dohkoxiaozu 这个判断只会进行 302 重定向,url = /routerName? 会被重定向到 url = /routerName,为什么会『无法访问页面』?

This comment has been minimized.

Copy link
@dohkoxiaozu

dohkoxiaozu May 8, 2020

无法访问页面是我改过判断里面的reject,那上面说的那种场景重定向一次好像也没必要?很纠结这个点

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 980ca32

Please sign in to comment.