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

Commit 1a2f891

Browse files
committed
add base pages (guides) to sitemap and tests
1 parent 19ebabb commit 1a2f891

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
"scripts": {
66
"postinstall": "husky && prisma generate && node scripts/collect-assets",
77
"dev": "next dev",
8+
"prebuild": "yarn generate-sitemap-pages",
89
"build": "next build",
910
"start": "next start",
1011
"lint": "next lint",
1112
"test:spellcheck": "spellchecker",
13+
"generate-sitemap-pages": "node scripts/build-sitemap",
1214
"format:check": "prettier --check src",
1315
"format:fix": "prettier --write src",
1416
"cypress": "cypress open",

scripts/build-pages-fixture.mjs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import fs from 'fs/promises'
22

33
import { allDocs } from '../.contentlayer/generated/index.mjs'
4+
import pages from '../src/assets/sitemap.json' with { type: 'json' }
45

56
const FIXTURE_PATH = 'cypress/fixtures/pages.json'
67

78
async function run() {
89
try {
9-
const paths = allDocs
10-
.map((doc) => {
11-
return doc.slug === '/' ? '/docs' : `/docs/${doc.slug}`
12-
})
13-
.sort((a, b) => a.localeCompare(b))
10+
const basePages = pages.map((p) => p.replace('https://nitric.io', ''))
11+
12+
const docPages = allDocs.map((doc) => {
13+
return doc.slug === '/' ? '/docs' : `/docs/${doc.slug}`
14+
})
15+
16+
const paths = [...basePages, ...docPages].sort((a, b) => a.localeCompare(b))
1417

1518
console.log(`${paths.length} paths found. Generating fixture`)
1619

scripts/build-sitemap.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const fs = require('fs/promises')
2+
3+
const excludedDirectories = ['[[...slug]]'] // ignore dynamic routes
4+
const pages = []
5+
6+
const URL = 'https://nitric.io/docs'
7+
8+
const readDirRecursive = async (dir) => {
9+
const files = await fs.readdir(dir)
10+
11+
for (const file of files) {
12+
const filePath = `${dir}/${file}`
13+
const stats = await fs.stat(filePath)
14+
if (stats.isDirectory() && !excludedDirectories.includes(file)) {
15+
await readDirRecursive(filePath)
16+
} else if (file.startsWith('page.')) {
17+
const loc = `${URL}${filePath
18+
.replace('src/app', '')
19+
.replace('.tsx', '')
20+
.replace('.mdx', '')
21+
.replace('page', '')}`.replace(/\/$/, '')
22+
23+
pages.push(loc)
24+
}
25+
}
26+
}
27+
28+
readDirRecursive('src/app').then(async () => {
29+
console.log('static pages for sitemap: ', pages)
30+
31+
await fs.writeFile('./src/assets/sitemap.json', JSON.stringify(pages))
32+
})

src/app/(sitemaps)/sitemap-0.xml/route.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { allDocs } from '@/content'
2+
import staticPaths from '@/assets/sitemap.json'
23

34
const URL = 'https://nitric.io/docs'
45

@@ -9,16 +10,27 @@ interface SitemapItem {
910
priority: number
1011
}
1112

13+
const lastmod = new Date().toISOString()
14+
1215
// Function to construct the XML structure of the sitemap index.
1316
export async function GET() {
14-
const pages: SitemapItem[] = allDocs.map((page) => ({
17+
const pages = staticPaths.map((page) => ({
18+
loc: page,
19+
lastmod,
20+
changefreq: 'daily',
21+
priority: 0.7,
22+
}))
23+
24+
const docPages: SitemapItem[] = allDocs.map((page) => ({
1525
loc: page.slug === '' ? URL : `${URL}/${page.slug}`,
1626
lastmod: new Date(page.lastModified).toISOString(),
1727
changefreq: 'daily',
1828
priority: 0.7,
1929
}))
2030

21-
const allPagesSorted = [...pages].sort((a, b) => (a.loc < b.loc ? -1 : 1))
31+
const allPagesSorted = [...pages, ...docPages].sort((a, b) =>
32+
a.loc < b.loc ? -1 : 1,
33+
)
2234

2335
const sitemapIndexXML = buildSitemap(allPagesSorted)
2436

0 commit comments

Comments
 (0)