@@ -20,40 +20,49 @@ export const ScriptManagerScripts = ({ scripts, strategy }: ScriptRendererProps)
2020
2121 return (
2222 < >
23- { scriptNodes . map ( ( script ) => {
24- const scriptProps : ComponentProps < typeof Script > = {
25- strategy,
26- } ;
27-
28- // Handle external scripts (SrcScript)
29- if ( script . __typename === 'SrcScript' && script . src ) {
30- scriptProps . src = script . src ;
31-
32- // Add integrity hashes if provided
33- if ( script . integrityHashes . length > 0 ) {
34- scriptProps . integrity = script . integrityHashes . map ( ( hashObj ) => hashObj . hash ) . join ( ' ' ) ;
35- scriptProps . crossOrigin = 'anonymous' ;
23+ { scriptNodes
24+ . map ( ( script ) => {
25+ const scriptProps : ComponentProps < typeof Script > = {
26+ strategy,
27+ } ;
28+
29+ // Handle external scripts (SrcScript)
30+ if ( script . __typename === 'SrcScript' && script . src ) {
31+ scriptProps . src = script . src ;
32+
33+ // Add integrity hashes if provided
34+ if ( script . integrityHashes . length > 0 ) {
35+ scriptProps . integrity = script . integrityHashes
36+ . map ( ( hashObj ) => hashObj . hash )
37+ . join ( ' ' ) ;
38+ scriptProps . crossOrigin = 'anonymous' ;
39+ }
3640 }
37- }
3841
39- // Handle inline scripts (InlineScript)
40- if ( script . __typename === 'InlineScript' && script . scriptTag ) {
41- const scriptMatch = / < s c r i p t [ ^ > ] * > ( [ \s \S ] * ?) < \/ s c r i p t > / i. exec ( script . scriptTag ) ;
42+ // Handle inline scripts (InlineScript)
43+ if ( script . __typename === 'InlineScript' && script . scriptTag ) {
44+ const scriptMatch = / < s c r i p t [ ^ > ] * > ( [ \s \S ] * ?) < \/ s c r i p t > / i. exec ( script . scriptTag ) ;
4245
43- if ( scriptMatch ?. [ 1 ] ) {
44- scriptProps . dangerouslySetInnerHTML = {
45- __html : scriptMatch [ 1 ] ,
46- } ;
46+ if ( scriptMatch ?. [ 1 ] ) {
47+ scriptProps . dangerouslySetInnerHTML = {
48+ __html : scriptMatch [ 1 ] ,
49+ } ;
50+ }
4751 }
48- }
4952
50- scriptProps . id = `bc-script-${ script . entityId } ` ;
53+ scriptProps . id = `bc-script-${ script . entityId } ` ;
54+
55+ // Return null for invalid scripts (will be filtered out)
56+ if ( ! scriptProps . src && ! scriptProps . dangerouslySetInnerHTML ) {
57+ return null ;
58+ }
5159
52- if ( ! scriptProps . src && ! scriptProps . dangerouslySetInnerHTML ) {
53- return null ;
54- }
55- return < Script key = { scriptProps . id } { ...scriptProps } /> ;
56- } ) }
60+ return scriptProps ;
61+ } )
62+ . filter ( ( scriptProps ) : scriptProps is ComponentProps < typeof Script > => scriptProps !== null )
63+ . map ( ( scriptProps ) => {
64+ return < Script key = { scriptProps . id } { ...scriptProps } /> ;
65+ } ) }
5766 </ >
5867 ) ;
5968} ;
0 commit comments