Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 3f98b54

Browse files
committed
sitemap and robots
1 parent 5fba1bc commit 3f98b54

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { allDocs } from '@/content'
2+
3+
const URL = 'https://nitric.io/docs'
4+
5+
interface SitemapItem {
6+
loc: string
7+
lastmod: string
8+
changefreq: string
9+
priority: number
10+
}
11+
12+
const lastmod = new Date().toISOString()
13+
// Function to construct the XML structure of the sitemap index.
14+
export async function GET() {
15+
console.log(allDocs)
16+
const pages: SitemapItem[] = allDocs.map((page) => ({
17+
loc: page.slug === '' ? URL : `${URL}/${page.slug}`,
18+
lastmod,
19+
changefreq: 'daily',
20+
priority: 0.7,
21+
}))
22+
23+
const allPagesSorted = [...pages].sort((a, b) => (a.loc < b.loc ? -1 : 1))
24+
25+
const sitemapIndexXML = buildSitemap(allPagesSorted)
26+
27+
// Return the sitemap index XML with the appropriate content type.
28+
return new Response(sitemapIndexXML, {
29+
headers: {
30+
'Content-Type': 'application/xml',
31+
},
32+
})
33+
}
34+
35+
function buildSitemap(items: SitemapItem[]) {
36+
let xml = '<?xml version="1.0" encoding="UTF-8"?>'
37+
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
38+
39+
for (const pageURL of items) {
40+
xml += '<url>'
41+
xml += `<loc>${pageURL.loc}</loc>`
42+
xml += `<lastmod>${pageURL.lastmod}</lastmod>` // Set the <lastmod> to the current date in IST
43+
xml += '</url>'
44+
}
45+
46+
xml += '</urlset>'
47+
return xml
48+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Function to construct the XML structure of the sitemap index.
2+
export async function GET() {
3+
const sitemapIndexXML = buildSitemapIndex([
4+
'https://nitric.io/docs/sitemap-0.xml',
5+
])
6+
7+
// Return the sitemap index XML with the appropriate content type.
8+
return new Response(sitemapIndexXML, {
9+
headers: {
10+
'Content-Type': 'application/xml',
11+
},
12+
})
13+
}
14+
15+
function buildSitemapIndex(sitemaps: string[]) {
16+
// XML declaration and opening tag for the sitemap index.
17+
let xml = '<?xml version="1.0" encoding="UTF-8"?>'
18+
xml += '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
19+
20+
// Iterate over each sitemap URL and add it to the sitemap index.
21+
for (const sitemapURL of sitemaps) {
22+
xml += '<sitemap>'
23+
xml += `<loc>${sitemapURL}</loc>` // Location tag specifying the URL of a sitemap file.
24+
xml += '</sitemap>'
25+
}
26+
27+
// Closing tag for the sitemap index.
28+
xml += '</sitemapindex>'
29+
return xml
30+
}

src/app/robots.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { MetadataRoute } from 'next'
2+
3+
export default function robots(): MetadataRoute.Robots {
4+
// staging robots.txt
5+
if (process.env.NEXT_PUBLIC_VERCEL_ENV !== 'production') {
6+
return {
7+
rules: {
8+
userAgent: '*',
9+
disallow: '/',
10+
},
11+
}
12+
}
13+
14+
// production robots.txt
15+
return {
16+
rules: {
17+
userAgent: '*',
18+
allow: '/',
19+
},
20+
host: 'https://nitric.io/docs',
21+
sitemap: 'https://nitric.io/docs/sitemap.xml',
22+
}
23+
}

0 commit comments

Comments
 (0)