|
| 1 | +/** |
| 2 | + * Here we handle preloading of chunks. |
| 3 | + * |
| 4 | + * Given a symbol hash (in fact any string), we can find all the chunks that it depends on, via the |
| 5 | + * bundle graph. We then generate preload link tags for each of those chunks. |
| 6 | + * |
| 7 | + * The priority is set to high for direct imports and low for indirect imports. |
| 8 | + * |
| 9 | + * There are several parts to this: |
| 10 | + * |
| 11 | + * - Load the bundle graph from the preload link tag that was injected during SSR |
| 12 | + * - Given a string, find all the chunks that it depends on |
| 13 | + * - Generate the preload link tags if needed |
| 14 | + */ |
| 15 | + |
| 16 | +import { isDev } from '@builder.io/qwik/build'; |
| 17 | +import type { QwikBundleGraph } from '../../optimizer/src/types'; |
| 18 | +import { QBaseAttr, QManifestHash } from '../util/markers'; |
| 19 | + |
| 20 | +import { QContainerSelector } from '../util/markers'; |
| 21 | + |
| 22 | +let bundlesP: Promise<void> | undefined; |
| 23 | +enum BundleImportState { |
| 24 | + None, |
| 25 | + Low, |
| 26 | + Loading, |
| 27 | +} |
| 28 | +type BundleImport = { |
| 29 | + $url$: string | null; |
| 30 | + $state$: BundleImportState; |
| 31 | + $imports$: string[]; |
| 32 | + $dynamicImports$: string[]; |
| 33 | +}; |
| 34 | +let bundles: Map<string, BundleImport> | undefined; |
| 35 | +type WantedBundle = { |
| 36 | + name: string; |
| 37 | + priority: boolean; |
| 38 | +}; |
| 39 | +const wantedBundles: Set<WantedBundle> = new Set(); |
| 40 | + |
| 41 | +const parseBundleGraph = (text: string, base: string) => { |
| 42 | + try { |
| 43 | + const graph = JSON.parse(text) as QwikBundleGraph; |
| 44 | + bundles ||= new Map<string, BundleImport>(); |
| 45 | + let i = 0; |
| 46 | + while (i < graph.length) { |
| 47 | + const name = graph[i++] as string; |
| 48 | + const url = name.endsWith('.js') ? `${base}${name}` : null; |
| 49 | + const imports: string[] = []; |
| 50 | + const dynamicImports: string[] = []; |
| 51 | + let idx: number | string; |
| 52 | + let collection = imports; |
| 53 | + while (((idx = graph[i]), typeof idx === 'number')) { |
| 54 | + if (idx === -1) { |
| 55 | + collection = dynamicImports; |
| 56 | + } else { |
| 57 | + collection.push(graph[idx] as string); |
| 58 | + } |
| 59 | + i++; |
| 60 | + } |
| 61 | + bundles.set(name, { |
| 62 | + $url$: url, |
| 63 | + $state$: BundleImportState.None, |
| 64 | + $imports$: imports, |
| 65 | + $dynamicImports$: dynamicImports, |
| 66 | + }); |
| 67 | + } |
| 68 | + for (const { name, priority } of wantedBundles) { |
| 69 | + preload(name, priority); |
| 70 | + } |
| 71 | + wantedBundles.clear(); |
| 72 | + } catch (e) { |
| 73 | + console.error('Error parsing bundle graph', e, text); |
| 74 | + throw e; |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +export const loadBundleGraph = (element: Element) => { |
| 79 | + if (typeof window === 'undefined' || bundlesP) { |
| 80 | + return; |
| 81 | + } |
| 82 | + const container = element.closest(QContainerSelector); |
| 83 | + if (!container) { |
| 84 | + return; |
| 85 | + } |
| 86 | + const hash = container.getAttribute(QManifestHash); |
| 87 | + const base = container.getAttribute(QBaseAttr) || '/'; |
| 88 | + const link = hash && (container.querySelector(`link#qwik-bg-${hash}`) as HTMLLinkElement | null); |
| 89 | + if (!link) { |
| 90 | + bundlesP = Promise.reject('No preload link found'); |
| 91 | + // prevent uncaught promise rejection |
| 92 | + bundlesP.catch(() => {}); |
| 93 | + return; |
| 94 | + } |
| 95 | + bundlesP = fetch(link.href) |
| 96 | + .then((res) => res.text()) |
| 97 | + .then((text) => parseBundleGraph(text, base)) |
| 98 | + .catch((e) => { |
| 99 | + console.error('Error loading bundle graph, retrying later', e); |
| 100 | + setTimeout(() => { |
| 101 | + bundlesP = undefined; |
| 102 | + }, 60000); |
| 103 | + }); |
| 104 | +}; |
| 105 | + |
| 106 | +const makePreloadLink = (bundle: BundleImport, priority: boolean) => { |
| 107 | + const link = document.createElement('link'); |
| 108 | + link.rel = 'modulepreload'; |
| 109 | + link.href = bundle.$url$!; |
| 110 | + link.fetchPriority = priority ? 'high' : 'low'; |
| 111 | + link.as = 'script'; |
| 112 | + document.head.appendChild(link); |
| 113 | +}; |
| 114 | + |
| 115 | +const prioritizeLink = (url: string) => { |
| 116 | + const link = document.querySelector(`link[href="${url}"]`) as HTMLLinkElement | null; |
| 117 | + if (link) { |
| 118 | + link.fetchPriority = 'high'; |
| 119 | + } else { |
| 120 | + console.warn(`Preload link ${url} not found`); |
| 121 | + } |
| 122 | +}; |
| 123 | + |
| 124 | +const preloadBundle = (bundle: BundleImport, priority: boolean) => { |
| 125 | + if (bundle.$state$ >= BundleImportState.Loading) { |
| 126 | + return; |
| 127 | + } |
| 128 | + if (bundle.$url$) { |
| 129 | + if (bundle.$state$ === BundleImportState.None) { |
| 130 | + makePreloadLink(bundle, priority); |
| 131 | + } else if (priority && bundle.$state$ === BundleImportState.Low) { |
| 132 | + prioritizeLink(bundle.$url$!); |
| 133 | + } else { |
| 134 | + return; |
| 135 | + } |
| 136 | + } |
| 137 | + bundle.$state$ = priority ? BundleImportState.Loading : BundleImportState.Low; |
| 138 | +}; |
| 139 | + |
| 140 | +export const preload = (name: string, priority: boolean) => { |
| 141 | + if (!bundles) { |
| 142 | + wantedBundles.add({ name, priority }); |
| 143 | + return; |
| 144 | + } |
| 145 | + const bundle = bundles.get(name); |
| 146 | + if (!bundle) { |
| 147 | + isDev && console.warn(`Bundle ${name} not found`); |
| 148 | + return; |
| 149 | + } |
| 150 | + const isReprioritize = priority && bundle.$state$ === BundleImportState.Low; |
| 151 | + if (bundle.$state$ !== BundleImportState.None && !isReprioritize) { |
| 152 | + // prevent loops |
| 153 | + return; |
| 154 | + } |
| 155 | + preloadBundle(bundle, priority); |
| 156 | + for (const importName of bundle.$imports$) { |
| 157 | + preload(importName, priority); |
| 158 | + } |
| 159 | + if (!isReprioritize) { |
| 160 | + for (const importName of bundle.$dynamicImports$) { |
| 161 | + preload(importName, false); |
| 162 | + } |
| 163 | + } |
| 164 | +}; |
0 commit comments