diff --git a/.env.local b/.env.local index cfc0b2cdb8e..9e10408697d 100644 --- a/.env.local +++ b/.env.local @@ -4,12 +4,12 @@ NEXT_PUBLIC_VERSION=4.2.0 # 可在此添加环境变量,去掉最左边的(# )注释即可 # 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= diff --git a/lib/notion/getAllPosts.js b/lib/notion/getAllPosts.js index ad4acde6c7d..5d63107ad5d 100644 --- a/lib/notion/getAllPosts.js +++ b/lib/notion/getAllPosts.js @@ -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) } diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index 5b77e716581..7211c31e2ac 100755 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -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) }) // 旧的菜单 diff --git a/lib/notion/getPageProperties.js b/lib/notion/getPageProperties.js index aca92b25d3b..92a94342e15 100644 --- a/lib/notion/getPageProperties.js +++ b/lib/notion/getPageProperties.js @@ -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] @@ -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' } diff --git a/lib/notion/getPostBlocks.js b/lib/notion/getPostBlocks.js index 167f0b0e700..7b8bf01ec6b 100644 --- a/lib/notion/getPostBlocks.js +++ b/lib/notion/getPostBlocks.js @@ -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 diff --git a/package-lock.json b/package-lock.json index 1656ec4b3cf..3fe6eeea105 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "notion-next", - "version": "4.0.15", + "version": "4.1.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/pages/_document.js b/pages/_document.js index 96c999c344e..976a45fb0c3 100644 --- a/pages/_document.js +++ b/pages/_document.js @@ -20,7 +20,6 @@ class MyDocument extends Document { } {BLOG.FONT_URL?.map((fontUrl, index) => { - console.log(fontUrl) if (fontUrl.endsWith('.css')) { return } else {