portfolio/src/lib/generateSitemap.js

67 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-01-14 19:31:05 +00:00
import glob from 'fast-glob'
import path from 'path'
import {getAllArticles} from '@/lib/getAllArticles'
import {writeFile} from 'fs/promises'
const BASE_URL = process.env.NEXT_PUBLIC_SITE_URL
async function createSitemap(pages) {
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${pages.map((url) => {
return `
<url>
<loc>${url}</loc>
<lastmod>${new Date().toISOString()}</lastmod>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>`
}).join("")}
</urlset>`
await writeFile('./public/sitemap.xml', sitemap, 'utf8')
}
async function createRobots() {
const robots = `# *
User-agent: *
Allow: /
# Host
Host: ${BASE_URL}
# Sitemaps
Sitemap: ${BASE_URL}/sitemap.xml`
await writeFile('./public/robots.txt', robots, 'utf8')
}
export async function generateSitemap() {
const excluded = [
'_app.tsx',
'_document.tsx',
'index.tsx'
]
const pages = (await glob(['*.tsx', '*.jsx'], {
cwd: path.join(process.cwd(), 'src/pages/'),
})).filter((page) => {
return !excluded
.includes(page)
}).map((page) => {
return `${BASE_URL}/${page}`
.replace(/\.(tsx|jsx)$/, '')
})
pages.unshift(`${BASE_URL}/`)
pages.push(`${BASE_URL}/writing`)
const articles = await getAllArticles()
const slugs = articles.map(({slug}) => `${BASE_URL}/writing/${slug}`)
const allPages = [...pages, ...slugs]
await Promise.all([
await createSitemap(allPages),
await createRobots()
])
}