预渲染category、tag

This commit is contained in:
tangly
2022-11-12 23:07:05 +08:00
parent 4b653688a8
commit c6b0926ff7
3 changed files with 38 additions and 26 deletions

View File

@@ -33,10 +33,8 @@ export async function getStaticProps({ params: { category, page } }) {
const from = 'category-page-props'
let props = await getGlobalNotionData({ from })
// 过滤状态
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published')
// 过滤类型
props.posts = props.posts.filter(post => post && post.category && post.category.includes(category))
// 过滤状态类型
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.category.includes(category))
// 处理文章页数
props.postCount = props.posts.length
// 处理分页
@@ -55,11 +53,24 @@ export async function getStaticProps({ params: { category, page } }) {
export async function getStaticPaths() {
const from = 'category-paths'
const { categories } = await getGlobalNotionData({ from })
const { categories, allPages } = await getGlobalNotionData({ from })
const paths = []
categories?.forEach(category => {
// 过滤状态类型
const categoryPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.category.includes(category.name))
// 处理文章页数
const postCount = categoryPosts.length
const totalPages = Math.ceil(postCount / BLOG.POSTS_PER_PAGE)
if (totalPages > 1) {
for (let i = 1; i <= totalPages; i++) {
paths.push({ params: { category: category.name, page: '' + i } })
}
}
})
return {
paths: Object.keys(categories).map(category => ({
params: { category: categories[category]?.name, page: '1' }
})),
paths,
fallback: true
}
}

View File

@@ -28,10 +28,7 @@ export async function getStaticProps({ params: { tag } }) {
const props = await getGlobalNotionData({ from })
// 过滤状态
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published')
// 处理标签
props.posts = props.posts.filter(post => post && post.tags && post.tags.includes(tag))
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.tags && post.tags.includes(tag))
// 处理文章页数
props.postCount = props.posts.length

View File

@@ -26,18 +26,16 @@ const Tag = props => {
export async function getStaticProps({ params: { tag, page } }) {
const from = 'tag-page-props'
const props = await getGlobalNotionData({ from })
// 过滤状态
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published')
// 过滤标签
props.posts = props.posts.filter(post => post && post.tags && post.tags.includes(tag))
// 处理文章页数
// 过滤状态、标签
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.tags && post.tags.includes(tag))
// 处理文章数
props.postCount = props.posts.length
// 处理分页
props.posts = props.posts.slice(BLOG.POSTS_PER_PAGE * (page - 1), BLOG.POSTS_PER_PAGE * page - 1)
props.tag = tag
props.page = page
delete props.allPages
return {
props,
revalidate: 1
@@ -46,16 +44,22 @@ export async function getStaticProps({ params: { tag, page } }) {
export async function getStaticPaths() {
const from = 'tag-page-static-path'
const { tags } = await getGlobalNotionData({ from })
const tagNames = []
tags.forEach(tag => {
tagNames.push(tag.name)
const { tags, allPages } = await getGlobalNotionData({ from })
const paths = []
tags?.forEach(tag => {
// 过滤状态类型
const categoryPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.tags.includes(tag.name))
// 处理文章页数
const postCount = categoryPosts.length
const totalPages = Math.ceil(postCount / BLOG.POSTS_PER_PAGE)
if (totalPages > 1) {
for (let i = 1; i <= totalPages; i++) {
paths.push({ params: { tag: tag.name, page: '' + i } })
}
}
})
return {
paths: Object.keys(tagNames).map(index => ({
params: { tag: tagNames[index], page: '1' }
})),
paths: paths,
fallback: true
}
}