mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-23 15:09:46 +00:00
优化robots.txt\sitemap.xml
This commit is contained in:
19
lib/robots.txt.js
Normal file
19
lib/robots.txt.js
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
import fs from 'fs'
|
||||
import BLOG from '@/blog.config'
|
||||
|
||||
export async function generateRobotsTxt() {
|
||||
fs.mkdirSync('./public/rss', { recursive: true })
|
||||
fs.writeFileSync('./public/robots.txt', `
|
||||
# *
|
||||
User-agent: *
|
||||
Allow: /
|
||||
|
||||
# Host
|
||||
Host: ${BLOG.LINK}
|
||||
|
||||
# Sitemaps
|
||||
Sitemap: ${BLOG.LINK}/sitemap.xml
|
||||
|
||||
`)
|
||||
}
|
||||
57
lib/sitemap.xml.js
Normal file
57
lib/sitemap.xml.js
Normal file
@@ -0,0 +1,57 @@
|
||||
|
||||
import fs from 'fs'
|
||||
import BLOG from '@/blog.config'
|
||||
|
||||
export async function generateSitemapXml({ allPages }) {
|
||||
fs.mkdirSync('./public', { recursive: true })
|
||||
|
||||
const urls = [{
|
||||
loc: `${BLOG.LINK}`,
|
||||
lastmod: new Date().toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
}, {
|
||||
loc: `${BLOG.LINK}/archive`,
|
||||
lastmod: new Date().toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
}, {
|
||||
loc: `${BLOG.LINK}/category`,
|
||||
lastmod: new Date().toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
}, {
|
||||
loc: `${BLOG.LINK}/tag`,
|
||||
lastmod: new Date().toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
}]
|
||||
|
||||
allPages?.forEach(post => {
|
||||
urls.push({
|
||||
loc: `${BLOG.LINK}/${post.slug}`,
|
||||
lastmod: new Date(post?.date?.start_date || post?.createdTime).toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
})
|
||||
})
|
||||
const xml = createSitemapXml(urls)
|
||||
fs.writeFileSync('./public/sitemap.xml', xml)
|
||||
}
|
||||
|
||||
function createSitemapXml(urls) {
|
||||
let urlsXml = ''
|
||||
urls.forEach(u => {
|
||||
urlsXml += `<url>
|
||||
<loc>${u.loc}</loc>
|
||||
<lastmod>${u.lastmod}</lastmod>
|
||||
<changefreq>${u.changefreq}</changefreq>
|
||||
</url>
|
||||
`
|
||||
})
|
||||
|
||||
return `
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
|
||||
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
|
||||
xmlns:xhtml="http://www.w3.org/1999/xhtml"
|
||||
xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0"
|
||||
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
|
||||
${urlsXml}
|
||||
</urlset>
|
||||
`
|
||||
}
|
||||
Reference in New Issue
Block a user