Files
NotionNext/pages/index.js
tangly1024 e791752e68 async theme
2023-06-04 17:17:38 +08:00

78 lines
2.0 KiB
JavaScript

import BLOG from '@/blog.config'
import { getPostBlocks } from '@/lib/notion'
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { useGlobal } from '@/lib/global'
import { generateRss } from '@/lib/rss'
import { generateRobotsTxt } from '@/lib/robots.txt'
import dynamic from 'next/dynamic'
import Loading from '@/components/Loading'
/**
* 首页布局
* @param {*} props
* @returns
*/
const Index = props => {
const { theme } = useGlobal()
const LayoutIndex = dynamic(() => import(`@/themes/${theme}/LayoutIndex`)
.then(async (m) => { return m.LayoutIndex }), { ssr: false, loading: () => <Loading /> }
)
return <LayoutIndex {...props} />
}
/**
* SSG 获取数据
* @returns
*/
export async function getStaticProps() {
const from = 'index'
const props = await getGlobalNotionData({ from })
const { siteInfo } = props
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published')
const meta = {
title: `${siteInfo?.title} | ${siteInfo?.description}`,
description: siteInfo?.description,
image: siteInfo?.pageCover,
slug: '',
type: 'website'
}
// 处理分页
if (BLOG.POST_LIST_STYLE === 'scroll') {
// 滚动列表默认给前端返回所有数据
} else if (BLOG.POST_LIST_STYLE === 'page') {
props.posts = props.posts?.slice(0, BLOG.POSTS_PER_PAGE)
}
// 预览文章内容
if (BLOG.POST_LIST_PREVIEW === 'true') {
for (const i in props.posts) {
const post = props.posts[i]
if (post.password && post.password !== '') {
continue
}
post.blockMap = await getPostBlocks(post.id, 'slug', BLOG.POST_PREVIEW_LINES)
}
}
// 生成robotTxt
generateRobotsTxt()
// 生成Feed订阅
if (JSON.parse(BLOG.ENABLE_RSS)) {
generateRss(props?.latestPosts || [])
}
delete props.allPages
return {
props: {
meta,
...props
},
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND)
}
}
export default Index