Unable to use `dangerouslySetInnerHTML` and `beforeInteractive` strategy in Script component
See original GitHub issueWhat 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
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
Issue Analytics
- State:
- Created 2 years ago
- Reactions:7
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.