13

I am using nextJs version 11.x

When trying to include an external script like below, getting an error when executing the yarn build.

<Head>
<link rel="stylesheet" type="text/css" href="http://www.externalsite.com/style.css" />
<script src="https://www.websote.com/viewer.min.js"></script>
</Head>

Error :

./pages/_app.js
63:17  Error: External synchronous scripts are forbidden. See: https://nextjs.org/docs/messages/no-sync-scripts.  @next/next/no-sync-scripts

I am using eslint.

So, how can we include this external js ?

3
  • try using Script component nextjs.org/docs/basic-features/script Commented Nov 10, 2021 at 12:31
  • Thanks for replying... i tried the same but no luck :( Commented Nov 10, 2021 at 12:55
  • 1
    Issue solved by keeping the eslint validation to "extends": "next" Commented Nov 11, 2021 at 5:52

5 Answers 5

20

Resolved the problem.

It was happening because of eslint strict configuration. Now I changed this to Base.

Modified content in this file .eslintrc

Earlier the value was

   {
   "extends": "next/core-web-vitals"
   }

Now changed the content to

 {
 "extends": "next"
 }

Thanks for the Help

Sign up to request clarification or add additional context in comments.

3 Comments

Yes' It work for me , i have modify the file .eslintrc.json with the same . it working thanks.
Worked for me in 2022!
To clarify, this works because the next ESLint configuration is less restrictive than the next/core-web-vitals one, and does not enable the rule @next/next/no-sync-scripts which is what triggered the error.
11
import Script from 'next/script'

const Home = () => {
    return (
      <div class="container">
        <Script src="https://third-party-script.js"></Script>
        <div>Home Page</div>
      </div>
    )
}

export default Home

3 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
Here's more details about it: nextjs.org/docs/messages/no-sync-scripts
this is the wrong answer, because this is client side.
5

Modify ESLint configuration file, disable the no-sync-scripts rule mentioned by the error message works.

{
  "extends": "next/core-web-vitals",
  "rules": {
    "@next/next/no-sync-scripts": "off"
  }
}

Comments

3

If you are using <script></script> inside _document.js use async or defer keyword

        <script
            defer
            src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
            integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
            crossOrigin="anonymous"
       ></script>

          <script
            async
            src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
            integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN"
            crossOrigin="anonymous"
          ></script>

If in your component, use this

import Script from 'next/script'

1 Comment

Nice tips: Worked with <script defer src="xxxxxx" for me to load Google Places script in _document.ts in Nextjs. Thanks! PS. <Script> from next/script didnt worked for Google Places...
1

For nextjs, Instead of using use imported from next/script.

An example is given below :

import Script from 'next/script';

Use async or defer

<Script src="https://third-party-script.js" async />
<Script src="https://third-party-script.js" defer />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.