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:
parent
60aa5869e4
commit
3c44137b9b
|
@ -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 () {
|
||||||
|
|
|
@ -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$/, '')
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue