|

|  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 開発キット 2

    無限のカスタマイズ

    OMI 開発キット 2

    $69.99

    Omi AIネックレスで会話を音声化、文字起こし、要約。アクションリストやパーソナライズされたフィードバックを提供し、あなたの第二の脳となって考えや感情を語り合います。iOSとAndroidでご利用いただけます。

    • リアルタイムの会話の書き起こしと処理。
    • 行動項目、要約、思い出
    • Omi ペルソナと会話を活用できる何千ものコミュニティ アプリ

    もっと詳しく知る

Omi Dev Kit 2: 新しいレベルのビルド

主な仕様

OMI 開発キット

OMI 開発キット 2

マイクロフォン

はい

はい

バッテリー

4日間(250mAH)

2日間(250mAH)

オンボードメモリ(携帯電話なしで動作)

いいえ

はい

スピーカー

いいえ

はい

プログラム可能なボタン

いいえ

はい

配送予定日

-

1週間

人々が言うこと

「記憶を助ける、

コミュニケーション

ビジネス/人生のパートナーと、

アイデアを捉え、解決する

聴覚チャレンジ」

ネイサン・サッズ

「このデバイスがあればいいのに

去年の夏

記録する

「会話」

クリスY.

「ADHDを治して

私を助けてくれた

整頓された。"

デビッド・ナイ

OMIネックレス:開発キット
脳を次のレベルへ

最新ニュース
フォローして最新情報をいち早く入手しましょう

最新ニュース
フォローして最新情報をいち早く入手しましょう

thought to action.

Based Hardware Inc.
81 Lafayette St, San Francisco, CA 94103
team@basedhardware.com / help@omi.me

Company

Careers

Invest

Privacy

Events

Manifesto

Compliance

Products

Omi

Wrist Band

Omi Apps

omi Dev Kit

omiGPT

Personas

Omi Glass

Resources

Apps

Bounties

Affiliate

Docs

GitHub

Help Center

Feedback

Enterprise

Ambassadors

Resellers

© 2025 Based Hardware. All rights reserved.