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

Allow inline-scripts with beforeInteractive strategy #34610

Closed
pacocoursey opened this issue Feb 20, 2022 · 6 comments
Closed

Allow inline-scripts with beforeInteractive strategy #34610

pacocoursey opened this issue Feb 20, 2022 · 6 comments

Comments

@pacocoursey
Copy link
Contributor

pacocoursey commented Feb 20, 2022

Describe the feature you'd like to request

My library next-themes injects an inline script that must at the start of page load to avoid any color flashing. This means I must use the beforeInteractive strategy in next/script.

However, beforeInteractive does not support inline scripts:

I'd like to request that inline scripts should be supported in next/script with strategy="beforeInteractive".

Describe the solution you'd like

Allow passing dangerouslySetInnerHTML or children alongside usages of the script component with strategy="beforeInteractive".

Implementation strategy:

  1. Pass dangerouslySetInnerHTML or children to scripts.beforeInteractive on this line
  2. It should automatically be passed to the rendered script tag when it's inserted via the _document

Describe alternatives you've considered

  1. Use an external script. Not possible because the value of my inline script is constructed at runtime based on the Next.js page props.
  2. Base64 encoding the script as the src prop. This is what I'm currently doing, but it means we cannot have a stable, secure way to allow the script via CSP.
@pacocoursey
Copy link
Contributor Author

Oh, it seems children is already being passed, so that will work. I'll keep this open as I continue testing.

It seems the documentation is inaccurate then. External scripts are not "inlined" either - at least my understanding of inlining would be fetching the remote script text and embedding it directly in the HTML to avoid a network fetch, which I don't see happening at all.

@balazsorban44
Copy link
Member

This use case was purposefully left out from the initial implementation.

#26343 (comment)

As such, this sounds like a duplicate of #26343. Do you agree?

@pacocoursey
Copy link
Contributor Author

Yep, happy to close as duplicate.

@stefanprobst
Copy link
Contributor

@pacocoursey you could also just move to a regular script tag in _document as suggested in the react 18 streaming docs.

@pacocoursey
Copy link
Contributor Author

Yeah, that's what I'm planning to do 👍 (pacocoursey/next-themes#89)

@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 Mar 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants