DEV Community

Cover image for Day 52 of #100DayOfCode — Script, Link & Image components in Next.js
M Saad Ahmad
M Saad Ahmad

Posted on

Day 52 of #100DayOfCode — Script, Link & Image components in Next.js

If you're building modern web applications with Next.js, you've probably come across three powerful built-in components: Link, Script, and Image. These components are designed to improve performance, optimize user experience, and simplify common tasks like navigation, third-party script loading, and image optimization. Unlike traditional HTML elements, these components are deeply integrated with Next.js features such as routing, lazy loading, and performance optimization, making them essential tools for any developer working in the Next.js ecosystem.

The goal for day 52 was to understand the Link, Script, and Image components in Next.js and their use.


🔗 The Link Component in Next.js

The Link component in Next.js is used for client-side navigation between pages. Instead of using traditional <a> tags, Next.js encourages developers to use <Link> because it enables faster page transitions, prefetching, and avoids full page reloads.

Why use <Link> instead of <a>?

  • Client-side routing for faster navigation
  • No full page reloads
  • Automatic prefetching of linked pages
  • Improved performance and UX

💻 Example:

import Link from 'next/link';

export default function Home() {
  return (
    <div>
      <h1>Welcome to My App</h1>
      <Link href="/about">
        Go to About Page
      </Link>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

👉 Using <Link> ensures your app feels like a single-page application (SPA) while still benefiting from server-side rendering.


📜 The Script Component in Next.js

The Script component in Next.js allows you to load and manage third-party scripts efficiently. It gives you fine-grained control over when and how scripts are loaded, improving performance and avoiding render-blocking issues.

⚠️ Note: Unlike <script> in HTML, Next.js <Script> optimizes loading strategies.

Why use <Script> instead of traditional script tags?

  • Control loading strategies (beforeInteractive, afterInteractive, lazyOnload)
  • Prevent render-blocking
  • Optimize performance and Core Web Vitals
  • Better handling of third-party scripts

💻 Example:

import Script from 'next/script';

export default function Analytics() {
  return (
    <>
      <Script
        src="https://example.com/analytics.js"
        strategy="afterInteractive"
      />
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

👉 This ensures scripts load only when needed, improving your app’s speed and performance.


🖼️ The Image Component in Next.js

The Image component is one of the most powerful features of Next.js. It provides automatic image optimization, including resizing, lazy loading, and modern format delivery (like WebP).

What makes <Image> special?

  • Automatic image optimization
  • Lazy loading by default
  • Responsive images
  • Supports modern formats

💻 Example:

import Image from 'next/image';

export default function Profile() {
  return (
    <div>
      <h2>My Profile</h2>
      <Image
        src="/profile.jpg"
        alt="Profile Picture"
        width={300}
        height={300}
      />
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

👉 Using <Image> helps improve Core Web Vitals, especially Largest Contentful Paint (LCP).


🎯 Conclusion

Understanding and using Link, Script, and Image components in Next.js is crucial for building high-performance, SEO-friendly web applications. The <Link> component enhances navigation speed, <Script> optimizes third-party script loading, and <Image> ensures efficient image delivery. Together, they help developers create fast, scalable, and user-friendly applications.

Thanks for reading. Feel free to share your thoughts!

Top comments (0)