From 924e45ef7a50cab052e7a3609ae5cb2923d2a6cf Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sat, 11 May 2024 20:58:04 +0200 Subject: [PATCH] chore(website): add sitemap to website --- website/app/robots.ts | 2 ++ website/app/sitemap.ts | 28 +++++++++++++++++++++++++ website/content-collections.ts | 37 ++++++++++++++++++++++++++++++++-- website/lib/env.ts | 1 + 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 website/app/sitemap.ts create mode 100644 website/lib/env.ts diff --git a/website/app/robots.ts b/website/app/robots.ts index fc42c639..101eb95f 100644 --- a/website/app/robots.ts +++ b/website/app/robots.ts @@ -1,3 +1,4 @@ +import { FQDN } from "@/lib/env"; import { MetadataRoute } from "next"; export default function robots(): MetadataRoute.Robots { @@ -6,5 +7,6 @@ export default function robots(): MetadataRoute.Robots { userAgent: "*", allow: "/", }, + sitemap: `${FQDN}/sitemap.xml`, }; } diff --git a/website/app/sitemap.ts b/website/app/sitemap.ts new file mode 100644 index 00000000..44b96eb0 --- /dev/null +++ b/website/app/sitemap.ts @@ -0,0 +1,28 @@ +import { FQDN } from "@/lib/env"; +import { allDocs, allQuickstarts, allSamples } from "content-collections"; +import { MetadataRoute } from "next"; + +export default function sitemap(): MetadataRoute.Sitemap { + return [ + { + url: `${FQDN}/`, + lastModified: new Date(), + priority: 1, + }, + ...allQuickstarts.map((quickstart) => ({ + url: `${FQDN}/${quickstart.href}`, + lastModified: quickstart.lastModified, + priority: 0.8, + })), + ...allDocs.map((docPage) => ({ + url: `${FQDN}/${docPage.href}`, + lastModified: docPage.lastModified, + priority: 0.7, + })), + ...allSamples.map((sample) => ({ + url: `${FQDN}/${sample.href}`, + lastModified: sample.lastModified, + priority: 0.7, + })), + ]; +} diff --git a/website/content-collections.ts b/website/content-collections.ts index ce681002..39d3ef5f 100644 --- a/website/content-collections.ts +++ b/website/content-collections.ts @@ -1,10 +1,21 @@ -import { defineCollection, defineConfig } from "@content-collections/core"; +import { + Context, + Meta, + defineCollection, + defineConfig, + Document, +} from "@content-collections/core"; import rehypeSlug from "rehype-slug"; import rehypeShiki from "@shikijs/rehype"; import { Options, compileMDX } from "@content-collections/mdx"; import { selectAll } from "hast-util-select"; import { Root } from "hast"; import GithubSlugger from "github-slugger"; +import { exec as cpExec } from "node:child_process"; +import { promisify } from "node:util"; +import path from "node:path"; + +const exec = promisify(cpExec); function liCodeSlug() { return (tree: Root) => { @@ -20,6 +31,26 @@ function liCodeSlug() { }; } +async function lastModificationDate(ctx: Context, document: Document) { + return ctx.cache( + // TODO: this is a dirty hack to avoid cache key conflicts + // we should find a way which handles this automatically + { key: "_git_last_modified", ...document }, + async (document) => { + const filePath = path.join( + ctx.collection.directory, + document._meta.filePath + ); + + const { stdout } = await exec(`git log -1 --format=%ai -- ${filePath}`); + if (stdout) { + return new Date(stdout.trim()).toISOString(); + } + return new Date().toISOString(); + } + ); +} + const mdxOptions: Options = { rehypePlugins: [ liCodeSlug, @@ -57,6 +88,7 @@ const quickstart = defineCollection({ name, body, category: data.category, + lastModified: await lastModificationDate(ctx, data), }; }, }); @@ -89,11 +121,11 @@ const samples = defineCollection({ name, body, tags: data.tags, + lastModified: await lastModificationDate(ctx, data), }; }, }); - const docs = defineCollection({ name: "docs", directory: "../docs", @@ -126,6 +158,7 @@ const docs = defineCollection({ body, href, slug, + lastModified: await lastModificationDate(ctx, data), }; }, }); diff --git a/website/lib/env.ts b/website/lib/env.ts new file mode 100644 index 00000000..7d7c79c8 --- /dev/null +++ b/website/lib/env.ts @@ -0,0 +1 @@ +export const FQDN = "https://www.content-collections.dev"; \ No newline at end of file