From 8640eee3929981c38dec62d22e066312da111d7a Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Thu, 29 Dec 2022 21:38:58 +0800 Subject: [PATCH] =?UTF-8?q?sitemap.xml=E6=89=8B=E5=8A=A8=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- pages/sitemap.xml.js | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 pages/sitemap.xml.js diff --git a/package.json b/package.json index 8d68b740..3024d51a 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "scripts": { "dev": "next dev", - "build": "next build && next-sitemap --config next-sitemap.config.js", + "build": "next build", "start": "next start", "post-build": "next-sitemap --config next-sitemap.config.js", "bundle-report": "ANALYZE=true yarn build" diff --git a/pages/sitemap.xml.js b/pages/sitemap.xml.js new file mode 100644 index 00000000..403d6cd7 --- /dev/null +++ b/pages/sitemap.xml.js @@ -0,0 +1,55 @@ +// 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}`, // Absolute url + lastmod: new Date(), + changefreq: 'daily', + priority: '0.7' + }, { + loc: `${BLOG.LINK}/archive`, // Absolute url + lastmod: new Date(), + changefreq: 'daily', + priority: '0.7' + }, { + loc: `${BLOG.LINK}/category`, // Absolute url + lastmod: new Date(), + changefreq: 'daily', + priority: '0.7' + }, { + loc: `${BLOG.LINK}/feed`, // Absolute url + lastmod: new Date(), + changefreq: 'daily', + priority: '0.7' + }, { + loc: `${BLOG.LINK}/search`, // Absolute url + lastmod: new Date(), + changefreq: 'daily', + priority: '0.7' + }, { + loc: `${BLOG.LINK}/tag`, // Absolute url + lastmod: new Date(), + changefreq: 'daily', + priority: '0.7' + } + ] + const postFields = allPages?.map(post => { + console.log(post) + return { + loc: `${BLOG.LINK}/${post.slug}`, // Absolute url + lastmod: new Date(post?.date?.start_date || post?.createdTime), + changefreq: 'daily', + priority: '0.7' + } + }) + const fields = defaultFields.concat(postFields) + + return getServerSideSitemap(ctx, fields) +} + +export default () => { }