Skip to content

Commit

Permalink
Merge pull request #47 from seemorg/dev
Browse files Browse the repository at this point in the history
split sitemaps
  • Loading branch information
ahmedriad1 authored Dec 12, 2024
2 parents 1e13214 + 3ab0e2b commit 5f97d71
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 80 deletions.
80 changes: 0 additions & 80 deletions src/app/sitemap.ts

This file was deleted.

41 changes: 41 additions & 0 deletions src/app/sitemap.xml/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { relativeUrl } from "@/lib/sitemap";
import { generateSitemaps } from "../sitemap/sitemap";
import { env } from "@/env";

export const revalidate = 60 * 60 * 24 * 7; // cache for 7 days

function getLoc(id: number) {
return relativeUrl(`/sitemap/${id}.xml`);
}

function getLastMod() {
return new Date().toISOString();
}

function getSitemap(id: number) {
return `<sitemap><loc>${getLoc(id)}</loc><lastmod>${getLastMod()}</lastmod></sitemap>`;
}

function getSitemaps(ids: { id: number }[]) {
return ids.map(({ id }) => getSitemap(id)).join("");
}

export async function GET() {
const sitemapIds = generateSitemaps();

// We only want to generate the sitemap.xml file in production
const isProd = env.VERCEL_ENV === "production";
if (!isProd) return Response.json({ error: "Not found" }, { status: 404 });

const xml = `<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${getSitemaps(sitemapIds as any)}
</sitemapindex>
`;

return new Response(xml, {
headers: {
"Content-Type": "application/xml",
},
});
}
88 changes: 88 additions & 0 deletions src/app/sitemap/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { env } from "@/env";
import { localesWithoutDefault, relativeUrl } from "@/lib/sitemap";
import { navigation } from "@/lib/urls";
import { db } from "@/server/db";
import { findAllYearRanges } from "@/server/services/years";
import type { MetadataRoute } from "next";

const rootEntityPages = [
navigation.books.all(),
navigation.authors.all(),
navigation.centuries.all(),
navigation.regions.all(),
navigation.genres.all(),
];

const generateEntryFromUrl = (url: string) => ({
url: relativeUrl(url),
lastModified: new Date(),
alternates: {
languages: localesWithoutDefault.reduce(
(acc, locale) => {
acc[locale] = relativeUrl(`/${locale}${url === "/" ? "" : url}`);
return acc;
},
{} as Record<string, string>,
),
},
});

export function generateSitemaps() {
// TODO: for now we have only 2 sitemaps, change that later to be dynamic
return [{ id: 1 }, { id: 2 }] as const;
}

export default async function sitemap(
params: ReturnType<typeof generateSitemaps>[number],
): Promise<MetadataRoute.Sitemap> {
const isProd = env.VERCEL_ENV === "production";
if (!isProd) return [];

if (params.id === 1) {
const regions = await db.region.findMany({
select: { slug: true },
});
const centuries = await findAllYearRanges();
const authors = await db.author.findMany({
select: { slug: true },
});
const genres = await db.genre.findMany({
select: { slug: true },
});

return [
// home page
generateEntryFromUrl("/"),
// about page
generateEntryFromUrl("/about"),
// team page
generateEntryFromUrl("/team"),
// root entities
...rootEntityPages.map((entityUrl) => generateEntryFromUrl(entityUrl)),
...authors.map((author) =>
generateEntryFromUrl(navigation.authors.bySlug(author.slug)),
),
...centuries.map((century) =>
generateEntryFromUrl(
navigation.centuries.byNumber(century.centuryNumber),
),
),
...regions.map((region) =>
generateEntryFromUrl(navigation.regions.bySlug(region.slug)),
),
...genres.map((genre) =>
generateEntryFromUrl(navigation.genres.bySlug(genre.slug)),
),
];
}

const books = await db.book.findMany({
select: { slug: true },
});

return [
...books.map((book) =>
generateEntryFromUrl(navigation.books.reader(book.slug)),
),
];
}

0 comments on commit 5f97d71

Please sign in to comment.