( base: string, resolvedManifest: ResolvedManifest | undefined, options: PreloaderOptions | false | undefined, beforeContent: JSXNode<string>[], nonce?: string )
| 22 | }; |
| 23 | |
| 24 | export const preloaderPre = ( |
| 25 | base: string, |
| 26 | resolvedManifest: ResolvedManifest | undefined, |
| 27 | options: PreloaderOptions | false | undefined, |
| 28 | beforeContent: JSXNode<string>[], |
| 29 | nonce?: string |
| 30 | ) => { |
| 31 | const preloaderPath = simplifyPath(base, resolvedManifest?.manifest?.preloader); |
| 32 | const bundleGraphPath = |
| 33 | (import.meta.env.BASE_URL || '/') + resolvedManifest?.manifest.bundleGraphAsset; |
| 34 | if (preloaderPath && bundleGraphPath && options !== false) { |
| 35 | // Initialize the SSR preloader |
| 36 | const preloaderOpts: Parameters<typeof initPreloader>[1] = |
| 37 | typeof options === 'object' |
| 38 | ? { |
| 39 | debug: options.debug, |
| 40 | preloadProbability: options.ssrPreloadProbability, |
| 41 | } |
| 42 | : undefined; |
| 43 | initPreloader(resolvedManifest?.manifest.bundleGraph, preloaderOpts); |
| 44 | |
| 45 | // Add the preloader script to the head |
| 46 | const opts: string[] = []; |
| 47 | if (options?.debug) { |
| 48 | opts.push('d:1'); |
| 49 | } |
| 50 | if (options?.maxIdlePreloads) { |
| 51 | opts.push(`P:${options.maxIdlePreloads}`); |
| 52 | } |
| 53 | if (options?.preloadProbability) { |
| 54 | opts.push(`Q:${options.preloadProbability}`); |
| 55 | } |
| 56 | const optsStr = opts.length ? `,{${opts.join(',')}}` : ''; |
| 57 | |
| 58 | const script = |
| 59 | `let b=fetch("${bundleGraphPath}");` + |
| 60 | `import("${preloaderPath}").then(({l})=>` + |
| 61 | `l(${JSON.stringify(base)},b${optsStr})` + |
| 62 | `);`; |
| 63 | |
| 64 | beforeContent.push( |
| 65 | /** |
| 66 | * We add modulepreloads even when the script is at the top because they already fire during |
| 67 | * html download |
| 68 | */ |
| 69 | jsx('link', { rel: 'modulepreload', href: preloaderPath, nonce, crossorigin: 'anonymous' }), |
| 70 | jsx('link', { |
| 71 | rel: 'preload', |
| 72 | href: bundleGraphPath, |
| 73 | as: 'fetch', |
| 74 | crossorigin: 'anonymous', |
| 75 | nonce, |
| 76 | }), |
| 77 | jsx('script', { |
| 78 | type: 'module', |
| 79 | async: true, |
| 80 | dangerouslySetInnerHTML: script, |
| 81 | nonce, |
no test coverage detected
searching dependent graphs…