Skip to content
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

fix(gatsby-script): Make load callback work when both load and error callbacks defined (#35760) #35787

Merged
merged 1 commit into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
describe(`both onLoad and onError callbacks are declared`, () => {
beforeEach(() => {
cy.visit(`/gatsby-script-both-callbacks/`).waitForRouteChange()
})

it(`should execute the onLoad callback`, () => {
cy.get(`[data-on-load-result=both-callbacks]`).should(`have.length`, 1)
})

it(`should execute the onError callback`, () => {
cy.get(`[data-on-error-result=both-callbacks]`).should(`have.length`, 1)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from "react"
import { Script } from "gatsby"
import { scripts } from "../../gatsby-script-scripts"
import { onLoad, onError } from "../utils/gatsby-script-callbacks"

const BothCallbacksPage = () => {
return (
<main>
<h1>Script component e2e test</h1>

<Script
src={scripts.marked}
onLoad={() => {
onLoad(`both-callbacks`)
}}
onError={() => {}}
/>

<Script
src="/non-existent-script.js"
onLoad={() => {}}
onError={() => {
onError(`both-callbacks`)
}}
/>
</main>
)
}

export default BothCallbacksPage
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Script } from "gatsby"
import { scripts } from "../../gatsby-script-scripts"
import { onLoad, onError } from "../utils/gatsby-script-callbacks"

const DuplicateScripts = () => {
const DuplicateScriptsPage = () => {
const [onLoadScriptLoaded, setOnLoadScriptLoaded] = useState(false)
const [onErrorScriptLoaded, setOnErrorScriptLoaded] = useState(false)
const [secondOnLoadScriptLoaded, setSecondOnLoadScriptLoaded] = useState(
Expand Down Expand Up @@ -98,4 +98,4 @@ const DuplicateScripts = () => {
)
}

export default DuplicateScripts
export default DuplicateScriptsPage
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
describe(`both onLoad and onError callbacks are declared`, () => {
beforeEach(() => {
cy.visit(`/gatsby-script-both-callbacks/`).waitForRouteChange()
})

it(`should execute the onLoad callback`, () => {
cy.get(`[data-on-load-result=both-callbacks]`).should(`have.length`, 1)
})

it(`should execute the onError callback`, () => {
cy.get(`[data-on-error-result=both-callbacks]`).should(`have.length`, 1)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from "react"
import { Script } from "gatsby"
import { scripts } from "../../gatsby-script-scripts"
import { onLoad, onError } from "../utils/gatsby-script-callbacks"

const BothCallbacksPage = () => {
return (
<main>
<h1>Script component e2e test</h1>

<Script
src={scripts.marked}
onLoad={() => {
onLoad(`both-callbacks`)
}}
onError={() => {}}
/>

<Script
src="/non-existent-script.js"
onLoad={() => {}}
onError={() => {
onError(`both-callbacks`)
}}
/>
</main>
)
}

export default BothCallbacksPage
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Script } from "gatsby"
import { scripts } from "../../gatsby-script-scripts"
import { onLoad, onError } from "../utils/gatsby-script-callbacks"

const DuplicateScripts = () => {
const DuplicateScriptsPage = () => {
const [onLoadScriptLoaded, setOnLoadScriptLoaded] = useState(false)
const [onErrorScriptLoaded, setOnErrorScriptLoaded] = useState(false)

Expand Down Expand Up @@ -58,4 +58,4 @@ const DuplicateScripts = () => {
)
}

export default DuplicateScripts
export default DuplicateScriptsPage
4 changes: 2 additions & 2 deletions packages/gatsby-script/src/gatsby-script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,17 @@ function injectScript(props: ScriptProps): IInjectedScriptDetails | null {
* If a duplicate script is already loaded/errored, we replay load/error callbacks with the original event.
* If it's not yet loaded/errored, keep track of callbacks so we can call load/error callbacks for each when the event occurs.
*/
const cachedCallbacks = scriptCallbackCache.get(scriptKey) || {}

for (const name of callbackNames) {
if (currentCallbacks?.[name]) {
const cachedCallbacks = scriptCallbackCache.get(scriptKey) || {}
const { callbacks = [] } = cachedCallbacks?.[name] || {}
callbacks.push(currentCallbacks?.[name])

if (cachedCallbacks?.[name]?.event) {
currentCallbacks?.[name]?.(cachedCallbacks?.[name]?.event)
} else {
scriptCallbackCache.set(scriptKey, {
...cachedCallbacks,
[name]: {
callbacks,
},
Expand Down