SEO and Metadata · Lesson 19 of 24
Sitemap, robots.txt, OG Images and JSON-LD in Next.js
Beyond page tags, good technical SEO needs a few special files: a sitemap listing your URLs, a robots.txt telling crawlers what to skip, Open Graph images for attractive link previews, and structured data that can earn rich results in Google. Next.js generates all of them from TypeScript files placed in app/.
In this lesson
sitemap.tsandrobots.ts- Generated Open Graph images with
ImageResponse - JSON-LD structured data
sitemap.ts
// app/sitemap.ts
import type { MetadataRoute } from "next";
const courses = ["react", "nextjs", "typescript"]; // in a real app: from the database
export default function sitemap(): MetadataRoute.Sitemap {
const base = "https://www.learnfrenzy.com";
return [
{ url: base, changeFrequency: "daily", priority: 1 },
{ url: `${base}/courses`, changeFrequency: "weekly", priority: 0.8 },
...courses.map((slug) => ({ url: `${base}/courses/${slug}`, changeFrequency: "monthly" as const, priority: 0.6 })),
];
}
robots.ts
// app/robots.ts
import type { MetadataRoute } from "next";
export default function robots(): MetadataRoute.Robots {
return {
rules: [{ userAgent: "*", allow: "/", disallow: ["/dashboard/", "/api/"] }],
sitemap: "https://www.learnfrenzy.com/sitemap.xml",
};
}
GET /sitemap.xml → 200 application/xml <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>https://www.learnfrenzy.com</loc> <changefreq>daily</changefreq> <priority>1</priority> </url> <url> <loc>https://www.learnfrenzy.com/courses</loc> <changefreq>weekly</changefreq> <priority>0.8</priority> </url> <url> <loc>https://www.learnfrenzy.com/courses/react</loc> <changefreq>monthly</changefreq> <priority>0.6</priority> </url> <url> <loc>https://www.learnfrenzy.com/courses/nextjs</loc> <changefreq>monthly</changefreq> <priority>0.6</priority> </url> <url> <loc>https://www.learnfrenzy.com/courses/typescript</loc> <changefreq>monthly</changefreq> <priority>0.6</priority> </url> </urlset> GET /robots.txt → 200 text/plain User-Agent: * Allow: / Disallow: /dashboard/ Disallow: /api/ Sitemap: https://www.learnfrenzy.com/sitemap.xml
Both are served at the site root and prerendered at build time. For very large sites (over 50,000 URLs), generateSitemaps splits the sitemap into several files. Don't list pages that are noindexed, redirected or behind a login.
Open Graph images
A link shared on WhatsApp, LinkedIn or X looks far better with a preview image. Instead of designing one per page, generate it: an opengraph-image.tsx file exports JSX that ImageResponse turns into a PNG, and Next.js adds the og:image tags automatically:
// app/courses/[slug]/opengraph-image.tsx
import { ImageResponse } from "next/og";
export const alt = "LearnFrenzy course";
export const size = { width: 1200, height: 630 };
export const contentType = "image/png";
export function generateStaticParams() {
return [{ slug: "react" }, { slug: "nextjs" }];
}
export default async function Image({ params }: { params: Promise<{ slug: string }> }) {
const { slug } = await params;
const title = slug === "nextjs" ? "Next.js Tutorial" : "React.js Tutorial";
return new ImageResponse(
(
<div style={{ width: "100%", height: "100%", display: "flex", flexDirection: "column", justifyContent: "center", padding: 80, background: "#0f172a", color: "white" }}>
<div style={{ fontSize: 32, color: "#38bdf8" }}>LearnFrenzy</div>
<div style={{ fontSize: 72, fontWeight: 700 }}>{title}</div>
<div style={{ fontSize: 32, opacity: 0.8 }}>Free · Hands-on · Certificate</div>
</div>
),
size
);
}
// app/courses/[slug]/page.tsx
export function generateStaticParams() {
return [{ slug: "react" }, { slug: "nextjs" }];
}
export default async function Course({ params }: PageProps<"/courses/[slug]">) {
const { slug } = await params;
return <h1>{slug} course</h1>;
}
// app/layout.tsx
import type { Metadata } from "next";
import "./globals.css";
export const metadata: Metadata = { metadataBase: new URL("https://www.learnfrenzy.com") };
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
GET /courses/nextjs/opengraph-image → 200 image/png (image/png binary data) <meta property="og:image" content="https://www.learnfrenzy.com/courses/nextjs/opengraph-image"> <meta property="og:image:type" content="image/png"> <meta property="og:image:width" content="1200"> <meta property="og:image:height" content="630"> <meta property="og:image:alt" content="LearnFrenzy course"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:image" content="https://www.learnfrenzy.com/courses/nextjs/opengraph-image"> <meta name="twitter:image:alt" content="LearnFrenzy course"> <meta name="twitter:image:type" content="image/png"> <meta name="twitter:image:width" content="1200"> <meta name="twitter:image:height" content="630"> <link rel="icon" href="/favicon.ico">
ImageResponse supports a subset of CSS (flexbox layout, no grid) and can load custom fonts. Static images work too: drop an opengraph-image.png into a route folder. A twitter-image file does the same for X.
JSON-LD structured data
Structured data describes your content to search engines in a standard vocabulary (schema.org). For a course page it can enable course rich results. Render it as a script tag from the page:
// app/page.tsx
export default function CoursePage() {
const jsonLd = {
"@context": "https://schema.org",
"@type": "Course",
name: "Next.js Tutorial",
description: "Learn Next.js 16: App Router, Server Components, caching and SEO.",
provider: { "@type": "Organization", name: "LearnFrenzy", sameAs: "https://www.learnfrenzy.com" },
isAccessibleForFree: true,
};
return (
<main>
<script
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd).replace(/</g, "\\u003c") }}
/>
<h1>Next.js Tutorial</h1>
</main>
);
}
<link rel="icon" href="/favicon.ico">
<script type="application/ld+json"> (in body) {"@context":"https://schema.org","@type":"Course","name":"Next.js Tutorial","description":"Learn Next.js 16: App Router, Server Components, caching and SEO.","provider":{"@type":"Organization","name":"LearnFrenzy","sameAs":"https://www.learnfrenzy.com"},"isAccessibleForFree":true}</script>
Next.js TutorialEscape the JSON
Replacing < with < stops data containing </script> from breaking out of the tag – important when the JSON includes user-generated text. Test your markup with Google's Rich Results Test, and only describe content that's actually visible on the page.
Practice
Try it yourself
Write a
robots.tsthat blocks every crawler from the whole site (useful for a staging deployment).Show solution
// app/robots.ts import type { MetadataRoute } from "next"; export default function robots(): MetadataRoute.Robots { return { rules: { userAgent: "*", disallow: "/" } }; }GET /robots.txt → 200 text/plain User-Agent: * Disallow: /
Make it depend on an environment variable so production never ships it by mistake.
Quick check
Where is app/sitemap.ts served?
At /sitemap.xml.
What does opengraph-image.tsx produce?
A generated PNG plus the og:image meta tags for that route.
Why replace < in JSON-LD?
To prevent a "</script>" inside the data from injecting HTML.
Summary
- sitemap.ts and robots.ts generate /sitemap.xml and /robots.txt from code.
- opengraph-image.tsx renders preview images with ImageResponse and adds the tags automatically.
- Add schema.org JSON-LD in a script tag, with < escaped.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.