Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESLint Plugin: Fix Document and Head import rules #25730

Merged
merged 1 commit into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions errors/no-document-import-in-page.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

### Why This Error Occurred

`next/document` was imported in a page outside of `pages/_document.js`. This can cause unexpected issues in your application.
`next/document` was imported in a page outside of `pages/_document.js` (or `pages/_document.tsx` if you are using TypeScript). This can cause unexpected issues in your application.

### Possible Ways to Fix It

Only import and use `next/document` within `pages/_document.js` to override the default `Document` component:
Only import and use `next/document` within `pages/_document.js` (or `pages/_document.tsx`) to override the default `Document` component:

```jsx
// pages/_document.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
}

const page = context.getFilename().split('pages')[1]
if (!page || path.parse(page).name === '_document') {
if (!page || path.parse(page).name.startsWith('_document')) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
}

const document = context.getFilename().split('pages')[1]
if (!document || path.parse(document).name !== '_document') {
if (!document || !path.parse(document).name.startsWith('_document')) {
return
}

Expand Down
14 changes: 14 additions & 0 deletions test/eslint-plugin-next/no-document-import-in-page.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ ruleTester.run('no-document-import-in-page', rule, {
`,
filename: 'pages/_document.tsx',
},
{
code: `import Document from "next/document"

export default class MyDocument extends Document {
render() {
return (
<Html>
</Html>
);
}
}
`,
filename: 'pages/_document.page.tsx',
},
],
invalid: [
{
Expand Down
30 changes: 30 additions & 0 deletions test/eslint-plugin-next/no-head-import-in-document.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,35 @@ ruleTester.run('no-head-import-in-document', rule, {
},
],
},
{
code: `
import Document, { Html, Main, NextScript } from 'next/document'
import Head from 'next/head'

class MyDocument extends Document {
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}

export default MyDocument
`,
filename: 'pages/_document.page.tsx',
errors: [
{
message:
'next/head should not be imported in pages/_document.page.tsx. Import Head from next/document instead. See https://nextjs.org/docs/messages/no-head-import-in-document.',
type: 'ImportDeclaration',
},
],
},
],
})