-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(gatsby,gatsby-link): add queue to prefetch #33530
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ const createPageDataUrl = rawPath => { | |
} | ||
|
||
function doFetch(url, method = `GET`) { | ||
return new Promise((resolve, reject) => { | ||
return new Promise(resolve => { | ||
const req = new XMLHttpRequest() | ||
req.open(method, url, true) | ||
req.onreadystatechange = () => { | ||
|
@@ -98,6 +98,8 @@ export class BaseLoader { | |
this.inFlightDb = new Map() | ||
this.staticQueryDb = {} | ||
this.pageDataDb = new Map() | ||
this.isPrefetchQueueRunning = false | ||
this.prefetchQueued = [] | ||
this.prefetchTriggered = new Set() | ||
this.prefetchCompleted = new Set() | ||
this.loadComponent = loadComponent | ||
|
@@ -396,32 +398,90 @@ export class BaseLoader { | |
|
||
prefetch(pagePath) { | ||
if (!this.shouldPrefetch(pagePath)) { | ||
return false | ||
return { | ||
then: resolve => resolve(false), | ||
abort: () => {}, | ||
} | ||
} | ||
if (this.prefetchTriggered.has(pagePath)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if already prefetched this is a no-op but we keep the signature |
||
return { | ||
then: resolve => resolve(true), | ||
abort: () => {}, | ||
} | ||
} | ||
|
||
const defer = { | ||
resolve: null, | ||
reject: null, | ||
promise: null, | ||
} | ||
defer.promise = new Promise((resolve, reject) => { | ||
defer.resolve = resolve | ||
defer.reject = reject | ||
}) | ||
this.prefetchQueued.push([pagePath, defer]) | ||
const abortC = new AbortController() | ||
wardpeet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
abortC.signal.addEventListener(`abort`, () => { | ||
const index = this.prefetchQueued.findIndex(([p]) => p === pagePath) | ||
// remove from the queue | ||
if (index !== -1) { | ||
this.prefetchQueued.splice(index, 1) | ||
} | ||
}) | ||
pieh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Tell plugins with custom prefetching logic that they should start | ||
// prefetching this path. | ||
if (!this.prefetchTriggered.has(pagePath)) { | ||
this.apiRunner(`onPrefetchPathname`, { pathname: pagePath }) | ||
this.prefetchTriggered.add(pagePath) | ||
if (!this.isPrefetchQueueRunning) { | ||
this.isPrefetchQueueRunning = true | ||
setTimeout(() => { | ||
this._processNextPrefetchBatch() | ||
}, 3000) | ||
} | ||
|
||
// If a plugin has disabled core prefetching, stop now. | ||
if (this.prefetchDisabled) { | ||
return false | ||
return { | ||
then: (resolve, reject) => defer.promise.then(resolve, reject), | ||
abort: abortC.abort.bind(abortC), | ||
} | ||
} | ||
|
||
_processNextPrefetchBatch() { | ||
const idleCallback = window.requestIdleCallback || (cb => setTimeout(cb, 0)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Start Prefetch when idle |
||
|
||
idleCallback(() => { | ||
const toPrefetch = this.prefetchQueued.splice(0, 4) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To me this is a bit weird - we create a batch here, but then later we might skip some that were already prefetched - this looks to me like we might create a lot of batches that do NO-OP, but because of construction, we might multiple 3s intervals until prefetching actually do anything? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True but it's difficult to do this correclty otherwise because a prefetch can get aborted and remove from the queue so checking if it's part of the queue beforehand might negate in never prefetching. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I can do when it's already prefetched, remove all instances of the path in the queue There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider this example: I have a link in the header and a link at the bottom of the viewprot both going to page A. Both pages get inside the prefetch queue. If I scroll down before 3s, the first link gets aborted thus not requested. Now page A still gets prefetched because we had 2 links. If I understand you correctly, this is what you saying, right? What we should in the future do is when a link is starting to prefetch remove other instances if present. |
||
const prefetches = Promise.all( | ||
toPrefetch.map(([pagePath, dPromise]) => { | ||
// Tell plugins with custom prefetching logic that they should start | ||
// prefetching this path. | ||
if (!this.prefetchTriggered.has(pagePath)) { | ||
this.apiRunner(`onPrefetchPathname`, { pathname: pagePath }) | ||
this.prefetchTriggered.add(pagePath) | ||
} | ||
|
||
// If a plugin has disabled core prefetching, stop now. | ||
if (this.prefetchDisabled) { | ||
return dPromise.resolve(false) | ||
} | ||
pieh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return this.doPrefetch(findPath(pagePath)).then(() => { | ||
if (!this.prefetchCompleted.has(pagePath)) { | ||
this.apiRunner(`onPostPrefetchPathname`, { pathname: pagePath }) | ||
this.prefetchCompleted.add(pagePath) | ||
} | ||
|
||
dPromise.resolve(true) | ||
}) | ||
}) | ||
) | ||
|
||
const realPath = findPath(pagePath) | ||
// Todo make doPrefetch logic cacheable | ||
// eslint-disable-next-line consistent-return | ||
this.doPrefetch(realPath).then(() => { | ||
if (!this.prefetchCompleted.has(pagePath)) { | ||
this.apiRunner(`onPostPrefetchPathname`, { pathname: pagePath }) | ||
this.prefetchCompleted.add(pagePath) | ||
if (this.prefetchQueued.length) { | ||
prefetches.then(() => { | ||
setTimeout(() => { | ||
this._processNextPrefetchBatch() | ||
}, 3000) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait 3 seconds for the next batch - we should add config options for these things. |
||
}) | ||
} else { | ||
this.isPrefetchQueueRunning = false | ||
} | ||
}) | ||
|
||
return true | ||
} | ||
|
||
doPrefetch(pagePath) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removes prefetch when link gets out of viewport. No need to add it to the queue