rsnext/errors/no-script-in-document-page.md
Jesse Jafa a28e775e88
[ESLint] Disallow <Script /> inside _document.js & <Script /> inside the next/head component (#27257)
## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. [Feature Request](https://github.com/vercel/next.js/discussions/26365)
- [x] Eslint unit ests added
- [x] Errors have helpful link attached, see `contributing.md`

Let me know if this looks good or something needs to be changed. I still need to add the error links and improve the eslint error messages.

I don't know if the CI runs the ESLint tests, but current all pass locally
2021-08-13 23:16:15 +00:00

727 B

Script component inside _document.js

Why This Error Occurred

You can't use the next/script component inside the _document.js page. That's because the _document.js page only runs on the server and next/script has client-side functionality to ensure loading order.

Possible Ways to Fix It

If you want a global script, instead use the _app.js page.

import Script from 'next/script'

function MyApp({ Component, pageProps }) {
  return (
    <>
      <Script src="/my-script.js" />
      <Component {...pageProps} />
    </>
  )
}

export default MyApp