`next/script` not working in custom `Document` when outside of `next/document`'s `<Head>`
See original GitHub issueVerify canary release
- I verified that the issue exists in Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:22 PDT 2022; root:xnu-8020.121.3~4/RELEASE_X86_64
Binaries:
Node: 16.15.0
npm: 8.12.2
Yarn: 1.22.18
pnpm: 7.2.1
Relevant packages:
next: 12.1.7-canary.40
react: 18.2.0
react-dom: 18.2.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
next start
Describe the Bug
I am trying to get <Script strategy="beforeInteractive" />
to work, I did exactly what the docs say, but the script is not present in the built index.html
nor working in next dev
.
Expected Behavior
<Script strategy="beforeInteractive" />
to work.
To Reproduce
- Initialize a next project
- Add the example code from the docs in
_document.js
- Run and check
DevTools > Network
- No request will be found to
https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js
Or from a shell terminal:
cd /tmp && npx create-next-app tmp && cd tmp
tee pages/_document.js <<'EOF' >/dev/null
import { Html, Head, Main, NextScript } from 'next/document'
import Script from 'next/script'
export default function Document() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
<Script
src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"
strategy="beforeInteractive"
></Script>
</body>
</Html>
)
}
EOF
yarn add next@canary && yarn dev
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5
Top Results From Across the Web
Script tag not working · Discussion #35064 · vercel/next.js
Unfortunately no scripts seem to load when I stick them inside my custom _document.tsx, whether that is inside of the Head or outside,...
Read more >Advanced Features: Custom `Document` - Next.js
import { Html, Head, Main, NextScript } from 'next/document' export default ... React components outside of <Main /> will not be initialized by...
Read more >Next 11 and adding Script tags not working. No scripts are ...
None of the scripts I am loading via the new "Script" tag are working. <Head> <meta name="viewport" content="width=device-width,initial-scale=1, ...
Read more >_document.js in Next.js: Master Next.js Tutorial #6 - YouTube
Learn about _document.js / _document.tsx file in Next.js, what it can do and how you can work with it.Full Next.js learning path: ...
Read more >How to add dark mode toggle to Next.js application using ...
In Next.js there is a way we can create our own custom document. ... import NextDocument, { Html, Head, Main, NextScript } from ......
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
There’s an issue currently that requires
beforeInteractive
scripts to live inside<Head>
in_document.js
:I’ll submit a PR next week that removes this restriction and allow you to place
<Script>
anywhere in_document.js
, but you can move it to<Head>
for the meantime to make it work 😃@aboqasem Go for it! I was meaning to work on it next week but will be glad to help you and review your PR instead 😃