Next.js Components: Build Reusable UI for High-Performance Web Applications
List of Common Next.js Components and Their Uses
1. <Image /> Component
Use: Optimizes images automatically for faster loading and better performance.
Features:
-
Automatic resizing and compression
-
Lazy loading by default
-
Supports modern formats (WebP)
-
Improves SEO and page speed
Example Use:
import Image from 'next/image';
<Image src="/logo.png" alt="Logo" width={150} height={50} />
2. <Link /> Component
Use: Handles client-side navigation between pages with faster page transitions.
Features:
-
Prefetching pages for faster navigation
-
Works with dynamic routes
-
Replaces standard
<a>tags in Next.js apps
Example Use:
import Link from 'next/link'; <Link href="/about">About Us</Link>
3. <Head /> Component
Use: Manages the HTML <head> section for meta tags, title, SEO, and scripts.
Features:
-
Add page title and meta description dynamically
-
Include external scripts or styles
-
Helps with SEO optimization
Example Use:
import Head from 'next/head'; <Head> <title>Home Page</title> <meta name="description" content="Next.js application example" /> </Head>
4. <Script /> Component
Use: Optimizes third-party or custom scripts for performance and proper loading.
Features:
-
Lazy loading or before/after page load
-
Avoid blocking main thread
-
Control execution order
Example Use:
import Script from 'next/script'; <Script src="https://example.com/script.js" strategy="lazyOnload" />
5. <Error /> or ErrorBoundary
Use: Handles errors in pages and components.
Features:
-
Provides custom error pages
-
Graceful handling of runtime errors
-
Improves user experience
Example:
function ErrorPage() {
return <h1>Something went wrong!</h1>;
}
export default ErrorPage;
6. <App /> Component (Custom App)
Use: Wraps all pages in a Next.js application to provide global layouts or state.
Features:
-
Persist layouts across pages
-
Add global styles or context providers
-
Customize page initialization
Example:
// pages/_app.js
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
7. <Document /> Component (Custom Document)
Use: Customize the server-rendered HTML document structure.
Features:
-
Modify
<html>and<body>tags -
Add global meta tags or fonts
-
Only rendered on the server
Example:
// pages/_document.js
import { Html, Head, Main, NextScript } from 'next/document';
export default function Document() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
8. <Main /> and <NextScript />
Use: Used inside <Document /> to render page content and Next.js scripts.
Features:
-
<Main />renders the page content -
<NextScript />includes Next.js JavaScript bundle
9. <Link /> Dynamic Routing
Use: Works with dynamic pages like /blog/[id].js.
Features:
-
Prefetching dynamic pages
-
Smooth client-side navigation
10. <Script /> Strategies
Use: Optimize performance by controlling how and when scripts load.
Strategies:
-
beforeInteractive– load before page becomes interactive -
afterInteractive– default lazy load after page renders -
lazyOnload– load during idle time