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) {
|
||||
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 () {
|
||||
|
|
|
@ -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$/, '')
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue