next/google-font-display
<link>
element’s href
attribute’s value begins with the official Google Fonts API URL. This ensures that only Google Fonts links are examined for the font-display
feature, making the rule highly targeted and relevant when optimizing font loading strategies for web performance.
font-display
parameter in Google Fonts links, the rule encourages developers to explicitly define how fonts should be loaded. This can improve the user experience by potentially reducing invisible text during web page loading, as developers can select a font-display
strategy (e.g., swap
, fallback
, optional
) that best suits their application’s needs.
font-display
values like auto
, block
, and fallback
. This advice steers developers away from choices that might lead to poorer performance or user experience. For example, block
can cause a significant delay in text rendering until the font has fully loaded, which might not be ideal for users on slow connections.
&display=optional
or highlighting the non-recommended usage of certain display values, this ESLint rule also serves an educational purpose. It alerts developers to the importance of font rendering strategies and their impact on web performance and user experience, potentially leading to better-informed decisions in web development projects.
next/google-font-preconnect
preconnect
is used with Google Fonts.preconnect
link relation is used. The preconnect mechanism allows the browser to set up early connections before an HTTP request is actually sent to the server. This can include DNS lookups, TLS negotiations, and TCP handshakes. By implementing preconnect specifically for Google Fonts, web developers can decrease the time it takes to fetch the fonts, resulting in faster page load times.
preconnect
links for Google Fonts loaded through the <link>
tag in JSX files, which are commonly used in React-based projects, including Next.js applications. Without this rule, developers might overlook the inclusion of preconnect, potentially degrading the site’s performance, especially on first load where the font files are required to render text styled with those fonts.
rel="preconnect"
attribute is missing on <link>
tags that include href="https://fonts.gstatic.com"
, this rule helps maintain a high standard of code quality and adherence to best practices for web performance. As a consequence, it serves not just to correct an oversight but also to educate developers about the importance of preconnection when dealing with external resources like fonts.
next/inline-script-id
id
attribute on next/script
components with inline content.next/script
components with inline JavaScript are easily identifiable by enforcing the presence of an id
attribute. This makes it simpler to reference and potentially manipulate these scripts later in the codebase or through developer tools, enhancing maintainability and debuggability.
id
on inline scripts, it aids in enforcing a standardized practice within a development team or project, leading to a more organized and readable codebase. This practice can be particularly beneficial in large projects where consistency in code structure is crucial for efficient collaboration and code review processes.
next/next-script-for-ga
next/script
component when using the inline script for Google Analytics.next/script
component for adding Google Analytics (GA) scripts to Next.js applications, which is specifically optimized for Next.js’ performance characteristics, leading to better loading times and potentially improved site performance metrics.
next/script
with a specific loading strategy (e.g., ‘lazyOnload’), the rule helps ensure that the GA script does not impact the critical rendering path or delay the interactive readiness of the page, thus maintaining a smooth user experience.
dangerouslySetInnerHTML
attribute for inserting GA tracking code directly into the HTML. This practice can be unsafe if not properly managed, as it opens the door to cross-site scripting (XSS) attacks if the content is not properly sanitized.
next/no-assign-module-variable
module
variable.module
, a global object in Node.js environment. This ensures that developers do not unintentionally overwrite or misuse the module
variable, which could lead to unforeseen bugs and behavior in the application, particularly in server-side code or when using Next.js’ API routes.
module
variable, it helps maintain a clearer boundary between Node.js modules and JavaScript variables that developers define, reducing the risk of name collisions and the ensuing runtime errors.
module
. This not only avoids the specific technical issue of overwriting the global module
variable but also enhances code readability and maintainability by making it clearer what each variable represents.
module
variable as errors, as this rule does, aids in code review processes and contributes to the overall quality assurance of the codebase. It acts as an automated guardrail that prompts developers to rethink and refactor their code in real-time, thereby preventing the deployment of potentially problematic code that could affect the module system or application stability.
next/no-async-client-component
useState
and useEffect
for handling side effects and asynchronous data fetching in functional components, which is the recommended approach in React, thereby maintaining uniformity and improving readability across the project.next/no-before-interactive-script-outside-document
next/script
’s beforeInteractive
strategy outside of pages/_document.js
.beforeInteractive
strategy is used correctly, which helps maintain the application’s performance by loading scripts before the page becomes interactive, but only when it’s appropriate to do so.next/script
’s beforeInteractive
strategy in components or pages other than pages/_document.js
, which could lead to scripts being loaded too early in the app’s lifecycle on pages where it’s not required, potentially affecting page speed and user experience negatively.beforeInteractive
strategy is used outside its intended context, thus aiding in debugging and reducing time spent on finding performance-related issues.next/no-css-tags
next/no-document-import-in-page
next/document
outside of pages/_document.js
.next/document
is only imported in the custom document file (_document.js
), which is the proper place for applying global CSS styles and setting up the HTML structure for Next.js applications. This helps maintain the intended functionality of _document.js
as a place for customization that applies to all pages.Document
component. Since Document
is intended to be used once and applied globally, importing it into individual pages could lead to misconceptions about its purpose and usage within the Next.js framework.next/document
to _document.js
, it simplifies understanding the flow and structure of Next.js projects, especially for new developers.next/document
contains server-side rendering specific logic that should not be included in regular pages or components. Importing it outside of _document.js
could introduce server-rendering issues or negatively impact the performance and SEO of the application.next/no-duplicate-head
<Head>
in pages/_document.js
.<Head>
components in _document.js
for Next.js applications, helping ensure that SEO and meta tags are efficiently centralized for easier management and maintenance. This centralization avoids conflicting metadata or redundant link tags that could negatively impact SEO or page performance.<Head>
content into a single instance. This consolidation can lead to cleaner and more readable code, making it easier for developers to track where specific meta tags, stylesheets, or other head-related elements are declared.<Head>
components, the rule aids in debugging and code review processes. When developers inadvertently insert duplicate <Head>
tags, especially in large projects or when working in teams, this rule quickly highlights the issue, preventing potential head-related conflicts before they reach production.<Head>
component through ESLint, it ensures that all developers adhere to this pattern, reducing the likelihood of inconsistencies in the project’s codebase and simplifying the code review process.next/no-head-element
<head>
element.<Head />
from next/head
instead of the native HTML <head>
element within Next.js applications. This is crucial because Next.js has specific enhancements and optimizations tied to its <Head />
component, which do not apply when using the traditional <head>
element. These enhancements can include automatically handling duplicates, ordering of script tags, and optimizing loading for performance.
<head>
element inside the pages directory can lead to unexpected behaviors in Next.js applications, such as issues with server-side rendering or meta tags not being correctly updated. By ensuring that developers use <Head />
from next/head
, we maintain consistency and leverage Next.js features optimally.
<head>
element is used. This educates developers on best practices within Next.js applications and guides them towards using <Head />
for modifying the document’s head, ensuring they are aware of the preferred method and its benefits, such as ease of setting per-page SEO attributes.
<Head />
from next/head
, this rule directly supports enhanced SEO capabilities and better performance. The <Head />
component ensures that meta tags are correctly merged or updated on page navigation, which is essential for single-page applications where page content changes without a full page reload. This aligns with modern web development practices, where SEO and performance are crucial for the visibility and user experience of web applications.
next/no-head-import-in-document
next/head
in pages/_document.js
.next/head
within pages/_document.js
because next/head
is intended for repeating head elements across pages rather than for modifying the document’s head directly. This ensures that the head modifications are done correctly at the document level using <Head />
from next/document
, which is the proper way to include elements in the head for all pages.<Head />
from next/document
in _document.js
, it promotes consistency and the correct usage patterns within the Next.js framework. This reduces the risk of common mistakes by developers new to or unfamiliar with the intricacies of Next.js, enhancing the maintainability and readability of the code.next/head
in _document.js
aids in avoiding conflicts or unexpected behavior that can arise from misusing next/head
at the document level. Since next/head
is designed to be used within individual pages or components, its usage in _document.js
can lead to problems with server-side rendering or the overall page head elements rendering.<Head />
from next/document
for modifying the document’s head), it ensures that the application leverages Next.js features optimally, leading to better performance and a more predictable application behavior.next/no-html-link-for-pages
<a>
elements to navigate to internal Next.js pages.<Link />
component for internal navigation, leveraging its prefetching capabilities for faster page transitions, unlike the traditional <a>
tag that does not prefetch pages.<a>
tags for internal links, it helps developers avoid the common pitfall of full page reloads on navigation within a Next.js app, which can degrade the app’s performance and user experience.<Link />
.<a>
tags for internal navigation during code analysis, thereby helping maintain code quality and consistency across large codebases.next/no-img-element
<img>
element due to slower LCP and higher bandwidth.<Image />
from next/image
instead of the standard <img>
tag, directly impacting the Largest Contentful Paint (LCP) metric favorably. LCP is crucial for user experience and SEO.
<img>
tag. The <Image />
component from Next.js automatically optimizes images for different screen sizes and resolutions, reducing unnecessary data transfer.
<img>
elements, developers are nudged towards using features specific to Next.js that are designed to enhance performance and user experience.
<picture>
element, acknowledging that there are valid use cases for <img>
in combination with <picture>
for manually specified image sources for different scenarios. This flexibility ensures that the rule doesn’t enforce a blanket restriction but rather guides towards optimization except when a specific use case justifies otherwise.
next/no-page-custom-font
_document.js
for global availability, preventing the need to import them on a per-page basis which can lead to performance issues.
_document.js
file, leading to faster page load times.
next/no-script-component-in-head
next/script
in next/head
component.<Script>
component from being used inside the <Head>
component, which is a pattern discouraged by the Next.js framework itself. This helps maintain the structural integrity of your Next.js application.<head>
element. Inserting scripts directly into the <head>
can block page rendering until the script is downloaded and executed. By moving the <Script>
component outside of <Head>
, scripts can be managed more efficiently, especially when using loading strategies like lazyOnload
.<head>
can delay the rendering of the page content, which can negatively impact search engine rankings as search engines prioritize fast-loading pages.<Script>
component from the <Head>
, it encourages developers to organize their scripts in a way that respects the loading and execution order required for proper initialization of web pages.next/no-styled-jsx-in-document
styled-jsx
in pages/_document.js
.styled-jsx
in pages/_document.js
, which is crucial because _document.js
is meant for augmenting the initial HTML document structure and not for defining page-level styles. This ensures that styles are defined where they are most efficient and maintainable.styled-jsx
in _document.js
, the rule promotes the usage of global stylesheets or CSS-in-JS libraries in a way that aligns with Next.js’s recommended patterns for styling. This supports better performance and consistency in styling across the application.styled-jsx
is incorrectly used within _document.js
, providing immediate feedback to developers. This helps catch and correct potential misuse early in the development process, reducing the likelihood of styling issues in production._document.js
is mainly concerned with server-rendered document markup, incorporating styles outside of this file (as suggested in the fixed code example) aligns with Next.js’s architecture, ensuring styles are correctly applied and managed.next/no-sync-scripts
async
or defer
attributes in script tags, this rule promotes the use of asynchronous or deferred scripts. These practices are essential for modern web development, as they allow scripts to be loaded in the background or after the HTML document has been fully parsed, thereby improving the site’s performance and user experience.
next/no-title-in-document-head
<title>
with Head
component from next/document
.<title>
element within the <Head>
component imported from next/document
, ensuring that developers follow the best practice of defining titles at the page level rather than in the document head. This approach aligns with Next.js’s recommended way to handle document metadata, ensuring consistent behavior across different pages in the application.<Head>
from next/head
for setting the page title, this rule helps maintain the dynamic nature of single-page applications (SPAs) built with Next.js. Titles can be updated based on the page content or user interactions, improving the user experience and making the application more accessible and SEO-friendly.<title>
tags in _document.js
will not update dynamically as the user navigates through the application, identifying the misuse of <Head>
from next/document
for titles ensures that developers are immediately made aware of the incorrect implementation before it becomes an issue.<title>
within the <Head>
component from next/document
. This modular approach is in line with modern web development practices, promoting code reuse and maintainability. Ensuring titles are set within specific pages rather than globally in _document.js
allows for more granular control over how pages are presented and indexed by search engines.next/no-typos
getStaticsProps
instead of getStaticProps
.next/no-unwanted-polyfillio