|
| 1 | +import { removeEdgesAndNodes } from '@bigcommerce/catalyst-client'; |
| 2 | +import Script from 'next/script'; |
| 3 | + |
| 4 | +interface ScriptNode { |
| 5 | + entityId: unknown; |
| 6 | + consentCategory: string; |
| 7 | + location: string; |
| 8 | + visibility: string; |
| 9 | + __typename?: 'InlineScript' | 'SrcScript'; |
| 10 | + scriptTag?: string; |
| 11 | + src?: string; |
| 12 | + integrityHashes: Array<{ |
| 13 | + hash: string; |
| 14 | + }>; |
| 15 | +} |
| 16 | + |
| 17 | +interface ScriptsData { |
| 18 | + edges: Array<{ |
| 19 | + node: ScriptNode; |
| 20 | + }> | null; |
| 21 | +} |
| 22 | + |
| 23 | +interface ScriptRendererProps { |
| 24 | + scripts: ScriptsData | null; |
| 25 | + location: 'head' | 'footer'; |
| 26 | + strategy: 'afterInteractive' | 'lazyOnload'; |
| 27 | +} |
| 28 | + |
| 29 | +interface ScriptsProps { |
| 30 | + scripts: ScriptsData | null; |
| 31 | +} |
| 32 | + |
| 33 | +interface ScriptProps { |
| 34 | + src?: string; |
| 35 | + dangerouslySetInnerHTML?: { |
| 36 | + __html: string; |
| 37 | + }; |
| 38 | + strategy: 'afterInteractive' | 'lazyOnload'; |
| 39 | + integrity?: string; |
| 40 | + id?: string; |
| 41 | + crossorigin?: string; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Shared component that renders BigCommerce scripts using Next.js Script component |
| 46 | + * @param {ScriptRendererProps} props - The props for the script renderer |
| 47 | + * @param {ScriptsData | null} props.scripts - The scripts data from GraphQL |
| 48 | + * @param {'head' | 'footer'} props.location - Where to render the scripts |
| 49 | + * @param {'afterInteractive' | 'lazyOnload'} props.strategy - The loading strategy |
| 50 | + * @returns {JSX.Element | null} JSX element containing the rendered scripts |
| 51 | + */ |
| 52 | +const ScriptRenderer = ({ scripts, location, strategy }: ScriptRendererProps) => { |
| 53 | + if (!scripts?.edges) return null; |
| 54 | + |
| 55 | + const scriptNodes = removeEdgesAndNodes(scripts); |
| 56 | + const locationScripts = scriptNodes.filter( |
| 57 | + (script) => script.location.toLowerCase() === location, |
| 58 | + ); |
| 59 | + const filteredScripts = locationScripts.filter( |
| 60 | + (script) => script.visibility === 'ALL_PAGES' || script.visibility === 'STOREFRONT', |
| 61 | + ); |
| 62 | + |
| 63 | + return ( |
| 64 | + <> |
| 65 | + {filteredScripts.map((script) => { |
| 66 | + const scriptProps: ScriptProps = { |
| 67 | + strategy, |
| 68 | + }; |
| 69 | + |
| 70 | + // Handle external scripts (SrcScript) |
| 71 | + if (script.__typename === 'SrcScript' && script.src) { |
| 72 | + scriptProps.src = script.src; |
| 73 | + } |
| 74 | + |
| 75 | + // Handle inline scripts (InlineScript) |
| 76 | + if (script.__typename === 'InlineScript' && script.scriptTag) { |
| 77 | + const scriptMatch = /<script[^>]*>([\s\S]*?)<\/script>/i.exec(script.scriptTag); |
| 78 | + |
| 79 | + if (scriptMatch?.[1]) { |
| 80 | + scriptProps.dangerouslySetInnerHTML = { |
| 81 | + __html: scriptMatch[1], |
| 82 | + }; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Add integrity hashes if provided |
| 87 | + if (script.integrityHashes.length > 0) { |
| 88 | + scriptProps.integrity = script.integrityHashes.map((hashObj) => hashObj.hash).join(' '); |
| 89 | + scriptProps.crossorigin = 'anonymous'; |
| 90 | + } |
| 91 | + |
| 92 | + // Add id for better debugging |
| 93 | + const entityId = |
| 94 | + (script.entityId && typeof script.entityId === 'string') || |
| 95 | + typeof script.entityId === 'number' |
| 96 | + ? String(script.entityId) |
| 97 | + : `fallback-${Math.random().toString(36).substring(2, 11)}`; |
| 98 | + |
| 99 | + if (script.entityId) { |
| 100 | + scriptProps.id = `bc-script-${entityId}`; |
| 101 | + } |
| 102 | + |
| 103 | + return <Script key={`script-${entityId}`} {...scriptProps} />; |
| 104 | + })} |
| 105 | + </> |
| 106 | + ); |
| 107 | +}; |
| 108 | + |
| 109 | +/** |
| 110 | + * Renders scripts that should be placed in the <head> tag |
| 111 | + * Uses afterInteractive strategy for critical functionality |
| 112 | + * @param {ScriptsProps} props - The props containing scripts data |
| 113 | + * @param {ScriptsData | null} props.scripts - The scripts data from GraphQL |
| 114 | + * @returns {JSX.Element | null} JSX element containing header scripts |
| 115 | + */ |
| 116 | +export const HeaderScripts = ({ scripts }: ScriptsProps) => { |
| 117 | + return <ScriptRenderer location="head" scripts={scripts} strategy="afterInteractive" />; |
| 118 | +}; |
| 119 | + |
| 120 | +/** |
| 121 | + * Renders scripts that should be placed before the closing </body> tag |
| 122 | + * Uses lazyOnload strategy for better performance |
| 123 | + * @param {ScriptsProps} props - The props containing scripts data |
| 124 | + * @param {ScriptsData | null} props.scripts - The scripts data from GraphQL |
| 125 | + * @returns {JSX.Element | null} JSX element containing footer scripts |
| 126 | + */ |
| 127 | +export const FooterScripts = ({ scripts }: ScriptsProps) => { |
| 128 | + return <ScriptRenderer location="footer" scripts={scripts} strategy="lazyOnload" />; |
| 129 | +}; |
0 commit comments