mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-02 15:10:16 +00:00
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:
@@ -4,12 +4,12 @@ NEXT_PUBLIC_VERSION=4.1.2
|
|||||||
|
|
||||||
# 可在此添加环境变量,去掉最左边的(# )注释即可
|
# 可在此添加环境变量,去掉最左边的(# )注释即可
|
||||||
# Notion页面ID,必须
|
# Notion页面ID,必须
|
||||||
# NOTION_PAGE_ID=
|
# NOTION_PAGE_ID=097e5f674880459d8e1b4407758dc4fb
|
||||||
|
|
||||||
# 非必须
|
# 非必须
|
||||||
# NEXT_PUBLIC_PSEUDO_STATIC=
|
# NEXT_PUBLIC_PSEUDO_STATIC=
|
||||||
# NEXT_PUBLIC_REVALIDATE_SECOND=
|
# NEXT_PUBLIC_REVALIDATE_SECOND=
|
||||||
# NEXT_PUBLIC_THEME=
|
# NEXT_PUBLIC_THEME=matery
|
||||||
# NEXT_PUBLIC_THEME_SWITCH=
|
# NEXT_PUBLIC_THEME_SWITCH=
|
||||||
# NEXT_PUBLIC_LANG=
|
# NEXT_PUBLIC_LANG=
|
||||||
# NEXT_PUBLIC_APPEARANCE=
|
# NEXT_PUBLIC_APPEARANCE=
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ export async function getAllPosts({ notionPageData, from, pageType }) {
|
|||||||
if (!value) {
|
if (!value) {
|
||||||
continue
|
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)
|
data.push(properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import BLOG from '@/blog.config'
|
import BLOG from '@/blog.config'
|
||||||
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
|
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 { idToUuid } from 'notion-utils'
|
||||||
import { deepClone } from '../utils'
|
import { deepClone } from '../utils'
|
||||||
import { getAllCategories } from './getAllCategories'
|
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 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 = {
|
const empty = {
|
||||||
notice: null,
|
notice: null,
|
||||||
siteInfo: getSiteInfo({}),
|
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: [],
|
allNavPages: [],
|
||||||
collection: [],
|
collection: [],
|
||||||
collectionQuery: {},
|
collectionQuery: {},
|
||||||
@@ -263,9 +281,17 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
|
|||||||
const id = pageIds[i]
|
const id = pageIds[i]
|
||||||
const value = block[id]?.value
|
const value = block[id]?.value
|
||||||
if (!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
|
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) {
|
if (properties) {
|
||||||
collectionData.push(properties)
|
collectionData.push(properties)
|
||||||
}
|
}
|
||||||
@@ -280,8 +306,8 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
|
|||||||
postCount++
|
postCount++
|
||||||
}
|
}
|
||||||
return post && post?.slug &&
|
return post && post?.slug &&
|
||||||
(!post?.slug?.startsWith('http')) &&
|
(!post?.slug?.startsWith('http')) &&
|
||||||
(post?.status === 'Invisible' || post?.status === 'Published')
|
(post?.status === 'Invisible' || post?.status === 'Published')
|
||||||
})
|
})
|
||||||
|
|
||||||
// 站点配置优先读取配置表格,否则读取blog.config.js 文件
|
// 站点配置优先读取配置表格,否则读取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 categoryOptions = getAllCategories({ allPages, categoryOptions: getCategoryOptions(schema) })
|
||||||
const tagOptions = getAllTags({ allPages, tagOptions: getTagOptions(schema) })
|
const tagOptions = getAllTags({ allPages, tagOptions: getTagOptions(schema) })
|
||||||
// 旧的菜单
|
// 旧的菜单
|
||||||
|
|||||||
@@ -9,16 +9,15 @@ import { mapImgUrl } from './mapImage'
|
|||||||
/**
|
/**
|
||||||
* 获取页面元素成员属性
|
* 获取页面元素成员属性
|
||||||
* @param {*} id
|
* @param {*} id
|
||||||
* @param {*} block
|
* @param {*} value
|
||||||
* @param {*} schema
|
* @param {*} schema
|
||||||
* @param {*} authToken
|
* @param {*} authToken
|
||||||
* @param {*} tagOptions
|
* @param {*} tagOptions
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export default async function getPageProperties(id, block, schema, authToken, tagOptions) {
|
export default async function getPageProperties(id, value, schema, authToken, tagOptions) {
|
||||||
const rawProperties = Object.entries(block?.[id]?.value?.properties || [])
|
const rawProperties = Object.entries(value?.properties || [])
|
||||||
const excludeProperties = ['date', 'select', 'multi_select', 'person']
|
const excludeProperties = ['date', 'select', 'multi_select', 'person']
|
||||||
const value = block[id]?.value
|
|
||||||
const properties = {}
|
const properties = {}
|
||||||
for (let i = 0; i < rawProperties.length; i++) {
|
for (let i = 0; i < rawProperties.length; i++) {
|
||||||
const [key, val] = rawProperties[i]
|
const [key, val] = rawProperties[i]
|
||||||
@@ -91,9 +90,9 @@ export default async function getPageProperties(id, block, schema, authToken, ta
|
|||||||
properties.lastEditedDate = new Date(value?.last_edited_time)
|
properties.lastEditedDate = new Date(value?.last_edited_time)
|
||||||
properties.lastEditedDay = formatDate(new Date(value?.last_edited_time), BLOG.LANG)
|
properties.lastEditedDay = formatDate(new Date(value?.last_edited_time), BLOG.LANG)
|
||||||
properties.fullWidth = value.format?.page_full_width ?? false
|
properties.fullWidth = value.format?.page_full_width ?? false
|
||||||
properties.pageIcon = mapImgUrl(block[id].value?.format?.page_icon, block[id].value) ?? ''
|
properties.pageIcon = mapImgUrl(value.value?.format?.page_icon, value.value) ?? ''
|
||||||
properties.pageCover = mapImgUrl(block[id].value?.format?.page_cover, block[id].value) ?? ''
|
properties.pageCover = mapImgUrl(value.value?.format?.page_cover, value.value) ?? ''
|
||||||
properties.pageCoverThumbnail = mapImgUrl(block[id].value?.format?.page_cover, block[id].value, 'block', 'pageCoverThumbnail') ?? ''
|
properties.pageCoverThumbnail = mapImgUrl(value.value?.format?.page_cover, value.value, 'block', 'pageCoverThumbnail') ?? ''
|
||||||
properties.content = value.content ?? []
|
properties.content = value.content ?? []
|
||||||
properties.tagItems = properties?.tags?.map(tag => {
|
properties.tagItems = properties?.tags?.map(tag => {
|
||||||
return { name: tag, color: tagOptions?.find(t => t.value === tag)?.color || 'gray' }
|
return { name: tag, color: tagOptions?.find(t => t.value === tag)?.color || 'gray' }
|
||||||
|
|||||||
@@ -30,6 +30,25 @@ export async function getPostBlocks(id, from, slice) {
|
|||||||
return pageBlock
|
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
|
* @param {*} id
|
||||||
|
|||||||
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "notion-next",
|
"name": "notion-next",
|
||||||
"version": "4.0.15",
|
"version": "4.1.2",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
Reference in New Issue
Block a user