|

|  Error: next/script is not allowed in the _document file in Next.js: Causes and How to Fix

Error: next/script is not allowed in the _document file in Next.js: Causes and How to Fix

February 10, 2025

Discover the causes of the "next/script not allowed in _document" error in Next.js and learn effective solutions to fix it effortlessly in your projects.

What is Error: next/script is not allowed in the _document file in Next.js

 

Understanding the Error

 

  • In Next.js, the \_document.js file is a custom component that is responsible for updating the HTML document structure that Next.js's server-side rendering produces. It is important to understand that its purpose is to augment the static structure of the document and customize the initial server-side rendering of the HTML.
  •  

  • next/script is a specialized component in Next.js used for loading third-party scripts and managing their execution behavior, such as setting loading strategies. next/script can be powerful for managing scripts dynamically within the application, leveraging Next.js's features to optimize loading and performance.

 

Reason for the Restriction

 

  • The \_document.js file is executed on the server side only. It is rendered only once during the server-side rendering process to create the initial HTML structure, which means it doesn't run on the client side.
  •  

  • In contrast, the next/script component is designed to be used on the client side to manage script loading and execution. It facilitates various optimizations that are specifically client-oriented and therefore is not viable in a server-side rendering context like \_document.js.

 

Consequences of Misuse

 

  • Using next/script inside the \_document.js will lead to a build-time error because this setup does not align with the intended architecture of Next.js.
  •  

  • The server-side nature of \_document.js contradicts the need to manage dynamic scripts that rely on client-side execution and interaction. As such, including next/script in this file would not only result in an error but would defeat the script’s purpose entirely.

 

Code Context

 

// Example of a typical _document.js file structure
import Document, { Html, Head, Main, NextScript } from 'next/document';

class MyDocument extends Document {
  render() {
    return (
      <Html>
        <Head>
          {/* Meta tags and other head elements can be included here */}
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

export default MyDocument;

 

Best Practices

 

  • Always use the next/script component in files such as \_app.js or directly within page components where client-side logic is handled and optimized scripts can be managed effectively.
  •  

  • Keep the \_document.js file minimal and focused on structural elements that are static and essential for server-rendered HTML content optimization.

 

What Causes Error: next/script is not allowed in the _document file in Next.js

 

Understanding the Error: next/script in _document.js

 

  • In Next.js, the `next/script` component is used for managing the loading of third-party scripts efficiently and optimally. It provides features to enable lazy loading, prioritize script execution, and prevent render-blocking, which enhances the performance of web applications.
  •  

  • The `_document.js` file in Next.js is a special file used for customizing the default Document structure of a Next.js application. It is responsible for controlling the initial HTML document markup, including ``, ``, and `` tags. It is only rendered on the server side.
  •  

  • The error "next/script is not allowed in the \_document file" arises due to the logic of separating server-rendered and client-rendered components in Next.js. The `next/script` component is designed to operate with client-side execution, and placing it in `_document.js` conflicts with the server-rendered nature of this file.
  •  

  • In essence, the `next/script` component needs to access the browser's `window` object for operations like lazy loading and executing scripts when they're needed. Since the `_document.js` file is executed in a server-side context, it does not have access to the `window` object, which is only available on the client side. This is one main reason why it is disallowed in this file.
  •  

  • Let's look at a minimal example of `_document.js`, where the use of `next/script` would cause an error:

     

    import { Html, Head, Main, NextScript } from 'next/document';
    import Script from 'next/script';
    
    class MyDocument extends Document {
      render() {
        return (
          <Html>
            <Head>
              <Script src="https://example.com/some-external-script.js" strategy="lazyOnload" />
            </Head>
            <body>
              <Main />
              <NextScript />
            </body>
          </Html>
        );
      }
    }
    
    export default MyDocument;
    

     

  • In the example above, placing `

    Omi Dev Kit 2

    Endless customization

    OMI DEV KIT 2

    $69.99

    Make your life more fun with your AI wearable clone. It gives you thoughts, personalized feedback and becomes your second brain to discuss your thoughts and feelings. Available on iOS and Android.

    Your Omi will seamlessly sync with your existing omi persona, giving you a full clone of yourself – with limitless potential for use cases:

    • Real-time conversation transcription and processing;
    • Develop your own use cases for fun and productivity;
    • Hundreds of community apps to make use of your Omi Persona and conversations.

    Learn more

Omi Dev Kit 2: build at a new level

Key Specs

OMI DEV KIT

OMI DEV KIT 2

Microphone

Yes

Yes

Battery

4 days (250mAH)

2 days (250mAH)

On-board memory (works without phone)

No

Yes

Speaker

No

Yes

Programmable button

No

Yes

Estimated Delivery 

-

1 week

What people say

“Helping with MEMORY,

COMMUNICATION

with business/life partner,

capturing IDEAS, and solving for

a hearing CHALLENGE."

Nathan Sudds

“I wish I had this device

last summer

to RECORD

A CONVERSATION."

Chris Y.

“Fixed my ADHD and

helped me stay

organized."

David Nigh

OMI NECKLACE: DEV KIT
Take your brain to the next level

LATEST NEWS
Follow and be first in the know

Latest news
FOLLOW AND BE FIRST IN THE KNOW

thought to action

team@basedhardware.com

company

careers

invest

privacy

events

vision

products

omi

omi dev kit

omiGPT

personas

omi glass

resources

apps

bounties

affiliate

docs

github

help