Skip to content

Commit

Permalink
fix(gatsby): e.remove() is not a function when using Gatsby Head API (#…
Browse files Browse the repository at this point in the history
…36338)

* remove useless new array creation

* use for..of over forEach for browser compatibility

* use parentNode.removeChild() for compatibility with IE

* fix error when parentNode is null
  • Loading branch information
marvinjude authored Aug 11, 2022
1 parent 25fb9d1 commit 7fcf580
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions packages/gatsby/cache-dir/head/head-export-handler-for-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import {
const hiddenRoot = document.createElement(`div`)

const removePrevHeadElements = () => {
const prevHeadNodes = [...document.querySelectorAll(`[data-gatsby-head]`)]
prevHeadNodes.forEach(e => e.remove())
const prevHeadNodes = document.querySelectorAll(`[data-gatsby-head]`)

for (const node of prevHeadNodes) {
node.parentNode.removeChild(node)
}
}

const onHeadRendered = () => {
Expand Down Expand Up @@ -49,7 +52,9 @@ const onHeadRendered = () => {
seenIds.set(id, validHeadNodes.length - 1)
} else {
const indexOfPreviouslyInsertedNode = seenIds.get(id)
validHeadNodes[indexOfPreviouslyInsertedNode].remove()
validHeadNodes[indexOfPreviouslyInsertedNode].parentNode?.removeChild(
validHeadNodes[indexOfPreviouslyInsertedNode]
)
validHeadNodes[indexOfPreviouslyInsertedNode] = clonedNode
}
} else {
Expand All @@ -58,9 +63,7 @@ const onHeadRendered = () => {
}
}

const existingHeadElements = [
...document.querySelectorAll(`[data-gatsby-head]`),
]
const existingHeadElements = document.querySelectorAll(`[data-gatsby-head]`)

if (existingHeadElements.length === 0) {
document.head.append(...validHeadNodes)
Expand All @@ -71,7 +74,7 @@ const onHeadRendered = () => {
diffNodes({
oldNodes: existingHeadElements,
newNodes: validHeadNodes,
onStale: node => node.remove(),
onStale: node => node.parentNode.removeChild(node),
onNew: node => newHeadNodes.push(node),
})

Expand Down

0 comments on commit 7fcf580

Please sign in to comment.