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

Adds consistency to ESLint rules. #34335

Merged
merged 39 commits into from Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
b92b744
Adds consistency to ESLint rules.
manovotny Feb 14, 2022
75fb7e8
Fixes lint errors.
manovotny Feb 14, 2022
8b63051
Fixes manifest.
manovotny Feb 14, 2022
d20d76a
Merge branch 'canary' into eslint-consistency
manovotny Feb 14, 2022
5f5d4c7
Adds missing title.
manovotny Feb 14, 2022
28815b9
Fixes copy / paste error.
manovotny Feb 14, 2022
66bda56
Update errors/no-script-in-document.md
manovotny Feb 14, 2022
c4f4225
Update errors/no-sync-scripts.md
manovotny Feb 14, 2022
831afe2
Updates a couple of rule descriptions.
manovotny Feb 14, 2022
a22d98e
Merge branch 'eslint-consistency' of github.com:manovotny/next.js int…
manovotny Feb 14, 2022
0468818
Adds redirects.
manovotny Feb 14, 2022
9131067
Merge branch 'canary' into eslint-consistency
leerob Feb 14, 2022
1df47a8
Fixes unit tests.
manovotny Feb 14, 2022
fbf973f
Merge branch 'canary' into eslint-consistency
manovotny Feb 24, 2022
b0692c6
Merge branch 'canary' into eslint-consistency
manovotny Mar 1, 2022
23d6faa
Merge branch 'canary' into eslint-consistency
manovotny Apr 14, 2022
d16be8b
Removes duplicated section.
manovotny Apr 14, 2022
1482de9
Merge branch 'canary' into eslint-consistency
timneutkens Apr 15, 2022
81ebcf9
Merge branch 'canary' into eslint-consistency
manovotny Apr 22, 2022
37a09c0
Updates `no-before-interactive-script-outside-document` description.
manovotny Apr 22, 2022
c60ba22
Fixes lint.
manovotny Apr 22, 2022
75dc0fa
Fixes integration tests.
manovotny Apr 23, 2022
c91b648
Adds description to `no-before-interactive-script-outside-document` d…
manovotny Apr 23, 2022
f237741
Merge branch 'canary' into eslint-consistency
manovotny Apr 23, 2022
334ceaa
Merge branch 'canary' into eslint-consistency
manovotny May 5, 2022
490e77a
Removes `link-passhref` from rules list.
manovotny May 5, 2022
3ce6043
Merge branch 'canary' into eslint-consistency
manovotny May 18, 2022
e93750f
Merge branch 'canary' into eslint-consistency
manovotny May 19, 2022
8093af9
Updates remaining `pages/_middleware.js` references.
manovotny May 19, 2022
db695fb
Merge branch 'canary' into eslint-consistency
manovotny May 19, 2022
49043af
Merge branch 'canary' into eslint-consistency
manovotny May 23, 2022
0acdf1b
Adds consistancy to messaging in new `no-styled-jsx-in-document` rule.
manovotny May 23, 2022
6339cfd
Merge branch 'canary' into eslint-consistency
manovotny May 23, 2022
2e2ba1b
Merge branch 'canary' into eslint-consistency
manovotny Jun 13, 2022
063135d
Merge branch 'canary' into eslint-consistency
manovotny Jun 13, 2022
07c1519
Merge branch 'canary' into eslint-consistency
manovotny Jun 13, 2022
0578065
Apply suggestions from code review
ijjk Jun 14, 2022
13b642f
Merge branch 'canary' into eslint-consistency
ijjk Jun 14, 2022
031ba70
Apply suggestions from code review
ijjk Jun 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 23 additions & 20 deletions docs/basic-features/eslint.md
Expand Up @@ -80,28 +80,31 @@ This will take precedence over the configuration from `next.config.js`.

