Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
perf: don't add more preload components when we're fetching > 4
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Oct 15, 2022
1 parent e99c36d commit 6e40b0e
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/nuxt/src/app/components/nuxt-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,21 +329,30 @@ function isSlowConnection () {
return false
}

async function preloadRouteComponents (to: string, router: Router & { _nuxtLinkPreloaded?: Set<string> } = useRouter()) {
async function preloadRouteComponents (to: string, router: Router & { _nuxtLinkPreloaded?: Set<string>; _preloadPromises?: Array<Promise<any>> } = useRouter()): Promise<void> {
if (process.server) { return }

if (!router._nuxtLinkPreloaded) { router._nuxtLinkPreloaded = new Set() }
if (router._nuxtLinkPreloaded.has(to)) { return }
router._nuxtLinkPreloaded.add(to)

const promises = router._preloadPromises ||= []

if (promises.length > 4) {
// Defer adding new preload requests until the existing ones have resolved
return Promise.all(promises).then(() => preloadRouteComponents(to, router))
}

const components = router.resolve(to).matched
.map(component => component.components?.default)
.filter(component => typeof component === 'function')

const promises: Promise<any>[] = []
for (const component of components) {
const promise = Promise.resolve((component as Function)()).catch(() => {})
const promise = Promise.resolve((component as Function)())
.catch(() => {})
.finally(() => promises.splice(promises.indexOf(promise)))
promises.push(promise)
}

await Promise.all(promises)
}

0 comments on commit 6e40b0e

Please sign in to comment.