@@ -114,7 +114,7 @@ export interface SSRRenderOptions {
114
114
enum QwikLoaderInclude {
115
115
Module ,
116
116
Inline ,
117
- Never ,
117
+ Done ,
118
118
}
119
119
120
120
export function ssrCreateContainer ( opts : SSRRenderOptions ) : ISSRContainer {
@@ -255,12 +255,12 @@ class SSRContainer extends _SharedContainer implements ISSRContainer {
255
255
this . qlInclude = qlOpt
256
256
? typeof qlOpt === 'object'
257
257
? qlOpt . include === 'never'
258
- ? QwikLoaderInclude . Never
258
+ ? QwikLoaderInclude . Done
259
259
: QwikLoaderInclude . Module
260
260
: qlOpt === 'inline'
261
261
? QwikLoaderInclude . Inline
262
262
: qlOpt === 'never'
263
- ? QwikLoaderInclude . Never
263
+ ? QwikLoaderInclude . Done
264
264
: QwikLoaderInclude . Module
265
265
: QwikLoaderInclude . Module ;
266
266
if ( this . qlInclude === QwikLoaderInclude . Module ) {
@@ -387,6 +387,11 @@ class SSRContainer extends _SharedContainer implements ISSRContainer {
387
387
constAttrs ?: SsrAttrs | null ,
388
388
currentFile ?: string | null
389
389
) : string | undefined {
390
+ if ( this . qlInclude === QwikLoaderInclude . Inline && this . size > 30 * 1024 ) {
391
+ // We waited long enough, on slow connections the page is already partially visible
392
+ this . emitQwikLoaderInline ( ) ;
393
+ }
394
+
390
395
let innerHTML : string | undefined = undefined ;
391
396
this . lastNode = null ;
392
397
const isQwikStyle =
@@ -897,6 +902,7 @@ class SSRContainer extends _SharedContainer implements ISSRContainer {
897
902
898
903
emitQwikLoaderAtTopIfNeeded ( ) {
899
904
if ( this . qlInclude === QwikLoaderInclude . Module ) {
905
+ this . qlInclude = QwikLoaderInclude . Done ;
900
906
// always emit the preload+import. It will probably be used at some point on the site
901
907
const qwikLoaderBundle = this . $buildBase$ + this . resolvedManifest . manifest . qwikLoader ! ;
902
908
const linkAttrs = [ 'rel' , 'modulepreload' , 'href' , qwikLoaderBundle ] ;
@@ -913,31 +919,39 @@ class SSRContainer extends _SharedContainer implements ISSRContainer {
913
919
}
914
920
this . openElement ( 'script' , scriptAttrs ) ;
915
921
this . closeElement ( ) ;
916
- } else if ( this . qlInclude === QwikLoaderInclude . Inline ) {
917
- // TODO send at the end or after 30kB generated
918
- // if at the end, only include when snapshot is not static
919
- const qwikLoaderScript = getQwikLoaderScript ( { debug : this . renderOptions . debug } ) ;
920
- // module + async lets it run asap without waiting for DOM, even when inline
921
- const scriptAttrs = [ 'id' , 'qwikloader' , 'async' , true , 'type' , 'module' ] ;
922
- if ( this . renderOptions . serverData ?. nonce ) {
923
- scriptAttrs . push ( 'nonce' , this . renderOptions . serverData . nonce ) ;
924
- }
925
- this . openElement ( 'script' , scriptAttrs ) ;
926
- this . write ( qwikLoaderScript ) ;
927
- this . closeElement ( ) ;
928
922
}
929
923
}
930
924
925
+ emitQwikLoaderInline ( ) {
926
+ this . qlInclude = QwikLoaderInclude . Done ;
927
+ // if at the end, only include when snapshot is not static
928
+ const qwikLoaderScript = getQwikLoaderScript ( { debug : this . renderOptions . debug } ) ;
929
+ // module + async lets it run asap without waiting for DOM, even when inline
930
+ const scriptAttrs = [ 'id' , 'qwikloader' , 'async' , true , 'type' , 'module' ] ;
931
+ if ( this . renderOptions . serverData ?. nonce ) {
932
+ scriptAttrs . push ( 'nonce' , this . renderOptions . serverData . nonce ) ;
933
+ }
934
+ this . openElement ( 'script' , scriptAttrs ) ;
935
+ this . write ( qwikLoaderScript ) ;
936
+ this . closeElement ( ) ;
937
+ }
938
+
931
939
private emitQwikLoaderAtBottomIfNeeded ( ) {
932
- // emit the used events so the loader can subscribe to them
933
- this . emitQwikEvents ( Array . from ( this . serializationCtx . $eventNames$ , ( s ) => JSON . stringify ( s ) ) ) ;
940
+ if ( ! this . isStatic ( ) ) {
941
+ if ( this . qlInclude !== QwikLoaderInclude . Done ) {
942
+ this . emitQwikLoaderInline ( ) ;
943
+ }
944
+ // emit the used events so the loader can subscribe to them
945
+ this . emitQwikEvents ( Array . from ( this . serializationCtx . $eventNames$ , ( s ) => JSON . stringify ( s ) ) ) ;
946
+ }
934
947
}
935
948
936
949
private emitQwikEvents ( eventNames : string [ ] ) {
937
950
if ( eventNames . length > 0 ) {
938
- const scriptAttrs = this . renderOptions . serverData ?. nonce
939
- ? [ 'nonce' , this . renderOptions . serverData . nonce ]
940
- : null ;
951
+ const scriptAttrs = [ 'async' , true , 'type' , 'module' ] ;
952
+ if ( this . renderOptions . serverData ?. nonce ) {
953
+ scriptAttrs . push ( 'nonce' , this . renderOptions . serverData . nonce ) ;
954
+ }
941
955
this . openElement ( 'script' , scriptAttrs ) ;
942
956
this . write ( `(window.qwikevents||(window.qwikevents=[])).push(` ) ;
943
957
this . writeArray ( eventNames , ', ' ) ;
0 commit comments