fix: 修复部分文章无法加载的问题,原因是Notion API加载时block上限1000

修复思路是在加载所有文章时,会首先一次性加载数据库中所有block,此时因为上限1000会发生溢出。之后会读取所有page的IDs,然后逐个获取page对应的block。此时如果发现ID查找不到(说明发生了溢出),则再调用API去逐一获取这些溢出了的page。可能这不是最优解。
修改的代码包括:
1. getDataBaseInfoByNotionAPI中添加上述判断逻辑。
2. 修改getPageProperties函数签名,直接传入block对应的value,而不是block数组。
This commit is contained in:
Olimi
2023-12-21 21:11:37 +08:00
committed by tangly1024.com
parent 50eb1a28fa
commit f73dddd72a
6 changed files with 64 additions and 18 deletions

View File

@@ -1,6 +1,6 @@
import BLOG from '@/blog.config'
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
import { getPostBlocks } from '@/lib/notion/getPostBlocks'
import { getPostBlocks, getSingleBlock } from '@/lib/notion/getPostBlocks'
import { idToUuid } from 'notion-utils'
import { deepClone } from '../utils'
import { getAllCategories } from './getAllCategories'
@@ -185,7 +185,17 @@ export function getNavPages({ allPages }) {
return post && post?.slug && (!post?.slug?.startsWith('http')) && post?.type === 'Post' && post?.status === 'Published'
})
return allNavPages.map(item => ({ id: item.id, title: item.title || '', pageCoverThumbnail: item.pageCoverThumbnail || '', category: item.category || null, tags: item.tags || null, summary: item.summary || null, slug: item.slug, pageIcon: item.pageIcon || '', lastEditedDate: item.lastEditedDate }))
return allNavPages.map(item => ({
id: item.id,
title: item.title || '',
pageCoverThumbnail: item.pageCoverThumbnail || '',
category: item.category || null,
tags: item.tags || null,
summary: item.summary || null,
slug: item.slug,
pageIcon: item.pageIcon || '',
lastEditedDate: item.lastEditedDate
}))
}
/**
@@ -205,7 +215,15 @@ const EmptyData = (pageId) => {
const empty = {
notice: null,
siteInfo: getSiteInfo({}),
allPages: [{ id: 1, title: `无法获取Notion数据请检查Notion_ID \n 当前 ${pageId}`, summary: '访问文档获取帮助→ https://tangly1024.com/article/vercel-deploy-notion-next', status: 'Published', type: 'Post', slug: '13a171332816461db29d50e9f575b00d', date: { start_date: '2023-04-24', lastEditedDay: '2023-04-24', tagItems: [] } }],
allPages: [{
id: 1,
title: `无法获取Notion数据请检查Notion_ID \n 当前 ${pageId}`,
summary: '访问文档获取帮助→ https://tangly1024.com/article/vercel-deploy-notion-next',
status: 'Published',
type: 'Post',
slug: '13a171332816461db29d50e9f575b00d',
date: { start_date: '2023-04-24', lastEditedDay: '2023-04-24', tagItems: [] }
}],
allNavPages: [],
collection: [],
collectionQuery: {},
@@ -263,9 +281,17 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
const id = pageIds[i]
const value = block[id]?.value
if (!value) {
// 如果找不到文章对应的block说明发生了溢出使用pageID再去请求
const pageBlock = await getSingleBlock(id, from)
if (pageBlock.block[id].value) {
const properties = (await getPageProperties(id, pageBlock.block[id].value, schema, null, getTagOptions(schema))) || null
if (properties) {
collectionData.push(properties)
}
}
continue
}
const properties = (await getPageProperties(id, block, schema, null, getTagOptions(schema))) || null
const properties = (await getPageProperties(id, block[id].value, schema, null, getTagOptions(schema))) || null
if (properties) {
collectionData.push(properties)
}
@@ -280,8 +306,8 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
postCount++
}
return post && post?.slug &&
(!post?.slug?.startsWith('http')) &&
(post?.status === 'Invisible' || post?.status === 'Published')
(!post?.slug?.startsWith('http')) &&
(post?.status === 'Invisible' || post?.status === 'Published')
})
// 站点配置优先读取配置表格否则读取blog.config.js 文件
@@ -294,7 +320,9 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
})
}
const notice = await getNotice(collectionData.filter(post => { return post && post?.type && post?.type === 'Notice' && post.status === 'Published' })?.[0])
const notice = await getNotice(collectionData.filter(post => {
return post && post?.type && post?.type === 'Notice' && post.status === 'Published'
})?.[0])
const categoryOptions = getAllCategories({ allPages, categoryOptions: getCategoryOptions(schema) })
const tagOptions = getAllTags({ allPages, tagOptions: getTagOptions(schema) })
// 旧的菜单