Skip to content

Commit

Permalink
fix: 修复部分文章无法加载的问题,原因是Notion API加载时block上限1000
Browse files Browse the repository at this point in the history
修复思路是在加载所有文章时,会首先一次性加载数据库中所有block,此时因为上限1000会发生溢出。之后会读取所有page的IDs,然后逐个获取page对应的block。此时如果发现ID查找不到(说明发生了溢出),则再调用API去逐一获取这些溢出了的page。可能这不是最优解。
修改的代码包括:
1. getDataBaseInfoByNotionAPI中添加上述判断逻辑。
2. 修改getPageProperties函数签名,直接传入block对应的value,而不是block数组。
  • Loading branch information
Olimiya authored and tangly1024 committed Dec 27, 2023
1 parent 50eb1a2 commit f73dddd
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .env.local
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ NEXT_PUBLIC_VERSION=4.1.2

# 可在此添加环境变量,去掉最左边的(# )注释即可
# Notion页面ID,必须
# NOTION_PAGE_ID=
# NOTION_PAGE_ID=097e5f674880459d8e1b4407758dc4fb

# 非必须
# NEXT_PUBLIC_PSEUDO_STATIC=
# NEXT_PUBLIC_REVALIDATE_SECOND=
# NEXT_PUBLIC_THEME=
# NEXT_PUBLIC_THEME=matery
# NEXT_PUBLIC_THEME_SWITCH=
# NEXT_PUBLIC_LANG=
# NEXT_PUBLIC_APPEARANCE=
Expand Down
2 changes: 1 addition & 1 deletion lib/notion/getAllPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function getAllPosts({ notionPageData, from, pageType }) {
if (!value) {
continue
}
const properties = (await getPageProperties(id, block, schema, null, tagOptions)) || null
const properties = (await getPageProperties(id, block[id].value, schema, null, tagOptions)) || null
data.push(properties)
}

Expand Down
42 changes: 35 additions & 7 deletions lib/notion/getNotionData.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
}))
}

/**
Expand All @@ -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: {},
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 文件
Expand All @@ -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) })
// 旧的菜单
Expand Down
13 changes: 6 additions & 7 deletions lib/notion/getPageProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ import { mapImgUrl } from './mapImage'
/**
* 获取页面元素成员属性
* @param {*} id
* @param {*} block
* @param {*} value
* @param {*} schema
* @param {*} authToken
* @param {*} tagOptions
* @returns
*/
export default async function getPageProperties(id, block, schema, authToken, tagOptions) {
const rawProperties = Object.entries(block?.[id]?.value?.properties || [])
export default async function getPageProperties(id, value, schema, authToken, tagOptions) {
const rawProperties = Object.entries(value?.properties || [])
const excludeProperties = ['date', 'select', 'multi_select', 'person']
const value = block[id]?.value
const properties = {}
for (let i = 0; i < rawProperties.length; i++) {
const [key, val] = rawProperties[i]
Expand Down Expand Up @@ -91,9 +90,9 @@ export default async function getPageProperties(id, block, schema, authToken, ta
properties.lastEditedDate = new Date(value?.last_edited_time)
properties.lastEditedDay = formatDate(new Date(value?.last_edited_time), BLOG.LANG)
properties.fullWidth = value.format?.page_full_width ?? false
properties.pageIcon = mapImgUrl(block[id].value?.format?.page_icon, block[id].value) ?? ''
properties.pageCover = mapImgUrl(block[id].value?.format?.page_cover, block[id].value) ?? ''
properties.pageCoverThumbnail = mapImgUrl(block[id].value?.format?.page_cover, block[id].value, 'block', 'pageCoverThumbnail') ?? ''
properties.pageIcon = mapImgUrl(value.value?.format?.page_icon, value.value) ?? ''
properties.pageCover = mapImgUrl(value.value?.format?.page_cover, value.value) ?? ''
properties.pageCoverThumbnail = mapImgUrl(value.value?.format?.page_cover, value.value, 'block', 'pageCoverThumbnail') ?? ''
properties.content = value.content ?? []
properties.tagItems = properties?.tags?.map(tag => {
return { name: tag, color: tagOptions?.find(t => t.value === tag)?.color || 'gray' }
Expand Down
19 changes: 19 additions & 0 deletions lib/notion/getPostBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ export async function getPostBlocks(id, from, slice) {
return pageBlock
}

export async function getSingleBlock(id, from) {
const cacheKey = 'single_block_' + id
let pageBlock = await getDataFromCache(cacheKey)
if (pageBlock) {
console.log('[缓存]:', `from:${from}`, cacheKey)
return pageBlock
}

const start = new Date().getTime()
pageBlock = await getPageWithRetry(id, from)
const end = new Date().getTime()
console.log('[API耗时]', `${end - start}ms`)

if (pageBlock) {
await setDataToCache(cacheKey, pageBlock)
}
return pageBlock
}

/**
* 调用接口,失败会重试
* @param {*} id
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f73dddd

Please sign in to comment.