Skip to content

Commit

Permalink
same script warning
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinjude committed May 9, 2022
1 parent 0751d79 commit b5ef811
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/gatsby-script/src/gatsby-script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const handledProps = new Set([
])

export const scriptCache = new Set()
export const scripts = {}

export function Script(props: ScriptProps): ReactElement | null {
const {
Expand Down Expand Up @@ -110,10 +111,27 @@ function injectScript(props: ScriptProps): HTMLScriptElement | null {
onError,
} = props || {}

if (scriptCache.has(id || src)) {
const scriptKey = id || src

if (scriptCache.has(scriptKey)) {
// NO WARNING ON SAME SCRIPT ?? @tyhopp

// @ts-ignore - TBD
if (scripts[scriptKey]?.strategy === strategy) {
console.warn(
`Script ${scriptKey} is already loaded with the same strategy. Consider removing duplicates`
)
}

return null
}

// @ts-ignore - TBD
scripts[scriptKey] = {
src: src,
strategy: strategy,
}

const inlineScript = resolveInlineScript(props)
const attributes = resolveAttributes(props)

Expand Down Expand Up @@ -147,7 +165,7 @@ function injectScript(props: ScriptProps): HTMLScriptElement | null {

document.body.appendChild(script)

scriptCache.add(id || src)
scriptCache.add(scriptKey)

return script
}
Expand Down

0 comments on commit b5ef811

Please sign in to comment.