SkillPilotAI

Metadata, SEO and optimisation

When a link is shared or Google crawls a page, a bot reads the tags in the HTML head. Next.js gives you the Metadata API for titles, descriptions and OG images, plus next/font, next/image and next/script to keep pages fast and stable (better Core Web Vitals).

69.1 Problem: the same title on every page

  • The root layout from create-next-app exports a fixed metadata: title "Create Next App" and a generic description.
  • The layout wraps every page, so home and product pages have exactly the same title.
React (TSX)
// app/layout.tsx (from create-next-app)import type { Metadata } from "next";export const metadata: Metadata = {  title: "Create Next App",  description: "Generated by create next app",}; export default function RootLayout({  children,}: { children: React.ReactNode }) {  return (    <html lang="en">      <body>{children}</body>    </html>  );}
Output
$ npm run build && npm start$ curl -s localhost:3000/ | grep title<title>Create Next App</title>$ curl -s localhost:3000/products/1 | grep title<title>Create Next App</title>

69.2 Why is it used?

  • On a link share or crawl, a bot fetches the page HTML and reads the head tags.
  • It needs a title, a description and an og:image to build a search result or preview card.
  • If these are the same or missing, Google cannot understand the page and the WhatsApp preview comes out blank.
How a bot builds a preview
  1. 1.Link shared on WhatsApp / Google crawls the URL

  2. 2.Bot fetches the server-rendered HTML

  3. 3.Reads head tags: <title>, meta description, og:image

  4. 4.Downloads the og:image (1200x630)

  5. 5.Shows a search result or preview card

69.3 Fix: a title template

  • metadataBase: the site's base URL, so Next.js can build full URLs for images and links.
  • title.default ("ShopKart") is used when a page gives no title.
  • title.template ("%s | ShopKart"): the page's title replaces %s, so every page differs and the brand stays consistent.
React (TSX)
// app/layout.tsximport type { Metadata } from "next"; export const metadata: Metadata = {  metadataBase: new URL("https://shopkart.in"),  title: {    default: "ShopKart",    template: "%s | ShopKart",  },  description: "Online shopping made simple",};
Output
$ curl -s localhost:3000/ | grep title<title>ShopKart</title>

69.4 Dynamic: generateMetadata

  • Each product needs its own title from the database: export an async generateMetadata.
  • It reads the id from params and calls the same getProduct the page uses.
  • Product name becomes the title, summary becomes the description.
React (TSX)
// app/products/[id]/page.tsximport type { Metadata } from "next";import { getProduct } from "@/lib/products"; type Props = { params: Promise<{ id: string }> }; export async function generateMetadata(  { params }: Props,): Promise<Metadata> {  const { id } = await params;  const p = await getProduct(id);  return {    title: p.name,    description: p.summary,  };}
Output
$ curl -s localhost:3000/products/1 | grep title<title>Running Shoes | ShopKart</title><meta name="description"  content="Lightweight shoes for daily runs"/>

