-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from seemorg/dev
split sitemaps
- Loading branch information
Showing
3 changed files
with
129 additions
and
80 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
), | ||
]; | ||
} |