@@ -31,6 +31,12 @@ const relPrefetch =
31
31
32
32
const hasNoModule = 'noModule' in document . createElement ( 'script' )
33
33
34
+ const requestIdleCallback =
35
+ window . requestIdleCallback ||
36
+ function ( cb ) {
37
+ return setTimeout ( cb , 1 )
38
+ }
39
+
34
40
/** @param {string } route */
35
41
function normalizeRoute ( route ) {
36
42
if ( route [ 0 ] !== '/' ) {
@@ -184,19 +190,20 @@ export default class PageLoader {
184
190
prefetchData ( href , asPath ) {
185
191
const { pathname : hrefPathname } = parseRelativeUrl ( href )
186
192
const route = normalizeRoute ( hrefPathname )
187
- return this . promisedSsgManifest . then (
188
- ( s , _dataHref ) =>
193
+ return this . promisedSsgManifest . then ( ( s , _dataHref ) => {
194
+ requestIdleCallback ( ( ) => {
189
195
// Check if the route requires a data file
190
196
s . has ( route ) &&
191
- // Try to generate data href, noop when falsy
192
- ( _dataHref = this . getDataHref ( href , asPath , true ) ) &&
193
- // noop when data has already been prefetched (dedupe)
194
- ! document . querySelector (
195
- `link[rel="${ relPrefetch } "][href^="${ _dataHref } "]`
196
- ) &&
197
- // Inject the `<link rel=prefetch>` tag for above computed `href`.
198
- appendLink ( _dataHref , relPrefetch , 'fetch' )
199
- )
197
+ // Try to generate data href, noop when falsy
198
+ ( _dataHref = this . getDataHref ( href , asPath , true ) ) &&
199
+ // noop when data has already been prefetched (dedupe)
200
+ ! document . querySelector (
201
+ `link[rel="${ relPrefetch } "][href^="${ _dataHref } "]`
202
+ ) &&
203
+ // Inject the `<link rel=prefetch>` tag for above computed `href`.
204
+ appendLink ( _dataHref , relPrefetch , 'fetch' )
205
+ } )
206
+ } )
200
207
}
201
208
202
209
loadPage ( route ) {
0 commit comments