69.5 Checkpoint: what is the tab title?

  • About gives only title "About" -> "About | ShopKart" (child title fits the layout's template).
  • Home gives no metadata -> the default "ShopKart".
React (TSX)
// app/about/page.tsxexport const metadata = { title: "About" }; // app/page.tsx (no metadata)export default function Home() {  return <h1>ShopKart</h1>;}
Output
$ curl -s localhost:3000/about | grep title<title>About | ShopKart</title>$ curl -s localhost:3000/ | grep title<title>ShopKart</title>

69.6 OG image: the share preview

  • OG = Open Graph, used by WhatsApp, LinkedIn and X for link previews.
  • An opengraph-image file in the product folder exports a function; ImageResponse converts JSX into a PNG.
  • size 1200 x 630 is the standard for social apps; the image shows product name and price.
  • Next.js adds the og:image tags in the head automatically; every product gets its own card.
React (TSX)
// app/products/[id]/opengraph-image.tsximport { ImageResponse } from "next/og";import { getProduct } from "@/lib/products";export const size = { width: 1200, height: 630 };type Props = { params: Promise<{ id: string }> };export default async function Image(  { params }: Props,) {  const p = await getProduct((await params).id);  return new ImageResponse(    <div style={{ fontSize: 80 }}>      {`${p.name}: ₹${p.price}`}    </div>,    size,  );}
Output
$ curl -s localhost:3000/products/1 | grep og:image<meta property="og:image" content=  "https://shopkart.in/products/1/opengraph-image?88ac…"/><meta property="og:image:width" content="1200"/><meta property="og:image:height" content="630"/>

69.7 next/font: self-hosted fonts

  • Import Inter from next/font/google with the latin subset.
  • Next.js downloads the font at build time and serves it from your own site: no request to Google Fonts, no layout shift.
  • Put inter.className on <html> to apply it to the whole app; the font is preloaded.
React (TSX)
import { Inter } from "next/font/google"; const inter = Inter({ subsets: ["latin"] }); export default function RootLayout({  children,}: { children: React.ReactNode }) {  return (    <html lang="en" className={inter.className}>      <body>{children}</body>    </html>  );}
Output
$ curl -s localhost:3000/ | grep woff2<link rel="preload"  href="/_next/static/media/07c7…woff2"  as="font" crossorigin="" type="font/woff2"/>

69.8 next/image: smaller, stable images

  • A plain <img> downloads the full large image even on mobile, and content jumps when it loads (layout shift).
  • With a static import, width and height come automatically, so the browser reserves the space.
  • srcSet has widths from 640 up, so a phone gets a small image.
  • preload only on the hero image; all other images lazy load by default.
React (TSX)
import Image from "next/image";import shoes from "@/public/shoes.png"; export default function Home() {  return (    <Image      src={shoes}      alt="Running shoes"      sizes="100vw"      preload    />  );}
Output
$ curl -s localhost:3000/ | grep img<img alt="Running shoes" width="1280" height="720"  srcSet="/_next/image?url=…shoes…png&w=640&q=75 640w,  …&w=750 750w, …&w=1080 1080w, …&w=3840 3840w"

69.9 next/script: when to load

  • Third-party scripts (chat widget, analytics) slow the page if they load with it.
  • strategy="lazyOnload": load after the page has loaded and the browser is free.
React (TSX)
import Script from "next/script"; export default function ContactPage() {  return (    <>      <h1>Contact us</h1>      <Script        src="https://chat.example.com/widget.js"        strategy="lazyOnload"      />    </>  );}
StrategyWhen it loadsUse for
beforeInteractiveBefore the page becomes interactiveOnly truly critical scripts
afterInteractive (default)After the page becomes interactiveAnalytics, tag managers
lazyOnloadAfter load, when the browser is idleChat widgets, low priority
Optimisation pieces and the metric they help
  1. 1.next/font: self-hosted, preloaded -> no font layout shift (CLS)

  2. 2.next/image: width/height reserved -> no image layout shift (CLS)

  3. 3.next/image srcSet + preload on hero -> faster LCP

  4. 4.next/script lazyOnload -> main thread free for the page

69.10 Common mistakes

  • Putting "use client" on a whole page for one useState button and also exporting metadata: build error, metadata is resolved on the server.
  • Fix: keep the page a Server Component and move only the button into a separate Client Component.
  • Myth: Next.js = automatic SEO. Server HTML is a big plus, but unique titles, descriptions and good content are your job.
  • Also add sitemap.ts and robots.ts in the app folder; Core Web Vitals affect ranking too.
React (TSX)
// Wrong: build error, metadata + "use client""use client";import { useState } from "react"; export const metadata = { title: "Cart" }; export default function CartPage() {  const [qty, setQty] = useState(1);  return (    <button onClick={() => setQty(qty + 1)}>      Qty: {qty}    </button>  );}
React (TSX)
// Right: page on the server, button on the clientimport { QtyButton } from "./QtyButton"; export const metadata = { title: "Cart" }; export default function CartPage() {  return <QtyButton />;}

69.11 Best practices

  • Unique title and description on every page; title template and metadataBase in the layout.
  • Image, not img; preload only the main hero image.
  • Load fonts with next/font.
  • Load third-party scripts with Script and the right strategy.

69.12 Interview tips

  • Static pages export a metadata object; dynamic pages export generateMetadata (Server Components only).
  • An opengraph-image file generates the share image; Next.js adds the og tags.
  • title.template + metadataBase in the root layout keep titles unique and URLs absolute.
  • next/image, next/font and next/script improve Core Web Vitals like LCP and CLS.