Skip to the code

A sitemap built from the teas and the journal

Leaf and Kettle wants search engines to find every tea and every journal post. Write app/sitemap.ts: its default export returns a MetadataRoute.Sitemap array, one entry per page, and Next.js serves it as /sitemap.xml. getTeas() and getPosts() in @/lib/content return everything in the database.

Each entry's url is a full URL on SITE_URL: https://leafandkettle.example for the home page, and https://leafandkettle.example/teas/green for the green tea. List the pages in the table below, in any order, each with its lastModified (a Date), changeFrequency, and priority.

Leave out retired teas, whose pages are noindex, and drafts, posts without a publishedAt, which aren't on the site yet. They don't count toward the list pages' dates either: a list page changes when a page it shows does.

When a list page has nothing to take its date from, like a journal with no published posts, it's still listed, without a lastModified.

page              lastModified                                changeFrequency  priority
/                 the newest lastModified of all the pages     daily            1
/teas             the newest of the listed teas'               daily            0.9
/teas/<slug>      the tea's updatedAt                          weekly           0.8
/journal          the newest of the listed posts'              weekly           0.7
/journal/<slug>   the post's updatedAt, or its publishedAt     monthly          0.6
 
{ url: "https://leafandkettle.example/teas/green", lastModified: new Date("2026-10-02T09:00:00Z"), changeFrequency: "weekly", priority: 0.8 }

The app's other files

lib/content.ts, read-only. Import it as @/lib/content.

/** The shop's address in production. Every URL in a sitemap is a full URL. */
export const SITE_URL = "https://leafandkettle.example";
 
export type Tea = { slug: string; name: string; updatedAt: Date; retired: boolean };
export type Post = { slug: string; title: string; publishedAt: Date | null; updatedAt: Date | null };
 
/** Every tea, retired ones too: their pages stay up for their reviews, marked noindex. */
export const teas: Tea[] = [
  { slug: "green", name: "Green tea", updatedAt: new Date("2026-10-02T09:00:00Z"), retired: false },
  { slug: "mint", name: "Mint tea", updatedAt: new Date("2026-09-14T16:30:00Z"), retired: false },
  { slug: "oolong", name: "Oolong tea", updatedAt: new Date("2026-10-20T08:15:00Z"), retired: false },
  { slug: "winter-blend", name: "Winter blend", updatedAt: new Date("2026-11-01T10:00:00Z"), retired: true },
];
 
/** Every journal post, newest first. A draft has no publishedAt, and a post never edited has no updatedAt. */
export const posts: Post[] = [
  { slug: "spring-harvest", title: "The spring harvest is in", publishedAt: null, updatedAt: new Date("2026-11-05T18:00:00Z") },
  { slug: "brewing-oolong", title: "How to brew oolong", publishedAt: new Date("2026-08-01T07:00:00Z"), updatedAt: new Date("2026-10-25T12:00:00Z") },
  { slug: "cold-brew-season", title: "Cold brew season", publishedAt: new Date("2026-06-10T07:00:00Z"), updatedAt: null },
];
 
/** Every tea in the database. */
export async function getTeas(): Promise<Tea[]> {
  return teas;
}
 
/** Every journal post in the database, drafts too. */
export async function getPosts(): Promise<Post[]> {
  return posts;
}

