@@ -20,40 +20,47 @@ 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 . map ( ( hashObj ) => hashObj . hash ) . join ( ' ' ) ;
36+ scriptProps . crossOrigin = 'anonymous' ;
37+ }
3638 }
37- }
3839
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 ) ;
40+ // Handle inline scripts (InlineScript)
41+ if ( script . __typename === 'InlineScript' && script . scriptTag ) {
42+ const scriptMatch = / < s c r i p t [ ^ > ] * > ( [ \s \S ] * ?) < \/ s c r i p t > / i. exec ( script . scriptTag ) ;
4243
43- if ( scriptMatch ?. [ 1 ] ) {
44- scriptProps . dangerouslySetInnerHTML = {
45- __html : scriptMatch [ 1 ] ,
46- } ;
44+ if ( scriptMatch ?. [ 1 ] ) {
45+ scriptProps . dangerouslySetInnerHTML = {
46+ __html : scriptMatch [ 1 ] ,
47+ } ;
48+ }
4749 }
48- }
4950
50- scriptProps . id = `bc-script-${ script . entityId } ` ;
51+ scriptProps . id = `bc-script-${ script . entityId } ` ;
52+
53+ // Return null for invalid scripts (will be filtered out)
54+ if ( ! scriptProps . src && ! scriptProps . dangerouslySetInnerHTML ) {
55+ return null ;
56+ }
5157
52- if ( ! scriptProps . src && ! scriptProps . dangerouslySetInnerHTML ) {
53- return null ;
54- }
55- return < Script key = { scriptProps . id } { ...scriptProps } /> ;
56- } ) }
58+ return scriptProps ;
59+ } )
60+ . filter ( ( scriptProps ) : scriptProps is ComponentProps < typeof Script > => scriptProps !== null )
61+ . map ( ( scriptProps ) => {
62+ return < Script key = { scriptProps . id } { ...scriptProps } /> ;
63+ } ) }
5764 </ >
5865 ) ;
5966} ;
0 commit comments