question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HeadManager removes elements added by non-next scripts

See original GitHub issue

Bug report

Describe the bug

After upgrading from Next 8 to Next 9, charts embedded from https://infogram.com/ stopped working for us.

My investigation shows that Infogram’s loader adds a <script> element to <head> which is then removed by next’s HeadManager. This causes the Infogram script to fail.

Infogram’s embedding scripts expects the ability to:

  1. Add a new <script ... /> right before the currently first <script .../> in the DOM.
  2. Find this element (by id) at any point later.

To Reproduce

  1. Clone https://github.com/jarib/next-js-9-embed-bug
  2. Run npm install && npm run dev
  3. Visit http://localhost:3000 for a minimal example, check the console.
  4. Visit http://localhost:3000/infogram for a full example (using Infogram), check the console.

Expected behavior

The console should show “all ok” for step 3 and render an Infogram chart for step 4. Instead, the added script element is removed. The script being loaded assumes that the script element that loaded it exists in the DOM, and when it cannot find it, it fails.

You can get the expected behaviour by removing the initial script element from <Head> in both examples, or by downgrading to Next 8 (and moving public/ to static/).

System information

  • OS: macOs
  • Browser (if applies): Chrome
  • Version of Next.js: 9.5.2, 10.0.2, 10.0.4-canary.8, 10.0.5 and 10.0.6-canary.9
  • The bug does not reproduce on Next.js 9.5.4, 9.5.5, 10.0.0, 10.0.1

Additional context

Since the embed codes unfortunately are saved as HTML in our CMS, we use createContextualFragment to load it, including the script tag. This has served us well so far, including for client-side navigation, and covers our embed use cases outside of Infogram as well. It also worked perfectly in Next 8.

Possibly related to https://github.com/zeit/next.js/issues/9573

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jaribcommented, Dec 20, 2020

Bug does not reproduce on 9.5.4, 9.5.5, 10.0.0, 10.0.1 Bug does reproduce on 9.5.2, 10.0.2, 10.0.4-canary.8

1reaction
jaribcommented, Dec 20, 2020

@Timer That issue is closed as fixed, but my reprodcution repo still fails using 10.0.4-canary.8.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Upgrade Guide - Next.js
Upgrade Guide. Upgrading from 12 to 13. To update to Next.js version 13, run the following command using your preferred package manager:
Read more >
Remove specific <script> tag in <head> tag by onclick event
So any action started within that <script> tag will prevail, even if you delete the element, that started it in the first place!...
Read more >
The Noscript element - HTML: HyperText Markup Language
The HTML element defines a section of HTML to be inserted if a script type on the page is unsupported or if scripting...
Read more >
4.3.2 The noscript element — HTML5: Edition for Web Authors
When scripting is disabled, not in a head element: transparent, but there must ... The noscript element's content model is transparent, with the...
Read more >
Class UnsupportedElement | Apps Script - Google Developers
removeFromParent(), UnsupportedElement, Removes the element from its parent. ... Merge the newly added paragraphs into a single paragraph.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found