This repository was archived by the owner on May 20, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +56
-7
lines changed
src/app/(sitemaps)/sitemap-0.xml Expand file tree Collapse file tree 4 files changed +56
-7
lines changed Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff line change 11import fs from 'fs/promises'
22
33import { allDocs } from '../.contentlayer/generated/index.mjs'
4+ import pages from '../src/assets/sitemap.json' with { type : 'json' }
45
56const FIXTURE_PATH = 'cypress/fixtures/pages.json'
67
78async 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
Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 11import { allDocs } from '@/content'
2+ import staticPaths from '@/assets/sitemap.json'
23
34const 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.
1316export 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
You can’t perform that action at this time.
0 commit comments