Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
ESLint Plugin: Fix Document and Head import rules (vercel#25730)
Browse files Browse the repository at this point in the history
  • Loading branch information
housseindjirdeh committed Jun 7, 2021
1 parent 5037bfb commit 1ec7c17
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
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',
},
],
},
],
})

0 comments on commit 1ec7c17

Please sign in to comment.