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

Unable to use dangerouslySetInnerHTML and beforeInteractive strategy in Script component #31275

Closed
genetschneider opened this issue Nov 10, 2021 · 7 comments · Fixed by #37000 or #37033
Assignees
Labels
bug Issue was opened via the bug report template. Script (next/script) Related to Next.js Script Optimization.

Comments

@genetschneider
Copy link
Contributor

genetschneider commented Nov 10, 2021

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

@genetschneider genetschneider added the bug Issue was opened via the bug report template. label Nov 10, 2021
@timneutkens timneutkens added the Script (next/script) Related to Next.js Script Optimization. label Nov 15, 2021
@kara kara assigned janicklas-ralph and unassigned kara Nov 29, 2021
@studiosi
Copy link

studiosi commented 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.

@ijjk
Copy link
Member

ijjk commented May 5, 2022

Hi, this has bee updated in the latest version of Next.js v12.1.6, specifically #36364, please updated and give it a try!

@ijjk ijjk closed this as completed May 5, 2022
@alexanderdemchik
Copy link

Hi, this has bee updated in the latest version of Next.js v12.1.6, specifically #36364, please updated and give it a try!

Hi, checked it today. Seems it's still not working with v12.1.6.
Added console log to script:
image

And there is nothing in logs:
image
image

Browser - Chrome 101.0.4951.64

@balazsorban44 balazsorban44 reopened this May 13, 2022
@kodiakhq kodiakhq bot closed this as completed in #37000 May 18, 2022
kodiakhq bot pushed a commit that referenced this issue May 18, 2022
- Fixes #36997
- Fixes #31275

@janicklas-ralph Any idea why tests were passing while this check was failing? Can we add a stronger test for this?
@ijjk
Copy link
Member

ijjk commented May 19, 2022

Hi, this has been updated in v12.1.7-canary.8 of Next.js, please update and give it a try!

@Chastrlove
Copy link
Contributor

Chastrlove commented May 19, 2022

image

v12.1.7-canary.8 Still break; the script has unknow src , and the inline script dont execute @ijjk @housseindjirdeh

@Chastrlove
Copy link
Contributor

the "scriptProps" should omit src
https://github.com/vercel/next.js/blob/canary/packages/next/pages/_document.tsx#L558


updateScripts always post it @ijjk

kodiakhq bot pushed a commit that referenced this issue May 19, 2022
BeforeInteractive inline script in v12.1.7-canary.8  don't run. Beacause the script has unknow src.

![image](https://user-images.githubusercontent.com/17813559/169257330-4419228a-6d10-4815-9451-d9a5dd7f011b.png)

Fixes #31275


## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. Script (next/script) Related to Next.js Script Optimization.
Projects
None yet
9 participants