1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

improve hot-reloading

This commit is contained in:
nkzawa 2016-10-17 23:35:31 +09:00
parent 60aa5869e4
commit 3c44137b9b
2 changed files with 15 additions and 14 deletions

View file

@ -49,22 +49,14 @@ export default class Router {
}) })
} }
async update (route, data) { update (route, Component) {
data.Component = evalScript(data.component).default const data = this.components[route] || {}
delete data.component const newData = { ...data, Component }
this.components[route] = data this.components[route] = newData
if (route === this.route) { if (route === this.route) {
let props this.notify(newData)
try {
props = await this.getInitialProps(data.Component, {})
} catch (err) {
if (err.cancelled) return false
throw err
}
this.notify({ ...data, props })
} }
return true
} }
back () { back () {

View file

@ -1,14 +1,23 @@
import { resolve, relative } from 'path'
module.exports = function (content) { module.exports = function (content) {
this.cacheable() this.cacheable()
const route = getRoute(this)
return content + ` return content + `
if (module.hot) { if (module.hot) {
module.hot.accept() module.hot.accept()
if ('idle' !== module.hot.status()) { if ('idle' !== module.hot.status()) {
const Component = module.exports.default || module.exports const Component = module.exports.default || module.exports
next.router.notify({ Component }) next.router.update('${route}', Component)
} }
} }
` `
} }
function getRoute (loaderContext) {
const pagesDir = resolve(loaderContext.options.context, 'pages')
const path = loaderContext.resourcePath
return '/' + relative(pagesDir, path).replace(/((^|\/)index)?\.js$/, '')
}