forked from QwikDev/qwik
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reduce prefetch request waterfall (QwikDev#1204)
- Loading branch information
1 parent
7e0b522
commit 49250a1
Showing
11 changed files
with
267 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
packages/qwik-city/runtime/src/library/service-worker/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import type { AppBundles } from './types'; | ||
import type { AppBundle, LinkBundle } from './types'; | ||
import { setupServiceWorkerScope } from './setup'; | ||
|
||
/** | ||
* @alpha | ||
*/ | ||
export const setupServiceWorker = () => { | ||
if (typeof self !== 'undefined' && typeof appBundles !== 'undefined') { | ||
setupServiceWorkerScope(self as any, appBundles); | ||
setupServiceWorkerScope(self as any, appBundles, libraryBundleIds, linkBundles); | ||
} | ||
}; | ||
|
||
declare const appBundles: AppBundles; | ||
declare const appBundles: AppBundle[]; | ||
declare const libraryBundleIds: number[]; | ||
declare const linkBundles: LinkBundle[]; |
102 changes: 80 additions & 22 deletions
102
packages/qwik-city/runtime/src/library/service-worker/prefetch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,97 @@ | ||
import type { Fetch, AppBundles } from './types'; | ||
import { cachedFetch } from './cached-fetch'; | ||
import type { AppBundle, Fetch, LinkBundle } from './types'; | ||
import { awaitingRequests, existingPrefetches } from './constants'; | ||
import { cachedFetch } from './cached-fetch'; | ||
import { getAppBundleByName, getAppBundlesNamesFromIds } from './utils'; | ||
|
||
export const prefetchBundleNames = ( | ||
appBundles: AppBundles, | ||
appBundles: AppBundle[], | ||
qBuildCache: Cache, | ||
fetch: Fetch, | ||
baseUrl: URL, | ||
activeDomQKeys: string[] | undefined, | ||
prefetchAppBundleNames: string[] | ||
prefetchAppBundleNames: (string | null)[] | undefined | null | ||
) => { | ||
const prefetchAppBundle = (prefetchAppBundleName: string) => { | ||
const appBundle = appBundles[prefetchAppBundleName]; | ||
const prefetchAppBundle = (prefetchAppBundleName: string | null) => { | ||
try { | ||
const appBundle = getAppBundleByName(appBundles, prefetchAppBundleName); | ||
|
||
if (appBundle && !existingPrefetches.has(prefetchAppBundleName)) { | ||
try { | ||
existingPrefetches.add(prefetchAppBundleName); | ||
if (appBundle && !existingPrefetches.has(prefetchAppBundleName!)) { | ||
existingPrefetches.add(prefetchAppBundleName!); | ||
|
||
const [importedBundleNames, symbolHashesInBundle] = appBundle; | ||
const importedBundleNames = getAppBundlesNamesFromIds(appBundles, appBundle[1]); | ||
const url = new URL(prefetchAppBundleName!, baseUrl); | ||
const request = new Request(url); | ||
|
||
const symbolActiveInDom = | ||
Array.isArray(activeDomQKeys) && | ||
activeDomQKeys.some((qKey) => symbolHashesInBundle.includes(qKey)); | ||
|
||
if (!symbolActiveInDom) { | ||
const url = new URL(prefetchAppBundleName, baseUrl).href; | ||
cachedFetch(qBuildCache, fetch, awaitingRequests, new Request(url)); | ||
} | ||
cachedFetch(qBuildCache, fetch, awaitingRequests, request); | ||
|
||
importedBundleNames.forEach(prefetchAppBundle); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
|
||
prefetchAppBundleNames.forEach(prefetchAppBundle); | ||
if (Array.isArray(prefetchAppBundleNames)) { | ||
prefetchAppBundleNames.forEach(prefetchAppBundle); | ||
} | ||
}; | ||
|
||
export const prefetchLinkBundles = ( | ||
appBundles: AppBundle[], | ||
libraryBundleIds: number[], | ||
linkBundles: LinkBundle[], | ||
qBuildCache: Cache, | ||
fetch: Fetch, | ||
baseUrl: URL, | ||
linkPathnames: string[] | ||
) => { | ||
try { | ||
prefetchBundleNames( | ||
appBundles, | ||
qBuildCache, | ||
fetch, | ||
baseUrl, | ||
getAppBundlesNamesFromIds(appBundles, libraryBundleIds) | ||
); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
|
||
for (const linkPathname of linkPathnames) { | ||
try { | ||
for (const linkBundle of linkBundles) { | ||
const [route, linkBundleIds] = linkBundle; | ||
console; | ||
if (route.test(linkPathname)) { | ||
prefetchBundleNames( | ||
appBundles, | ||
qBuildCache, | ||
fetch, | ||
baseUrl, | ||
getAppBundlesNamesFromIds(appBundles, linkBundleIds) | ||
); | ||
break; | ||
} | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
}; | ||
|
||
export const prefetchWaterfall = ( | ||
appBundles: AppBundle[], | ||
qBuildCache: Cache, | ||
fetch: Fetch, | ||
requestedBuildUrl: URL | ||
) => { | ||
try { | ||
const segments = requestedBuildUrl.href.split('/'); | ||
const requestedBundleName = segments[segments.length - 1]; | ||
segments[segments.length - 1] = ''; | ||
const baseUrl = new URL(segments.join('/')); | ||
|
||
prefetchBundleNames(appBundles, qBuildCache, fetch, baseUrl, [requestedBundleName]); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.