|
| 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 Reason for the Restriction Consequences of Misuse Code Context Best Practices
\_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.
\_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.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.
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.\_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.// 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;
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.\_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: Understanding the Error Context Using Custom Scripts in _document © 2026 Based Hardware. All rights reserved.next/script in _document.js
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;
How to Fix Error: next/script is not allowed in the _document file in Next.js