Skip to content

Commit 01a1773

Browse files
arunodankzawa
authored andcommitted
Reset prefetchCache when reloading a route. (#617)
* Reset prefetchCache when reloading a route. * Prefetch again when there's a HMR reload. * Make .send() to return a promise.
1 parent 8569789 commit 01a1773

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

lib/prefetch.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ class Messenger {
2424
this._resetCache()
2525
}
2626

27-
send (payload, cb) {
28-
if (this.serviceWorkerState === 'REGISTERED') {
29-
this._send(payload, cb)
30-
} else {
31-
this.serviceWorkerReadyCallbacks.push(() => {
32-
this._send(payload, cb)
33-
})
34-
}
27+
send (payload) {
28+
return new Promise((resolve, reject) => {
29+
if (this.serviceWorkerState === 'REGISTERED') {
30+
this._send(payload, handleCallback)
31+
} else {
32+
this.serviceWorkerReadyCallbacks.push(() => {
33+
this._send(payload, handleCallback)
34+
})
35+
}
36+
37+
function handleCallback (err) {
38+
if (err) return reject(err)
39+
return resolve()
40+
}
41+
})
3542
}
3643

3744
_send (payload, cb = () => {}) {
@@ -99,23 +106,35 @@ if (hasServiceWorkerSupport()) {
99106
messenger = new Messenger()
100107
}
101108

102-
export function prefetch (href) {
109+
function getPrefetchUrl (href) {
110+
let { pathname } = urlParse(href)
111+
const url = `/_next/pages${pathname}`
112+
113+
return url
114+
}
115+
116+
export async function prefetch (href) {
103117
if (!hasServiceWorkerSupport()) return
104118
if (!isLocal(href)) return
105119

106120
// Register the service worker if it's not.
107121
messenger.ensureInitialized()
108122

109-
let { pathname } = urlParse(href)
110-
// Add support for the index page
111-
112-
const url = `/_next/pages${pathname}`
123+
const url = getPrefetchUrl(href)
113124
if (PREFETCHED_URLS[url]) return
114125

115-
messenger.send({ action: 'ADD_URL', url: url })
126+
await messenger.send({ action: 'ADD_URL', url: url })
116127
PREFETCHED_URLS[url] = true
117128
}
118129

130+
export async function reloadIfPrefetched (href) {
131+
const url = getPrefetchUrl(href)
132+
if (!PREFETCHED_URLS[url]) return
133+
134+
delete PREFETCHED_URLS[url]
135+
await prefetch(href)
136+
}
137+
119138
export default class LinkPrefetch extends React.Component {
120139
render () {
121140
const { href } = this.props

lib/router/router.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { parse, format } from 'url'
22
import evalScript from '../eval-script'
33
import shallowEquals from '../shallow-equals'
44
import { EventEmitter } from 'events'
5+
import { reloadIfPrefetched } from '../prefetch'
56

67
export default class Router extends EventEmitter {
78
constructor (pathname, query, { Component, ErrorComponent, ctx } = {}) {
@@ -77,6 +78,7 @@ export default class Router extends EventEmitter {
7778

7879
async reload (route) {
7980
delete this.components[route]
81+
await reloadIfPrefetched(route)
8082

8183
if (route !== this.route) return
8284

0 commit comments

Comments
 (0)