-
Notifications
You must be signed in to change notification settings - Fork 27k
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
[NEXT-1304] <Script> with beforeInteractive
strategy ignores additional attributes in app router
#49830
Comments
I just ran into this issue with 13.4.4.
I was quite surprised about this. The |
Next 13.4.4 Debugging half a day, I have several points to add. All with App Directory:
|
@prepwork Hm, I just tried this out on 13.4.5, including the workaround (see victorandree/next-script-before-interactive-attributes-app-dir@3140d39).
For reference, this is what the start of my server-rendered HTML looks like (when running HEAD of my reproduction repository using <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="preload" as="script" href="https://example2.com/script2.js" />
<link rel="preload" as="script" href="https://example.com/script.js" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://example2.com/script2.js" data-test="script2"></script>
<script
src="/_next/static/chunks/polyfills-78c92fac7aa8fdd8.js"
nomodule=""
></script>
</head>
<body>
<script>
(self.__next_s = self.__next_s || []).push([
'https://example.com/script.js',
]);
</script> |
@victorandree, I've tried your project, and indeed, scripts are executing after being processed by "use client"
import { useSearchParams } from "next/navigation"
export default function Home() {
const searchParams = useSearchParams()
return <div>HOME PAGE</div>
} Just comment out line with "use client"
import { useSearchParams } from "next/navigation"
export default function Home() {
// const searchParams = useSearchParams()
return <div>HOME PAGE</div>
} Have no clue why it is so. |
@prepwork I tried this out, and if I add
I can verify that when this happens, the IMO, this is technically another bug - fundamentally the same issue as #48335 (which is about static rendering of meta tags). The workaround there is to wrap |
@victorandree, thanks a lot! It helped me greatly! |
beforeInteractive
strategy ignores additional attributes in app routerbeforeInteractive
strategy ignores additional attributes in app router
@prepwork i dont know if this has to do with a recent change, but you should also consider this from nextJS https://nextjs.org/docs/messages/no-before-interactive-script-outside-document Seems to be not really a bug, its just not intended that way |
with regard to this, is this still relevant when using the app router? it seems to me like something that just hasn't been updated yet as it seems conflicting to try and include a |
@desnor @prepwork I agree that this isn't relevant for the app router. I don't see that warning when using the app router. The app router docs for FWIW, I've updated my reproduction repository to [email protected] and the issue is still present. |
This seems to be a bug. Official docs for /app https://nextjs.org/docs/app/api-reference/components/script#beforeinteractive says that it's before and after interactive is supported from next 13.0.0 but not for build script. Proposed fix on docs for /app https://nextjs.org/docs/messages/no-before-interactive-script-outside-document is not relevant because it's conflicting with /app/layout.tsx and not working. The only workaround that I was able to make work to use standard html script tag and use defer on loading js script:
|
Hello ! Same issue here trying to add OneTrust script with beforeInteractive strategy. Did you add the OneTrust Cookie List to your website ? On my side, trying to add |
Experiencing the same issue on 13.5.6. |
Confirmed still not working as advertised in Following the documentation this should not happen:
|
Can confirm this is happening in 14 for me as well. |
I'm on |
I've created a PR to fix this issue. I've tested locally and can confirm this solves the problem. Now just waiting on Next.js folks to review. |
…attributes in App Router (#59779) ### What? Currently, `next/script` in the App Router does not behave as the docs describe in the [Additional Attributes](https://nextjs.org/docs/app/building-your-application/optimizing/scripts#additional-attributes) section – it does not pass on all additional attributes when `strategy="beforeInteractive"` ([#49830](#49830)). ### Why? The props from the `<Script>` component were not passed on to the underlying script loader (`self.__next_s`). ### How? I've added the missing props to the object that is `push`ed to `self.__next_s`. NEXT-1304 Fixes #49830
…attributes in App Router (vercel#59779) ### What? Currently, `next/script` in the App Router does not behave as the docs describe in the [Additional Attributes](https://nextjs.org/docs/app/building-your-application/optimizing/scripts#additional-attributes) section – it does not pass on all additional attributes when `strategy="beforeInteractive"` ([vercel#49830](vercel#49830)). ### Why? The props from the `<Script>` component were not passed on to the underlying script loader (`self.__next_s`). ### How? I've added the missing props to the object that is `push`ed to `self.__next_s`. NEXT-1304 Fixes vercel#49830
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Note: Updated on August 24, 2023, with latest version of Next.js.
Verify canary release
Provide environment information
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true), Script optimization (next/script)
Link to the code that reproduces this issue
https://github.com/victorandree/next-script-before-interactive-attributes-app-dir
To Reproduce
Setup a project with the app directory and in the root layout, use
<Script>
withstrategy="beforeInteractive"
and pass additional attributes. The additional attributes are not included in the resulting script tag.Describe the Bug
When using the
<Script>
tag fromnext/script
in the root layout of the app directory, additional attributes are not included if you usestrategy="beforeInteractive"
:A second point is that the
<script>
tag is not part of the HTML from the server, but is only added as (1) a<link rel="preload">
tag and (2) to aself.__next_s
array and (3) as part of server component 'chunks', before it's rendered into the DOM.Including such a script tag in
pages/_document
will render it with additional attributes (not reproduced) and as a script tag "directly". This issue therefore only affects the app directory.A possible workaround is to include the
script
tags directly inhead
of your root layout:For visibility, I encountered this while trying to install the OneTrust cookie consent snippet on a Next.js site, which requires passing an identifier as
data-domain-script
to ascript
tag.Expected Behavior
The documented behavior of the
<Script>
tag is that it includes additional attributes (see "Additional Attributes" on the "Script Optimization" page of the App router documentation).I also think it's a bit strange that the script tag is not rendered directly to the HTML from the server, since "before interactive" scripts are typically used for consent management (that's the example use case provided by the documentation), where it's important that they're inserted early.
I'd expect additional attributes to be included, even if I use it in the app directory.
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
NEXT-1304
The text was updated successfully, but these errors were encountered: