diff --git a/lib/robots.txt.js b/lib/robots.txt.js new file mode 100644 index 00000000..917796d7 --- /dev/null +++ b/lib/robots.txt.js @@ -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 + + `) +} diff --git a/lib/sitemap.xml.js b/lib/sitemap.xml.js new file mode 100644 index 00000000..0e057610 --- /dev/null +++ b/lib/sitemap.xml.js @@ -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 += ` + ${u.loc} + ${u.lastmod} + ${u.changefreq} + + ` + }) + + return ` + + ${urlsXml} + + ` +} diff --git a/pages/index.js b/pages/index.js index 087a37bc..663462fd 100644 --- a/pages/index.js +++ b/pages/index.js @@ -4,6 +4,8 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData' import * as ThemeMap from '@/themes' import { useGlobal } from '@/lib/global' import { generateRss } from '@/lib/rss' +import { generateRobotsTxt } from '@/lib/robots.txt' +import { generateSitemapXml } from '@/lib/sitemap.xml' const Index = props => { const { theme } = useGlobal() const ThemeComponents = ThemeMap[theme] @@ -17,7 +19,6 @@ export async function getStaticProps() { const { siteInfo } = props props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published') - delete props.allPages const meta = { title: `${siteInfo?.title} | ${siteInfo?.description}`, description: siteInfo?.description, @@ -47,6 +48,12 @@ export async function getStaticProps() { if (JSON.parse(BLOG.ENABLE_RSS)) { generateRss(props?.latestPosts || []) } + // 生成robotTxt + generateRobotsTxt() + // 生成sitemap.xml + generateSitemapXml({ allPages: props.allPages }) + + delete props.allPages return { props: { diff --git a/pages/sitemap.xml.js b/pages/sitemap.xml.js deleted file mode 100644 index 17980067..00000000 --- a/pages/sitemap.xml.js +++ /dev/null @@ -1,60 +0,0 @@ -// pages/sitemap.xml.js -import { getServerSideSitemap } from 'next-sitemap' -import { getGlobalNotionData } from '@/lib/notion/getNotionData' -import BLOG from '@/blog.config' - -export const getServerSideProps = async (ctx) => { - const { allPages } = await getGlobalNotionData({ from: 'rss' }) - const defaultFields = [ - { - loc: `${BLOG.LINK}`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily', - priority: '0.7' - }, { - loc: `${BLOG.LINK}/archive`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily', - priority: '0.7' - }, { - loc: `${BLOG.LINK}/category`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily', - priority: '0.7' - }, { - loc: `${BLOG.LINK}/feed`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily', - priority: '0.7' - }, { - loc: `${BLOG.LINK}/search`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily', - priority: '0.7' - }, { - loc: `${BLOG.LINK}/tag`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily', - priority: '0.7' - } - ] - const postFields = allPages?.map(post => { - return { - loc: `${BLOG.LINK}/${post.slug}`, - lastmod: new Date(post?.date?.start_date || post?.createdTime).toISOString().split('T')[0], - changefreq: 'daily', - priority: '0.7' - } - }) - const fields = defaultFields.concat(postFields) - - // 缓存 - // ctx.res.setHeader( - // 'Cache-Control', - // 'public, s-maxage=10, stale-while-revalidate=59' - // ) - - return getServerSideSitemap(ctx, fields) -} - -export default () => { }