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.

Unable to use `dangerouslySetInnerHTML` and `beforeInteractive` strategy in Script component

See original GitHub issue

What version of Next.js are you using?

14.17.1

What version of Node.js are you using?

12.0.3

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

npm run dev

Describe the Bug

Setting dangerouslySetInnerHtml in next Script component while also specifying beforeInteractive strategy will result in the Script component doing nothing. Since there is no src specified, the src will simply be (unknown). script

<Script
  id="test"
  strategy="beforeInteractive"
  dangerouslySetInnerHTML={{
    // will not run
    __html: `console.log('hello!')`,
  }}
/>

Expected Behavior

Script component can be used with no src while using dangerouslySetInnerHtml property.

To Reproduce

Try to render Script component using beforeInteractive while having no url and specifying dangerouslySetInnerHtml property.

<Script
  id="test"
  strategy="beforeInteractive"
  dangerouslySetInnerHTML={{
    // will not run
    __html: `console.log('hello!')`,
  }}
/>

This piece of code of next Script will result in the following script tag being added script

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
studiosicommented, May 5, 2022

Has anyone found a workaround for this?

What I currently do is placing the javascript on a file on the public folder and load it from there, but it does not allow me to use environment variables in order to change parameters.

0reactions
github-actions[bot]commented, Jun 18, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Script tag is not loading "dangerouslySetInnerHTML" content
I have put that code in _app.js file like mentioned here. By running code, I am not able to view the code which...
Read more >
Basic Features: Handling Scripts - Next.js
The Script component, next/script , allows you to optimally load third-party scripts anywhere in your Next.js application. It is an extension of the...
Read more >
How to Embed a Script in NextJS - The Engineer VC
You can't use the beforeInteractive strategy with inline scripts. You must provide an id to allow Next.js to track and optimise the script...
Read more >
NextJS Core Web Vitals - fix third party scripts
With next/script, you decide when to load your third-party script by using the strategy property: There are three different loading strategies that can...
Read more >
Optimizing third-party script loading in Next.js
This article discusses the Next.js Script component and demonstrates how it ... unless they explicitly apply the beforeInteractive strategy.
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