question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`next/script` not working in custom `Document` when outside of `next/document`'s `<Head>`

See original GitHub issue

Verify 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

  1. Initialize a next project
  2. Add the example code from the docs in _document.js
  3. Run and check DevTools > Network
  4. 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:closed
  • Created a year ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

6reactions
housseindjirdehcommented, Jun 16, 2022

There’s an issue currently that requires beforeInteractive scripts to live inside <Head> in _document.js:

import { Html, Head, Main, NextScript } from 'next/document'
import Script from 'next/script'

export default function Document() {
  return (
    <Html>
      <Head>
       <Script
         src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"
         strategy="beforeInteractive"
       ></Script>
      </Head>
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  )
}

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 😃

2reactions
housseindjirdehcommented, Jun 16, 2022

@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 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found