Files
NotionNext/pages/tag/[tag].js
tangly1024 dfc0f645d4 Code🤣
2021-09-27 09:33:21 +08:00

44 lines
1.0 KiB
JavaScript

import { getAllPosts, getAllTags } from '@/lib/notion'
import DefaultLayout from '@/layouts/DefaultLayout'
import BLOG from '@/blog.config'
export default function Tag ({ tags, posts, currentTag }) {
return <DefaultLayout tags={tags} posts={posts} currentTag={currentTag} />
}
export async function getStaticProps ({ params }) {
const currentTag = params.tag
let posts = await getAllPosts()
posts = posts.filter(
post => post.status[0] === 'Published' && post.type[0] === 'Post'
)
const tags = await getAllTags(posts)
const filteredPosts = posts.filter(
post => post && post.tags && post.tags.includes(currentTag)
)
return {
props: {
tags,
posts: filteredPosts,
currentTag
},
revalidate: 1
}
}
export async function getStaticPaths () {
if (BLOG.isProd) {
// 预渲染
const tags = await getAllTags()
return {
paths: Object.keys(tags).map(tag => ({ params: { tag } })),
fallback: true
}
} else {
return {
paths: [],
fallback: true
}
}
}