@@ -9,14 +9,43 @@ export function renderScriptElement({ props, children }: SSRElement) {
99 } ) ;
1010}
1111
12+ export function renderUniqueScriptElement ( result : SSRResult , { props, children } : SSRElement ) {
13+ if ( Array . from ( result . scripts ) . some ( s => {
14+ if ( s . props . type === props . type && s . props . src === props . src ) {
15+ return true ;
16+ }
17+ if ( ! props . src && s . children === children ) return true ;
18+ } ) ) return '' ;
19+ const key = `script-${ props . type } -${ props . src } -${ children } ` ;
20+ if ( checkOrAddContentKey ( result , key ) ) return '' ;
21+ return renderScriptElement ( { props, children } ) ;
22+
23+ }
24+
1225export function renderUniqueStylesheet ( result : SSRResult , sheet : StylesheetAsset ) {
1326 if ( sheet . type === 'external' ) {
1427 if ( Array . from ( result . styles ) . some ( ( s ) => s . props . href === sheet . src ) ) return '' ;
15- return renderElement ( 'link' , { props : { rel : 'stylesheet' , href : sheet . src } , children : '' } ) ;
28+ const key = 'link-external-' + sheet . src ;
29+ if ( checkOrAddContentKey ( result , key ) ) return '' ;
30+ return renderElement ( 'link' , {
31+ props : {
32+ rel : 'stylesheet' ,
33+ href : sheet . src
34+ } ,
35+ children : ''
36+ } ) ;
1637 }
1738
1839 if ( sheet . type === 'inline' ) {
1940 if ( Array . from ( result . styles ) . some ( ( s ) => s . children . includes ( sheet . content ) ) ) return '' ;
41+ const key = `link-inline-` + sheet . content ;
42+ if ( checkOrAddContentKey ( result , key ) ) return '' ;
2043 return renderElement ( 'style' , { props : { type : 'text/css' } , children : sheet . content } ) ;
2144 }
2245}
46+
47+ function checkOrAddContentKey ( result : SSRResult , key : string ) : boolean {
48+ if ( result . _metadata . contentKeys . has ( key ) ) return true ;
49+ result . _metadata . contentKeys . add ( key ) ;
50+ return false ;
51+ }
0 commit comments