What the tests check

  1. lists the home page, both list pages, every tea, and every published post, by full URL
    // The entries by URL, without a trailing slash, and an entry's lastModified as an ISO string.
    const entries = async () => new Map((await Sitemap()).map((entry) => [entry.url.replace(/\/$/, ""), entry]));
    const modified = (entry) => {
      if (entry.lastModified === undefined || entry.lastModified === null) return undefined;
      const date = new Date(entry.lastModified);
      return Number.isNaN(date.getTime()) ? "Invalid Date" : date.toISOString();
    };
    expect([...(await entries()).keys()].sort()).toEqual(
      [
        "https://leafandkettle.example",
        "https://leafandkettle.example/teas",
        "https://leafandkettle.example/teas/green",
        "https://leafandkettle.example/teas/mint",
        "https://leafandkettle.example/teas/oolong",
        "https://leafandkettle.example/journal",
        "https://leafandkettle.example/journal/brewing-oolong",
        "https://leafandkettle.example/journal/cold-brew-season",
      ].sort(),
    );
    expect((await Sitemap()).length).toBe(8);
    
  2. leaves out retired teas and draft posts
    // The entries by URL, without a trailing slash, and an entry's lastModified as an ISO string.
    const entries = async () => new Map((await Sitemap()).map((entry) => [entry.url.replace(/\/$/, ""), entry]));
    const modified = (entry) => {
      if (entry.lastModified === undefined || entry.lastModified === null) return undefined;
      const date = new Date(entry.lastModified);
      return Number.isNaN(date.getTime()) ? "Invalid Date" : date.toISOString();
    };
    const urls = [...(await entries()).keys()];
    expect(urls).not.toContain("https://leafandkettle.example/teas/winter-blend");
    expect(urls).not.toContain("https://leafandkettle.example/journal/spring-harvest");
    
  3. each tea and post has its own date, change frequency, and priority
    // The entries by URL, without a trailing slash, and an entry's lastModified as an ISO string.
    const entries = async () => new Map((await Sitemap()).map((entry) => [entry.url.replace(/\/$/, ""), entry]));
    const modified = (entry) => {
      if (entry.lastModified === undefined || entry.lastModified === null) return undefined;
      const date = new Date(entry.lastModified);
      return Number.isNaN(date.getTime()) ? "Invalid Date" : date.toISOString();
    };
    const sitemap = await entries();
    const green = sitemap.get("https://leafandkettle.example/teas/green");
    expect(green).toBeDefined();
    expect(modified(green)).toBe("2026-10-02T09:00:00.000Z");
    expect(green.changeFrequency).toBe("weekly");
    expect(green.priority).toBe(0.8);
    const post = sitemap.get("https://leafandkettle.example/journal/brewing-oolong");
    expect(post).toBeDefined();
    expect(modified(post)).toBe("2026-10-25T12:00:00.000Z");
    expect(post.changeFrequency).toBe("monthly");
    expect(post.priority).toBe(0.6);
    
  4. a post that was never edited goes by the day it was published
    // The entries by URL, without a trailing slash, and an entry's lastModified as an ISO string.
    const entries = async () => new Map((await Sitemap()).map((entry) => [entry.url.replace(/\/$/, ""), entry]));
    const modified = (entry) => {
      if (entry.lastModified === undefined || entry.lastModified === null) return undefined;
      const date = new Date(entry.lastModified);
      return Number.isNaN(date.getTime()) ? "Invalid Date" : date.toISOString();
    };
    const post = (await entries()).get("https://leafandkettle.example/journal/cold-brew-season");
    expect(post).toBeDefined();
    expect(modified(post)).toBe("2026-06-10T07:00:00.000Z");
    
  5. the home and list pages changed when the newest page they show did
    // The entries by URL, without a trailing slash, and an entry's lastModified as an ISO string.
    const entries = async () => new Map((await Sitemap()).map((entry) => [entry.url.replace(/\/$/, ""), entry]));
    const modified = (entry) => {
      if (entry.lastModified === undefined || entry.lastModified === null) return undefined;
      const date = new Date(entry.lastModified);
      return Number.isNaN(date.getTime()) ? "Invalid Date" : date.toISOString();
    };
    const sitemap = await entries();
    const home = sitemap.get("https://leafandkettle.example");
    const teaList = sitemap.get("https://leafandkettle.example/teas");
    const journal = sitemap.get("https://leafandkettle.example/journal");
    expect([home, teaList, journal].every(Boolean)).toBe(true);
    // Not the retired tea's November 1, nor the draft's November 5: neither is on any page.
    expect(modified(teaList)).toBe("2026-10-20T08:15:00.000Z");
    expect(modified(journal)).toBe("2026-10-25T12:00:00.000Z");
    expect(modified(home)).toBe("2026-10-25T12:00:00.000Z");
    expect([home.changeFrequency, home.priority]).toEqual(["daily", 1]);
    expect([teaList.changeFrequency, teaList.priority]).toEqual(["daily", 0.9]);
    expect([journal.changeFrequency, journal.priority]).toEqual(["weekly", 0.7]);
    
  6. with nothing published, the journal has no date and the home page goes by the teas
    // The entries by URL, without a trailing slash, and an entry's lastModified as an ISO string.
    const entries = async () => new Map((await Sitemap()).map((entry) => [entry.url.replace(/\/$/, ""), entry]));
    const modified = (entry) => {
      if (entry.lastModified === undefined || entry.lastModified === null) return undefined;
      const date = new Date(entry.lastModified);
      return Number.isNaN(date.getTime()) ? "Invalid Date" : date.toISOString();
    };
    for (const post of posts) post.publishedAt = null;
    const sitemap = await entries();
    expect([...sitemap.keys()].filter((url) => url.includes("/journal/"))).toEqual([]);
    const journal = sitemap.get("https://leafandkettle.example/journal");
    expect(journal).toBeDefined();
    expect(modified(journal)).toBeUndefined();
    expect(modified(sitemap.get("https://leafandkettle.example"))).toBe("2026-10-20T08:15:00.000Z");
    

Learn more

app/sitemap.tsSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).