Skip to content

Commit

Permalink
fix(gatsby-script): Make load callback work when both load and error …
Browse files Browse the repository at this point in the history
…callbacks defined (#35760) (#35787)

Co-authored-by: Ty Hopp <tyhopp@users.noreply.github.com>
  • Loading branch information
gatsbybot and tyhopp authored May 31, 2022
1 parent 9f5c107 commit ae95d66
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 6 deletions.
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

0 comments on commit ae95d66

Please sign in to comment.