aaronjy-me/next-sitemap.config.cjs
2025-03-21 13:03:13 +00:00

70 lines
1.5 KiB
JavaScript

const fs = require('fs')
const fm = require('front-matter')
/** @type {import('next-sitemap').IConfig} */
module.exports = {
siteUrl: process.env.SITE_URL || 'https://www.aaronjy.me',
changefreq: 'weekly',
generateRobotsTxt: true,
autoLastmod: false,
generateIndexSitemap: false,
robotsTxtOptions: {
policies: [
{
userAgent: '*',
allow: '/'
}
]
},
transform: async (config, path) => {
const metadata = {
loc: path
}
if (isHomepage(path)) {
metadata.priority = 1
} else if (isBasePage(path)) {
metadata.priority = 0.8
} else {
if (isArticle(path)) {
metadata.priority = 0.6
const attributes = getArticleAttibutes(`content${path}.md`)
console.log(attributes)
if (!attributes) { return null }
metadata.lastmod = attributes.moddate ?? attributes.pubdate ?? null
console.log('Calculated sitemap dates for article', path)
}
}
return metadata
}
}
function isHomepage (path) {
return path === '/'
}
function isBasePage (path) {
return path.split('/').length === 2
}
function isArticle (path) {
return path.startsWith('/writing/')
}
function getArticleAttibutes (path) {
const fileContents = fs.readFileSync(path, {
encoding: 'utf-8'
})
// @ts-ignore
const { attributes } = fm(fileContents)
return {
...attributes,
pubdate: attributes.pubdate?.toUTCString() ?? null,
moddate: attributes.moddate?.toUTCString() ?? null
}
}