Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

NX
Next.js
0/10 completed
Beginner
Why Next.js?Pages, layouts and routingLinks, images and fonts
Intermediate
Server and Client ComponentsFetching data and streamingRoute Handlers (API routes)Forms and Server Actions
Advanced
Caching and revalidationMetadata, SEO, errors and Proxy
Pro
Auth, deployment and production
Tracks / Next.js / Beginner

Links, images and fonts

Next.js includes components that make navigation instant and media fast, with almost no configuration.

<Link>: client-side navigation

Use <Link> from next/link instead of <a> for links within your app:

tsx
import Link from 'next/link';

export default function Nav() {
  return (
    <nav>
      <Link href="/">Home</Link>
      <Link href="/blog">Blog</Link>
      <Link href="/blog/hello-world">A post</Link>
      <Link href={{ pathname: '/search', query: { q: 'shoes' } }}>Search shoes</Link>
    </nav>
  );
}

What <Link> gives you over a plain <a>:

  • Client-side navigation: no full page reload, and layouts keep their state.
  • Prefetching: when a link scrolls into view, Next.js preloads the route in the background, so clicks feel instant.
  • It still renders a real <a href>, so it's accessible, works for SEO, and Ctrl+click opens a new tab.

For external sites, a normal <a href="https://…"> is fine.

Highlighting the active link

usePathname() reads the current URL. Because it's a hook, the component must be a Client Component:

tsx
'use client';

import Link from 'next/link';
import { usePathname } from 'next/navigation';

const links = [
  { href: '/', label: 'Home' },
  { href: '/blog', label: 'Blog' },
  { href: '/about', label: 'About' },
];

export function NavLinks() {
  const pathname = usePathname();
  return (
    <nav>
      {links.map((l) => (
        <Link key={l.href} href={l.href} className={pathname === l.href ? 'font-bold underline' : ''}>
          {l.label}
        </Link>
      ))}
    </nav>
  );
}

The same logic, simulated so you can try it:

Navigating in code: useRouter

tsx
'use client';
import { useRouter } from 'next/navigation';      // note: next/navigation, not next/router

export function LogoutButton() {
  const router = useRouter();
  async function logout() {
    await fetch('/api/logout', { method: 'POST' });
    router.push('/login');        // also: router.replace(), router.back(), router.refresh()
  }
  return <button onClick={logout}>Log out</button>;
}

On the server (in Server Components and Server Actions), use redirect('/login') from next/navigation instead.

<Image>: automatic image optimisation

tsx
import Image from 'next/image';
import hero from './hero.jpg';            // a local import gives width/height automatically

export default function Page() {
  return (
    <>
      <Image src={hero} alt="Mountains at sunrise" placeholder="blur" loading="eager" fetchPriority="high" />
      <Image src="/team/asha.jpg" alt="Asha Rao" width={320} height={320} />
    </>
  );
}

next/image automatically:

  • Serves modern formats (WebP/AVIF) at the right size for each device
  • Lazy-loads off-screen images
  • Prevents layout shift by reserving space (width/height)
Prop Use
alt Required: describe the image
width / height Required for remote or public/ images (not for static imports)
fill Fill the parent element (the parent needs position: relative)
sizes Tell the browser how wide the image will display, for responsive layouts
loading="eager" / fetchPriority="high" For the main hero image: load it immediately. (The old priority prop is deprecated in Next.js 16.)

Remote images must be allowed in the config:

ts
// next.config.ts
import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  images: {
    remotePatterns: [{ protocol: 'https', hostname: 'images.example-cdn.com' }],
  },
};
export default nextConfig;

next/font: fast, self-hosted fonts

tsx
// app/layout.tsx
import { Inter, JetBrains_Mono } from 'next/font/google';

const inter = Inter({ subsets: ['latin'], display: 'swap' });
const mono = JetBrains_Mono({ subsets: ['latin'], variable: '--font-mono' });

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en" className={`${inter.className} ${mono.variable}`}>
      <body>{children}</body>
    </html>
  );
}

Google Fonts are downloaded at build time and self-hosted. There are no requests to Google from your users' browsers, which is better for privacy and speed, and it avoids layout shift.

Styling options

Approach Notes
Tailwind CSS The default in create-next-app; utility classes in your JSX
CSS Modules Button.module.css → import styles from './Button.module.css', with class names scoped automatically
Global CSS app/globals.css, imported in the root layout
Sass, CSS-in-JS Supported (CSS-in-JS needs Client Components or compatible libraries)

Practice (in your project)

  1. Build a header with <Link>s to three pages, and highlight the active one with usePathname().
  2. Add a hero image with next/image and fetchPriority="high".
  3. Load a Google font with next/font and apply it in the root layout.
✎ Check your understanding

1.Why use <Link> instead of <a> for internal navigation?

2.Which import gives you useRouter in the App Router?

← Pages, layouts and routing
React · live preview
Preview