{"record":{"id":"811857be260dd594","repo":"oxc-project/oxc","slug":"document-from-next-document-should-not-be","errorCode":null,"errorMessage":"`<Document />` from `next/document` should not be imported outside of `pages/_document.js`. See: https://nextjs.org/docs/messages/no-document-import-in-page","messagePattern":"`<Document />` from `next/document` should not be imported outside of `pages/_document\\.js`\\. See: https://nextjs\\.org/docs/messages/no-document-import-in-page","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/nextjs/no_document_import_in_page.rs","lineNumber":14,"sourceCode":"use oxc_ast::AstKind;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{\n    AstNode,\n    context::{ContextHost, LintContext},\n    rule::Rule,\n    utils::is_document_page,\n};\n\nfn no_document_import_in_page_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"`<Document />` from `next/document` should not be imported outside of `pages/_document.js`. See: https://nextjs.org/docs/messages/no-document-import-in-page\").with_help(\"Prevent importing `next/document` outside of `pages/_document.js`.\").with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoDocumentImportInPage;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Prevent importing `next/document` outside of `pages/_document.js`.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Importing `next/document` outside of `pages/_document.js` can cause\n    /// unexpected issues in your Next.js application.\n    ///\n    /// ### Examples\n    ///\n    /// Examples of **incorrect** code for this rule:","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/nextjs/no_document_import_in_page.rs#L1-L32","documentation":"Warning from oxlint rule `nextjs/no-document-import-in-page`. `next/document` defines the server-rendered shell for the whole app and is only valid in `pages/_document.js`; importing it in any other file breaks rendering assumptions. The rule tracks import declarations resolving to `next/document` and reports them everywhere else.","triggerScenarios":"An `import ... from 'next/document'` (default, named, namespace, or side-effect form) in any module other than `pages/_document.js`.","commonSituations":"Wanting `<Html>`/`<Head>`/`<NextScript>` pieces in a regular page; copy-pasting from `_document.js` into a layout; IDE auto-import picking `next/document`'s `Head`.","solutions":["Delete the import; only `_document.js` may use `next/document`.","For per-page head tags in the pages router use `import Head from 'next/head'`.","In the app router, render tags in `app/layout.tsx` or use the `metadata` API."],"exampleFix":"// before (in pages/index.tsx)\nimport { Head } from 'next/document';\n\n// after\nimport Head from 'next/head';","handlingStrategy":"validation","validationCode":"// next/document imports outside _document.js:\n// rg -n \"from 'next/document'\" pages app components -g '!pages/_document.*'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Memorize the split: `next/document` only in `pages/_document.js`; `next/head` in pages.","Fix IDE auto-imports immediately when they suggest `next/document` outside the document.","Run oxlint with the nextjs plugin in CI to catch stray imports."],"tags":["nextjs","pages-router","imports","lint"],"backgroundTag":"lint-restricted-import","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}