-
Notifications
You must be signed in to change notification settings - Fork 355
Render scripts from Script Manager #2477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@bigcommerce/catalyst-core": minor | ||
| --- | ||
|
|
||
| Add support for Scripts API/Script Manager scripts rendering via next/script |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # Script Components | ||
|
|
||
| This directory contains components for rendering BigCommerce scripts using Next.js Script component. | ||
|
|
||
| ## Components | ||
|
|
||
| ### ScriptManagerScripts | ||
| A single component that renders scripts with configurable strategy. Location filtering is now handled at the GraphQL level, simplifying the component logic. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```tsx | ||
| import { ScriptManagerScripts } from '~/components/scripts'; | ||
|
|
||
| // In your layout component: | ||
| <head> | ||
| <ScriptManagerScripts | ||
| scripts={data.site.content.headerScripts} | ||
| strategy="afterInteractive" | ||
| /> | ||
| </head> | ||
| <body> | ||
| {children} | ||
| <ScriptManagerScripts | ||
| scripts={data.site.content.footerScripts} | ||
| strategy="lazyOnload" | ||
| /> | ||
| </body> | ||
| ``` | ||
|
|
||
| ## Architecture | ||
|
|
||
| The component accepts: | ||
| - `scripts`: Pre-filtered GraphQL scripts data (headerScripts or footerScripts) | ||
| - `strategy`: 'afterInteractive', 'lazyOnload', or other Next.js Script strategies | ||
|
|
||
| ## Script Processing | ||
|
|
||
| The component handles both types of BigCommerce scripts: | ||
|
|
||
| ### External Scripts (SrcScript) | ||
| ```tsx | ||
| <Script | ||
| id="bc-script-123" | ||
| src="https://example.com/script.js" | ||
| strategy="afterInteractive" | ||
| integrity="sha256-abc123..." | ||
| crossOrigin="anonymous" | ||
| /> | ||
| ``` | ||
|
|
||
| ### Inline Scripts (InlineScript) | ||
| Inline scripts are rendered using `dangerouslySetInnerHTML` for reliable execution: | ||
| ```tsx | ||
| <Script | ||
| id="bc-script-456" | ||
| strategy="lazyOnload" | ||
| dangerouslySetInnerHTML={{ __html: scriptContent }} | ||
| /> | ||
| ``` | ||
|
|
||
| ## Performance & Security | ||
|
|
||
| - Header scripts use `afterInteractive` strategy for critical functionality | ||
| - Footer scripts use `lazyOnload` strategy for better performance | ||
| - Integrity hashes are included when available for security |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,39 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { graphql } from '~/client/graphql'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| export const ScriptsFragment = graphql(` | ||||||||||||||||||||||||||||||||||||||||||||||||
| fragment ScriptsFragment on Content { | ||||||||||||||||||||||||||||||||||||||||||||||||
| headerScripts: scripts( | ||||||||||||||||||||||||||||||||||||||||||||||||
| first: 50 | ||||||||||||||||||||||||||||||||||||||||||||||||
| filters: { visibilities: [ALL_PAGES, STOREFRONT], location: HEAD } | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| ...ScriptTypeConnectionFragment | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| footerScripts: scripts( | ||||||||||||||||||||||||||||||||||||||||||||||||
| first: 50 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3
to
+12
|
||||||||||||||||||||||||||||||||||||||||||||||||
| export const ScriptsFragment = graphql(` | |
| fragment ScriptsFragment on Content { | |
| headerScripts: scripts( | |
| first: 50 | |
| filters: { visibilities: [ALL_PAGES, STOREFRONT], location: HEAD } | |
| ) { | |
| ...ScriptTypeConnectionFragment | |
| } | |
| footerScripts: scripts( | |
| first: 50 | |
| // Default limit for the number of scripts to fetch. Adjust this value as needed. | |
| const DEFAULT_SCRIPT_LIMIT = 50; | |
| export const ScriptsFragment = graphql(` | |
| fragment ScriptsFragment on Content { | |
| headerScripts: scripts( | |
| first: DEFAULT_SCRIPT_LIMIT | |
| filters: { visibilities: [ALL_PAGES, STOREFRONT], location: HEAD } | |
| ) { | |
| ...ScriptTypeConnectionFragment | |
| } | |
| footerScripts: scripts( | |
| first: DEFAULT_SCRIPT_LIMIT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { ScriptManagerScripts } from './scripts'; | ||
| export { ScriptsFragment } from './fragment'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { removeEdgesAndNodes } from '@bigcommerce/catalyst-client'; | ||
| import Script from 'next/script'; | ||
| import { ComponentProps } from 'react'; | ||
|
|
||
| import { FragmentOf } from '~/client/graphql'; | ||
|
|
||
| import { ScriptsFragment } from './fragment'; | ||
|
|
||
| type ScriptsData = FragmentOf<typeof ScriptsFragment>; | ||
|
|
||
| interface ScriptRendererProps { | ||
| scripts: ScriptsData['headerScripts'] | null; | ||
| strategy: ComponentProps<typeof Script>['strategy']; | ||
| } | ||
|
|
||
| export const ScriptManagerScripts = ({ scripts, strategy }: ScriptRendererProps) => { | ||
| if (!scripts?.edges) return null; | ||
|
|
||
| const scriptNodes = removeEdgesAndNodes(scripts); | ||
|
|
||
| return ( | ||
| <> | ||
| {scriptNodes | ||
| .map((script) => { | ||
| const scriptProps: ComponentProps<typeof Script> = { | ||
| strategy, | ||
| }; | ||
|
|
||
| // Handle external scripts (SrcScript) | ||
| if (script.__typename === 'SrcScript' && script.src) { | ||
| scriptProps.src = script.src; | ||
|
|
||
| // Add integrity hashes if provided | ||
| if (script.integrityHashes.length > 0) { | ||
| scriptProps.integrity = script.integrityHashes | ||
| .map((hashObj) => hashObj.hash) | ||
| .join(' '); | ||
| scriptProps.crossOrigin = 'anonymous'; | ||
| } | ||
| } | ||
|
|
||
| // Handle inline scripts (InlineScript) | ||
| if (script.__typename === 'InlineScript' && script.scriptTag) { | ||
| const scriptMatch = /<script[^>]*>([\s\S]*?)<\/script>/i.exec(script.scriptTag); | ||
|
|
||
| if (scriptMatch?.[1]) { | ||
| scriptProps.dangerouslySetInnerHTML = { | ||
| __html: scriptMatch[1], | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| scriptProps.id = `bc-script-${script.entityId}`; | ||
|
|
||
| // Return null for invalid scripts (will be filtered out) | ||
| if (!scriptProps.src && !scriptProps.dangerouslySetInnerHTML) { | ||
| return null; | ||
| } | ||
|
|
||
|
bookernath marked this conversation as resolved.
|
||
| return scriptProps; | ||
| }) | ||
| .filter((scriptProps): scriptProps is ComponentProps<typeof Script> => scriptProps !== null) | ||
| .map((scriptProps) => { | ||
| return <Script key={scriptProps.id} {...scriptProps} />; | ||
| })} | ||
| </> | ||
| ); | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] In the Next.js App Router, it’s conventional to use a self-closing
<head />tag and manage head contents in a dedicatedhead.tsxfile. Consider moving script injections into that file to align with framework conventions.