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): e.remove() is not a function when using Gatsby Head API #36338

Merged
merged 4 commits into from
Aug 11, 2022

Conversation

marvinjude
Copy link
Contributor

@marvinjude marvinjude commented Aug 8, 2022

Description

Adding a custom babel config with the loose option set to false causes some conversion that leads the error below

One unhandled runtime error found in your files. See the list below to fix it:

Error in function eval in ./.cache/head/head-export-handler-for-browser.js:18
e.remove is not a function

./.cache/head/head-export-handler-for-browser.js:18
Open in Editor
16 | const removePrevHeadElements = () => {
17 | const prevHeadNodes = [...document.querySelectorAll([data-gatsby-head])]

18 | prevHeadNodes.forEach(e => e.remove())
| ^
19 | }
20 |
21 | const onHeadRendered = () => {`

This PR fixes it by directly iterating over the NodeLists without creating new arrays and removing unnecessary spread operators

Also address issue with IE not having .remove() by using node.parentNode.removeChild(node)

Documentation

https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-head/

Related Issues

Fixes #36247

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Aug 8, 2022
@marvinjude marvinjude changed the title fix(gatsby): fix e.remove is not a function when using Head API fix(gatsby): e.remove is not a function when using Head API Aug 8, 2022
@marvinjude marvinjude changed the title fix(gatsby): e.remove is not a function when using Head API fix(gatsby): e.remove is not a function when using Gatsby Head API Aug 8, 2022
@marvinjude marvinjude changed the title fix(gatsby): e.remove is not a function when using Gatsby Head API fix(gatsby): e.remove() is not a function when using Gatsby Head API Aug 8, 2022
@marvinjude marvinjude marked this pull request as ready for review August 8, 2022 13:47
@marvinjude marvinjude added type: bug An issue or pull request relating to a bug in Gatsby and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Aug 8, 2022
@@ -15,7 +15,7 @@ import {
const hiddenRoot = document.createElement(`div`)

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break in older browser versions where nodeList does not contain forEach. You'll have to figure out the issue differently. What exactly is the issue with [...]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is the issue with [...]?

#36247 (comment)

Good catch 👍 I'd do a for...of instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you change it into Array.from ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array.from works fine — turns out we didn't even need to create new arrays in the first place

@LekoArts LekoArts added topic: DX Developer Experience (e.g. Fast Refresh, i18n, SSR, page creation, starters) and removed type: bug An issue or pull request relating to a bug in Gatsby labels Aug 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: DX Developer Experience (e.g. Fast Refresh, i18n, SSR, page creation, starters)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants