Skip to content

Commit 3c44137

Browse files
committed
improve hot-reloading
1 parent 60aa586 commit 3c44137

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

lib/router.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,14 @@ export default class Router {
4949
})
5050
}
5151

52-
async update (route, data) {
53-
data.Component = evalScript(data.component).default
54-
delete data.component
55-
this.components[route] = data
52+
update (route, Component) {
53+
const data = this.components[route] || {}
54+
const newData = { ...data, Component }
55+
this.components[route] = newData
5656

5757
if (route === this.route) {
58-
let props
59-
try {
60-
props = await this.getInitialProps(data.Component, {})
61-
} catch (err) {
62-
if (err.cancelled) return false
63-
throw err
64-
}
65-
this.notify({ ...data, props })
58+
this.notify(newData)
6659
}
67-
return true
6860
}
6961

7062
back () {
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1+
import { resolve, relative } from 'path'
12

23
module.exports = function (content) {
34
this.cacheable()
45

6+
const route = getRoute(this)
7+
58
return content + `
69
if (module.hot) {
710
module.hot.accept()
811
if ('idle' !== module.hot.status()) {
912
const Component = module.exports.default || module.exports
10-
next.router.notify({ Component })
13+
next.router.update('${route}', Component)
1114
}
1215
}
1316
`
1417
}
18+
19+
function getRoute (loaderContext) {
20+
const pagesDir = resolve(loaderContext.options.context, 'pages')
21+
const path = loaderContext.resourcePath
22+
return '/' + relative(pagesDir, path).replace(/((^|\/)index)?\.js$/, '')
23+
}

0 commit comments

Comments
 (0)