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) {
data.Component = evalScript(data.component).default
delete data.component
this.components[route] = data
update (route, Component) {
const data = this.components[route] || {}
const newData = { ...data, Component }
this.components[route] = newData
if (route === this.route) {
let props
try {
props = await this.getInitialProps(data.Component, {})
} catch (err) {
if (err.cancelled) return false
throw err
}
this.notify({ ...data, props })
this.notify(newData)
}
return true
}
back () {

View file

@ -1,14 +1,23 @@
import { resolve, relative } from 'path'
module.exports = function (content) {
this.cacheable()
const route = getRoute(this)
return content + `
if (module.hot) {
module.hot.accept()
if ('idle' !== module.hot.status()) {
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$/, '')
}