|
| 1 | +# Script Components |
| 2 | + |
| 3 | +This directory contains components for rendering BigCommerce scripts using Next.js Script component. |
| 4 | + |
| 5 | +## Components |
| 6 | + |
| 7 | +Both `HeaderScripts` and `FooterScripts` are exported from a single `scripts.tsx` file that follows DRY principles by sharing common logic through an internal `ScriptRenderer` component. |
| 8 | + |
| 9 | +### HeaderScripts |
| 10 | +Renders scripts that should be placed in the `<head>` tag with `strategy="afterInteractive"`. |
| 11 | + |
| 12 | +### FooterScripts |
| 13 | +Renders scripts that should be placed before the closing `</body>` tag with `strategy="lazyOnload"` for better performance. |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +```tsx |
| 18 | +import { HeaderScripts, FooterScripts } from '~/components/scripts'; |
| 19 | + |
| 20 | +// In your layout or page component: |
| 21 | +<HeaderScripts scripts={data.site.content.scripts} /> |
| 22 | +<FooterScripts scripts={data.site.content.scripts} /> |
| 23 | +``` |
| 24 | + |
| 25 | +## Architecture |
| 26 | + |
| 27 | +The components use a shared `ScriptRenderer` component that accepts: |
| 28 | +- `scripts`: The GraphQL scripts data |
| 29 | +- `location`: 'head' or 'footer' |
| 30 | +- `strategy`: 'afterInteractive' or 'lazyOnload' |
| 31 | + |
| 32 | +This ensures consistent behavior and reduces code duplication between header and footer scripts. |
| 33 | + |
| 34 | +## Script Processing |
| 35 | + |
| 36 | +The components handle both types of BigCommerce scripts: |
| 37 | + |
| 38 | +### External Scripts (SrcScript) |
| 39 | +```tsx |
| 40 | +<Script |
| 41 | + id="bc-script-123" |
| 42 | + src="https://example.com/script.js" |
| 43 | + strategy="afterInteractive" |
| 44 | + integrity="sha256-abc123..." |
| 45 | +/> |
| 46 | +``` |
| 47 | + |
| 48 | +### Inline Scripts (InlineScript) |
| 49 | +Inline scripts are rendered using `dangerouslySetInnerHTML` for reliable execution: |
| 50 | +```tsx |
| 51 | +<Script |
| 52 | + id="bc-script-456" |
| 53 | + strategy="lazyOnload" |
| 54 | + dangerouslySetInnerHTML={{ __html: scriptContent }} |
| 55 | +/> |
| 56 | +``` |
| 57 | + |
| 58 | +## Filtering Logic |
| 59 | + |
| 60 | +Scripts are filtered by: |
| 61 | +- **Location**: BigCommerce API returns `'HEAD'` or `'FOOTER'` (uppercase) |
| 62 | +- **Visibility**: Only renders scripts with `'ALL_PAGES'` or `'STOREFRONT'` visibility |
| 63 | + |
| 64 | +## Performance |
| 65 | + |
| 66 | +- Header scripts use `afterInteractive` strategy for critical functionality |
| 67 | +- Footer scripts use `lazyOnload` strategy for better performance |
| 68 | +- Script IDs are generated using the `entityId` from the BigCommerce API |
| 69 | +- Integrity hashes are included when available for security |
| 70 | +- Scripts are efficiently filtered and rendered based on location and visibility |
| 71 | + |
| 72 | +## GraphQL Integration |
| 73 | + |
| 74 | +Uses the `ScriptsFragment` to fetch script data: |
| 75 | +```graphql |
| 76 | +fragment ScriptsFragment on Content { |
| 77 | + scripts(first: 50, filters: { visibilities: [ALL_PAGES, STOREFRONT] }) { |
| 78 | + edges { |
| 79 | + node { |
| 80 | + integrityHashes { hash } |
| 81 | + entityId |
| 82 | + consentCategory |
| 83 | + location |
| 84 | + visibility |
| 85 | + ... on InlineScript { scriptTag } |
| 86 | + ... on SrcScript { src } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
0 commit comments