Skip to content

Commit 680de92

Browse files
committed
Add sitemap.xml file
Generate a sitemap.xml during build time that is served at `/sitemap.xml`, and add a reference to it in `robots.txt` so that crawlers can find it. (See: https://developers.google.com/search/docs/crawling-indexing/sitemaps/build-sitemap#addsitemap) The sitemap entries' `lastmod` property comes from the last modification time of the source MDX files for each page. Note that this might not be completely accurate, as updates to MDX fragments in `/includes` or `/platform-includes` won't be considered. Pages generated from our OpenAPI specs are missing `lastmod` properties since we don't have a way to isolate the timestamp when individual pages change.
1 parent a87ee3e commit 680de92

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

app/sitemap.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
import type {MetadataRoute} from 'next';
5+
6+
import {getDocsFrontMatter} from 'sentry-docs/mdx';
7+
8+
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
9+
const docs = await getDocsFrontMatter();
10+
const sitemapEntries = docs.map(doc => ({
11+
url: `https://docs.sentry.io/${doc.slug}`,
12+
lastModified: doc.lastModified,
13+
}));
14+
sitemapEntries.unshift({
15+
url: 'https://docs.sentry.io/',
16+
lastModified: fs.statSync(
17+
path.join(process.cwd(), 'src', 'components', 'homeClient.tsx')
18+
).mtime,
19+
});
20+
return sitemapEntries;
21+
}

src/mdx.ts

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export function getAllFilesFrontMatter(folder: string = 'docs'): FrontMatter[] {
100100
...frontmatter,
101101
slug: formatSlug(fileName),
102102
sourcePath: path.join(folder, fileName),
103+
lastModified: fs.statSync(file).mtime,
103104
});
104105
}
105106
});
@@ -133,6 +134,7 @@ export function getAllFilesFrontMatter(folder: string = 'docs'): FrontMatter[] {
133134
const commonFiles = commonFileNames.map(commonFileName => {
134135
const source = fs.readFileSync(commonFileName, 'utf8');
135136
const {data: frontmatter} = matter(source);
137+
frontmatter.lastModified = fs.statSync(commonFileName).mtime;
136138
return {commonFileName, frontmatter};
137139
});
138140

static/robots.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
Sitemap: https://docs.sentry.io/sitemap.xml
12
User-agent: *
23
Disallow: /development-api/

0 commit comments

Comments
 (0)