Skip to content

Commit

Permalink
feat(sitemap): add sitemap generation to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaggyTech committed May 1, 2023
1 parent a4d29f3 commit 4829041
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"prettier-config": "workspace:*",
"rimraf": "4.4.1",
"sass": "1.62.1",
"sitemap": "7.1.1",
"tsconfig": "workspace:*",
"typescript": "5.0.3",
"vitepress": "1.0.0-alpha.64"
Expand Down
43 changes: 43 additions & 0 deletions apps/docs/src/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { createWriteStream } from 'node:fs'
import { resolve } from 'node:path'
import { SitemapStream } from 'sitemap'
// import { generateSitemap } from 'sitemap-ts'
import { defineConfig, type HeadConfig } from 'vitepress'
import { nav, sidebar } from './menu'

/* Base URL for production deployment on Github Pages = shaggytech.com/nhtsa-api-wrapper */
/* Set env variable VITEPRESS_BASE to '/' for Vercel deployment previews */
const { VITEPRESS_BASE = '/nhtsa-api-wrapper/' } = process.env

interface SiteMapLink {
url: string
lastmod?: number
}

const sitemapLinks: SiteMapLink[] = []

export default defineConfig({
base: VITEPRESS_BASE,
lang: 'en-US',
Expand Down Expand Up @@ -44,6 +55,38 @@ export default defineConfig({
copyright: 'Copyright © 2017-present Brandon Eichler',
},
},

// buildEnd: async () => {
// return await new Promise((resolve) => {
// return resolve(
// generateSitemap({
// basePath: VITEPRESS_BASE,
// hostname: 'https://vpic.shaggytech.com/',
// outDir: 'src/.vitepress/dist',
// })
// )
// })
// },

transformHtml: (_, id, { pageData }) => {
if (!/[\\/]404\.html$/.test(id))
sitemapLinks.push({
// you might need to change this if not using clean urls mode
url: pageData.relativePath.replace(/((^|\/)index)?\.md$/, '$2'),
lastmod: pageData.lastUpdated,
})
},

buildEnd: async ({ outDir }) => {
const sitemap = new SitemapStream({
hostname: 'https://vpic.shaggytech.com/',
})
const writeStream = createWriteStream(resolve(outDir, 'sitemap.xml'))
sitemap.pipe(writeStream)
sitemapLinks.forEach((link) => sitemap.write(link))
sitemap.end()
await new Promise((r) => writeStream.on('finish', r))
},
})

function getHeadTags(): HeadConfig[] {
Expand Down
5 changes: 3 additions & 2 deletions apps/docs/src/public/robots.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
User-agent: *
Disallow:
User-agent: *
Allow: /

Sitemap: https://vpic.shaggytech.com/sitemap.xml
32 changes: 32 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4829041

Please sign in to comment.