Next.js provides an ESLint plugin, [`eslint-plugin-next`](https://www.npmjs.com/package/@next/eslint-plugin-next), already bundled within the base configuration that makes it possible to catch common issues and problems in a Next.js application. The full set of rules is as follows:

| | Rule | Description |
| :-: | ------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| ✔️ | [next/google-font-display](/docs/messages/google-font-display.md) | Enforce optional or swap font-display behavior with Google Fonts |
| ✔️ | [next/google-font-preconnect](/docs/messages/google-font-preconnect.md) | Enforce preconnect usage with Google Fonts |
| ✔️ | [next/link-passhref](/docs/messages/link-passhref.md) | Enforce passHref prop usage with custom Link components |
| ✔️ | [next/no-css-tags](/docs/messages/no-css-tags.md) | Prevent manual stylesheet tags |
| ✔️ | [next/no-document-import-in-page](/docs/messages/no-document-import-in-page.md) | Disallow importing next/document outside of pages/document.js |
| ✔️ | [next/no-head-import-in-document](/docs/messages/no-head-import-in-document.md) | Disallow importing next/head in pages/document.js |
| ✔️ | [next/no-html-link-for-pages](/docs/messages/no-html-link-for-pages.md) | Prohibit HTML anchor links to pages without a Link component |
| ✔️ | [next/no-img-element](/docs/messages/no-img-element.md) | Prohibit usage of HTML <img> element |
| ✔️ | [next/no-head-element](/docs/messages/no-head-element.md) | Prohibit usage of HTML <head> element |
| ✔️ | [next/no-page-custom-font](/docs/messages/no-page-custom-font.md) | Prevent page-only custom fonts |
| ✔️ | [next/no-sync-scripts](/docs/messages/no-sync-scripts.md) | Forbid synchronous scripts |
| ✔️ | [next/no-title-in-document-head](/docs/messages/no-title-in-document-head.md) | Disallow using <title> with Head from next/document |
| ✔️ | [next/no-unwanted-polyfillio](/docs/messages/no-unwanted-polyfillio.md) | Prevent duplicate polyfills from Polyfill.io |
| ✔️ | [next/inline-script-id](/docs/messages/inline-script-id.md) | Enforce id attribute on next/script components with inline content |
| ✔️ | next/no-typos | Ensure no typos were made declaring [Next.js's data fetching function](/docs/basic-features/data-fetching/overview.md) |
| ✔️ | [next/next-script-for-ga](/docs/messages/next-script-for-ga.md) | Use the Script component to defer loading of the script until necessary. |
| ✔️ | [next/no-styled-jsx-in-document](/docs/messages/no-styled-jsx-in-document.md) | styled-jsx should not be used in \_document |

- ✔: Enabled in the recommended configuration

| | Rule | Description |
| :-: | --------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| ✔️ | [@next/next/google-font-display](/docs/messages/google-font-display.md) | Enforce font-display behavior with Google Fonts. |
| ✔️ | [@next/next/google-font-preconnect](/docs/messages/google-font-preconnect.md) | Ensure `preconnect` is used with Google Fonts. |
| ✔️ | [@next/next/inline-script-id](/docs/messages/inline-script-id.md) | Enforce `id` attribute on `next/script` components with inline content. |
| ✔️ | [@next/next/next-script-for-ga](/docs/messages/next-script-for-ga.md) | Prefer `next/script` component when using the inline script for Google Analytics. |
| ✔️ | [@next/next/no-before-interactive-script-outside-document](/docs/messages/no-before-interactive-script-outside-document.md) | Prevent usage of `next/script`'s `beforeInteractive` strategy outside of `pages/_document.js`. |
| ✔️ | [@next/next/no-css-tags](/docs/messages/no-css-tags.md) | Prevent manual stylesheet tags. |
| ✔️ | [@next/next/no-document-import-in-page](/docs/messages/no-document-import-in-page.md) | Prevent importing `next/document` outside of `pages/_document.js`. |
| ✔️ | [@next/next/no-duplicate-head](/docs/messages/no-duplicate-head.md) | Prevent duplicate usage of `<Head>` in `pages/_document.js`. |
| ✔️ | [@next/next/no-head-element](/docs/messages/no-head-element.md) | Prevent usage of `<head>` element. |
| ✔️ | [@next/next/no-head-import-in-document](/docs/messages/no-head-import-in-document.md) | Prevent usage of `next/head` in `pages/_document.js`. |
| ✔️ | [@next/next/no-html-link-for-pages](/docs/messages/no-html-link-for-pages.md) | Prevent usage of `<a>` elements to navigate to internal Next.js pages. |
| ✔️ | [@next/next/no-img-element](/docs/messages/no-img-element.md) | Prevent usage of `<img>` element to prevent layout shift. |
| ✔️ | [@next/next/no-page-custom-font](/docs/messages/no-page-custom-font.md) | Prevent page-only custom fonts. |
| ✔️ | [@next/next/no-script-component-in-head](/docs/messages/no-script-component-in-head.md) | Prevent usage of `next/script` in `next/head` component. |
| ✔️ | [@next/next/no-server-import-in-page](/docs/messages/no-server-import-in-page.md) | Prevent usage of `next/server` outside of `middleware.js`. |
| ✔️ | [@next/next/no-styled-jsx-in-document](/docs/messages/no-styled-jsx-in-document.md) | Prevent usage of `styled-jsx` in `pages/_document.js`. |
| ✔️ | [@next/next/no-sync-scripts](/docs/messages/no-sync-scripts.md) | Prevent synchronous scripts. |
| ✔️ | [@next/next/no-title-in-document-head](/docs/messages/no-title-in-document-head.md) | Prevent usage of `<title>` with `Head` component from `next/document`. |
| ✔️ | @next/next/no-typos | Prevent common typos in [Next.js's data fetching functions](/docs/basic-features/data-fetching.md) |
| ✔️ | [@next/next/no-unwanted-polyfillio](/docs/messages/no-unwanted-polyfillio.md) | Prevent duplicate polyfills from Polyfill.io. |

If you already have ESLint configured in your application, we recommend extending from this plugin directly instead of including `eslint-config-next` unless a few conditions are met. Refer to the [Recommended Plugin Ruleset](/docs/basic-features/eslint.md#recommended-plugin-ruleset) to learn more.

### Custom Settings
Expand Down
7 changes: 5 additions & 2 deletions errors/google-font-display.md
@@ -1,8 +1,10 @@
# Google Font Display

> Enforce font-display behavior with Google Fonts.

### Why This Error Occurred

For a Google Font, the `display` descriptor was either not assigned or set to `auto`, `fallback`, or `block`.
For a Google Font, the font-display descriptor was either missing or set to `auto`, `block`, or `fallback`, which are not recommended.

### Possible Ways to Fix It

Expand All @@ -29,9 +31,10 @@ Specifying `display=optional` minimizes the risk of invisible text or layout shi

### When Not To Use It

If you want to specifically display a font using a `block` or `fallback` strategy, then you can disable this rule.
If you want to specifically display a font using an `auto`, `block`, or `fallback` strategy, then you can disable this rule.

### Useful Links

- [Controlling Font Performance with font-display](https://developer.chrome.com/blog/font-display/)
- [Google Fonts API Docs](https://developers.google.com/fonts/docs/css2#use_font-display)
- [CSS `font-display` property](https://www.w3.org/TR/css-fonts-4/#font-display-desc)
2 changes: 2 additions & 0 deletions errors/google-font-preconnect.md
@@ -1,5 +1,7 @@
# Google Font Preconnect

> Ensure `preconnect` is used with Google Fonts.

### Why This Error Occurred

A preconnect resource hint was not used with a request to the Google Fonts domain. Adding `preconnect` is recommended to initiate an early connection to the origin.
Expand Down
4 changes: 3 additions & 1 deletion errors/inline-script-id.md
@@ -1,4 +1,6 @@
# next/script components with inline content require an `id` attribute
# Inline script id

> Enforce `id` attribute on `next/script` components with inline content.

## Why This Error Occurred

Expand Down
4 changes: 3 additions & 1 deletion errors/link-passhref.md
@@ -1,4 +1,6 @@
# Link passHref
# Link `passHref`

> Ensure `passHref` is used with custom `Link` components.

### Why This Error Occurred

Expand Down
26 changes: 22 additions & 4 deletions errors/manifest.json
Expand Up @@ -563,16 +563,34 @@
"path": "/errors/sharp-version-avif.md"
},
{
"title": "script-in-document-page",
"path": "/errors/no-script-in-document-page.md"
"path": "/errors/no-script-in-document-page.md",
"redirect": {
"destination": "/errors/no-script-in-document"
}
},
{
"title": "no-script-in-document",
"path": "/errors/no-script-in-document.md"
leerob marked this conversation as resolved.
Show resolved Hide resolved
},
{
"title": "before-interactive-script-outside-document",
"path": "/errors/no-before-interactive-script-outside-document.md"
},
{
"title": "script-component-in-head-component",
"path": "/errors/no-script-component-in-head-component.md"
"path": "/errors/no-script-in-head-component.md",
"redirect": {
"destination": "/errors/no-script-component-in-head"
}
},
{
"path": "/errors/no-script-component-in-head-component.md",
"redirect": {
"destination": "/errors/no-script-component-in-head"
}
},
{
"title": "no-script-component-in-head",
"path": "/errors/no-script-component-in-head.md"
},
{
"title": "script-tags-in-head-component",
Expand Down
2 changes: 2 additions & 0 deletions errors/next-script-for-ga.md
@@ -1,5 +1,7 @@
# Next Script for Google Analytics

> Prefer `next/script` component when using the inline script for Google Analytics.

### Why This Error Occurred

An inline script was used for Google analytics which might impact your webpage's performance.
Expand Down
10 changes: 6 additions & 4 deletions errors/no-before-interactive-script-outside-document.md
@@ -1,15 +1,17 @@
# beforeInteractive Script component outside \_document.js
# No Before Interactive Script Outside Document

> Prevent usage of `next/script`'s `beforeInteractive` strategy outside of `pages/_document.js`.

#### Why This Error Occurred

You can't use the `next/script` component with the `beforeInteractive` strategy outside the `_document.js` page. That's because `beforeInteractive` strategy only works inside **\_document.js** and is designed to load scripts that are needed by the entire site (i.e. the script will load when any page in the application has been loaded server-side).
You cannot use the `next/script` component with the `beforeInteractive` strategy outside `pages/_document.js`. That's because `beforeInteractive` strategy only works inside **`pages/_document.js`** and is designed to load scripts that are needed by the entire site (i.e. the script will load when any page in the application has been loaded server-side).

#### Possible Ways to Fix It

If you want a global script, move the script inside `_document.js` page.
If you want a global script, move the script inside `pages/_document.js`.

```jsx
// In _document.js
// In `pages/_document.js`
import { Html, Head, Main, NextScript } from 'next/document'
import Script from 'next/script'

Expand Down
4 changes: 3 additions & 1 deletion errors/no-css-tags.md
@@ -1,8 +1,10 @@
# No CSS Tags
manovotny marked this conversation as resolved.
Show resolved Hide resolved

> Prevent manual stylesheet tags.

### Why This Error Occurred

An HTML link element was used to link to an external stylesheet. This can negatively affect CSS resource loading on your web page.
A `link` element was used to link to an external stylesheet. This can negatively affect CSS resource loading on your webpage.

### Possible Ways to Fix It

Expand Down
2 changes: 2 additions & 0 deletions errors/no-document-import-in-page.md
@@ -1,5 +1,7 @@
# No Document Import in Page

> Prevent importing `next/document` outside of `pages/_document.js`.

### Why This Error Occurred

`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.
Expand Down
2 changes: 2 additions & 0 deletions errors/no-duplicate-head.md
@@ -1,5 +1,7 @@
# No Duplicate Head

> Prevent duplicate usage of `<Head>` in `pages/_document.js`.

### Why This Error Occurred

More than a single instance of the `<Head />` component was used in a single custom document. This can cause unexpected behavior in your application.
Expand Down
4 changes: 3 additions & 1 deletion errors/no-head-element.md
@@ -1,8 +1,10 @@
# No Head Element

> Prevent usage of `<head>` element.

### Why This Error Occurred

An HTML `<head>` element was used to include page-level metadata, but this can cause unexpected behavior in a Next.js application. Use Next.js' built-in `<Head />` component instead.
A `<head>` element was used to include page-level metadata, but this can cause unexpected behavior in a Next.js application. Use Next.js' built-in `next/head` component instead.

### Possible Ways to Fix It

Expand Down
2 changes: 2 additions & 0 deletions errors/no-head-import-in-document.md
@@ -1,5 +1,7 @@
# No Head Import in Document

> Prevent usage of `next/head` in `pages/_document.js`.

### Why This Error Occurred

`next/head` was imported in `pages/_document.js`. This can cause unexpected issues in your application.
manovotny marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
4 changes: 3 additions & 1 deletion errors/no-html-link-for-pages.md
@@ -1,8 +1,10 @@
# No HTML link for pages

> Prevent usage of `<a>` elements to navigate to internal Next.js pages.

### Why This Error Occurred

An HTML anchor element, `<a>`, was used to navigate to a page route without using the `Link` component.
An `<a>` element was used to navigate to a page route without using the `next/link` component, causing unnecessary full page refreshes.

The `Link` component is required in order to enable client-side route transitions between pages and provide a single-page app experience.

Expand Down
4 changes: 3 additions & 1 deletion errors/no-img-element.md
@@ -1,8 +1,10 @@
# No Img Element

> Prevent usage of `<img>` element to prevent layout shift.

### Why This Error Occurred

An HTML `<img>` element was used to display an image. For better performance and automatic Image Optimization, use Next.js' built-in image component instead.
An `<img>` element was used to display an image. For better performance and automatic Image Optimization, use `next/image` instead.

### Possible Ways to Fix It

Expand Down
4 changes: 3 additions & 1 deletion errors/no-page-custom-font.md
@@ -1,9 +1,11 @@
# No Page Custom Font

> Prevent page-only custom fonts.

### Why This Error Occurred

- The custom font you're adding was added to a page - this only adds the font to the specific page and not the entire application.
- The custom font you're adding was added to a separate component within `Document` - this disables automatic font optimization.
- The custom font you're adding was added to a separate component within `pages/_document.js` - this disables automatic font optimization.

### Possible Ways to Fix It

Expand Down
@@ -1,12 +1,14 @@
# Script component inside Head component
# No Script Component in Head

> Prevent usage of `next/script` in `next/head` component.

#### Why This Error Occurred

The `next/script` component shouldn't be placed inside the `next/head` component
The `next/script` component should not be used in a `next/head` component.

#### Possible Ways to Fix It

Move the `<Script />` component outside of `<Head>...</Head>`
Move the `<Script />` component outside of `<Head>` instead.

**Before**

Expand Down
29 changes: 0 additions & 29 deletions errors/no-script-in-document-page.md

This file was deleted.