1
1
import { Fragment , jsx , type JSXNode } from '@builder.io/qwik' ;
2
2
import type { ResolvedManifest } from '../optimizer/src/types' ;
3
3
import { expandBundles } from './preload-strategy' ;
4
- import type { PreloaderOptions } from './types' ;
4
+ import type { PreloaderOptions , RenderToStreamOptions , SnapshotResult } from './types' ;
5
+ import { initPreloader } from '../core/preloader/bundle-graph' ;
6
+ import { getPreloadPaths } from './preload-strategy' ;
5
7
6
- export function includePreloader (
8
+ export const includePreloader = (
7
9
base : string ,
8
- manifest : ResolvedManifest | undefined ,
10
+ resolvedManifest : ResolvedManifest | undefined ,
9
11
options : PreloaderOptions | boolean | undefined ,
10
12
referencedBundles : string [ ] ,
11
13
nonce ?: string
12
- ) : JSXNode | null {
14
+ ) : JSXNode | null => {
13
15
if ( referencedBundles . length === 0 || options === false ) {
14
16
return null ;
15
17
}
@@ -30,9 +32,9 @@ export function includePreloader(
30
32
31
33
const links = [ ] ;
32
34
33
- const manifestHash = manifest ?. manifest . manifestHash ;
35
+ const manifestHash = resolvedManifest ?. manifest . manifestHash ;
34
36
if ( allowed ) {
35
- const expandedBundles = expandBundles ( referencedBundles , manifest ) ;
37
+ const expandedBundles = expandBundles ( referencedBundles , resolvedManifest ) ;
36
38
// Keep the same as in getQueue (but *10)
37
39
let probability = 4 ;
38
40
const tenXMinProbability = ssrPreloadProbability * 10 ;
@@ -51,7 +53,7 @@ export function includePreloader(
51
53
}
52
54
}
53
55
54
- const preloadChunk = manifestHash && manifest ?. manifest . preloader ;
56
+ const preloadChunk = manifestHash && resolvedManifest ?. manifest . preloader ;
55
57
if ( preloadChunk ) {
56
58
const insertLinks = links . length
57
59
? /**
@@ -117,7 +119,68 @@ export function includePreloader(
117
119
}
118
120
119
121
return null ;
120
- }
122
+ } ;
123
+
124
+ export const preloaderPre = (
125
+ base : string ,
126
+ resolvedManifest : ResolvedManifest | undefined ,
127
+ options : PreloaderOptions | boolean | undefined ,
128
+ beforeContent : JSXNode < string > [ ]
129
+ ) => {
130
+ const preloadChunk = resolvedManifest ?. manifest ?. preloader ;
131
+ if ( preloadChunk && options !== false ) {
132
+ const bundleGraph = resolvedManifest ?. manifest . bundleGraph ;
133
+ if ( bundleGraph ) {
134
+ const preloaderOpts : Parameters < typeof initPreloader > [ 1 ] =
135
+ typeof options === 'object'
136
+ ? {
137
+ debug : options . debug ,
138
+ preloadProbability : options . ssrPreloadProbability ,
139
+ }
140
+ : undefined ;
141
+ initPreloader ( bundleGraph , preloaderOpts ) ;
142
+ }
143
+ const core = resolvedManifest ?. manifest . core ;
144
+ beforeContent . push (
145
+ jsx ( 'link' , { rel : 'modulepreload' , href : `${ base } ${ preloadChunk } ` } ) ,
146
+ jsx ( 'link' , {
147
+ rel : 'preload' ,
148
+ href : `${ base } q-bundle-graph-${ resolvedManifest ?. manifest . manifestHash } .json` ,
149
+ as : 'fetch' ,
150
+ crossorigin : 'anonymous' ,
151
+ } )
152
+ ) ;
153
+ if ( core ) {
154
+ beforeContent . push ( jsx ( 'link' , { rel : 'modulepreload' , href : `${ base } ${ core } ` } ) ) ;
155
+ }
156
+ }
157
+ } ;
158
+
159
+ export const preloaderPost = (
160
+ base : string ,
161
+ snapshotResult : SnapshotResult ,
162
+ opts : RenderToStreamOptions ,
163
+ resolvedManifest : ResolvedManifest | undefined ,
164
+ output : ( JSXNode | null ) [ ]
165
+ ) => {
166
+ if ( opts . preloader !== false ) {
167
+ // skip prefetch implementation if prefetchStrategy === null
168
+ const preloadBundles = getPreloadPaths ( snapshotResult , opts , resolvedManifest ) ;
169
+ // If no preloadBundles, there is no reactivity, so no need to include the preloader
170
+ if ( preloadBundles . length > 0 ) {
171
+ const result = includePreloader (
172
+ base ,
173
+ resolvedManifest ,
174
+ opts . preloader ,
175
+ preloadBundles ,
176
+ opts . serverData ?. nonce
177
+ ) ;
178
+ if ( result ) {
179
+ output . push ( result ) ;
180
+ }
181
+ }
182
+ }
183
+ } ;
121
184
122
185
function normalizePreLoaderOptions (
123
186
input : PreloaderOptions | undefined
0 commit comments