diff --git a/lib/notion/getAllPageIds.js b/lib/notion/getAllPageIds.js index 5eb15570..1fbca908 100644 --- a/lib/notion/getAllPageIds.js +++ b/lib/notion/getAllPageIds.js @@ -1,24 +1,23 @@ -import { idToUuid } from 'notion-utils' -export default function getAllPageIds (collectionQuery, viewId) { - if (!collectionQuery) { - return [] - } - const views = Object.values(collectionQuery)[0] - if (!views) { +export default function getAllPageIds (collectionQuery, collectionId, collectionView, viewIds) { + if (!collectionQuery && !collectionView) { return [] } let pageIds = [] - if (viewId) { - const vId = idToUuid(viewId) - pageIds = views[vId]?.blockIds - } else { + if (collectionQuery && Object.values(collectionQuery).length > 0) { const pageSet = new Set() - Object.values(views).forEach(view => { + Object.values(collectionQuery[collectionId]).forEach(view => { view?.blockIds?.forEach(id => pageSet.add(id)) // group视图 view?.collection_group_results?.blockIds?.forEach(id => pageSet.add(id)) // table视图 }) pageIds = [...pageSet] + console.log('PageIds: 从collectionQuery获取', collectionQuery) + } else if (viewIds && viewIds.length > 0) { + const ids = collectionView[viewIds[0]].value.page_sort + console.log('PageIds: 从viewId获取', viewIds) + for (const id of ids) { + pageIds.push(id) + } } return pageIds } diff --git a/lib/notion/getAllPosts.js b/lib/notion/getAllPosts.js index 47198789..51d46193 100644 --- a/lib/notion/getAllPosts.js +++ b/lib/notion/getAllPosts.js @@ -21,22 +21,22 @@ export async function getAllPosts({ notionPageData, from, pageType }) { return [] } - const pageBlock = notionPageData.block - const schema = notionPageData.schema - const tagOptions = notionPageData.tags - const collectionQuery = notionPageData.collectionQuery - + const { block, schema, tagOptions, collectionQuery, collectionId, collectionView, viewIds } = notionPageData const data = [] - const pageIds = getAllPageIds(collectionQuery) + const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds) for (let i = 0; i < pageIds.length; i++) { const id = pageIds[i] - const properties = (await getPageProperties(id, pageBlock, schema)) || null + const properties = (await getPageProperties(id, block, schema)) || null + const value = block[id]?.value + if (!value) { + continue + } properties.slug = properties.slug ?? properties.id - properties.createdTime = formatDate(new Date(pageBlock[id].value?.created_time).toString(), BLOG.LANG) - properties.lastEditedTime = formatDate(new Date(pageBlock[id].value?.last_edited_time).toString(), BLOG.LANG) - properties.fullWidth = pageBlock[id].value?.format?.page_full_width ?? false - properties.page_cover = getPostCover(id, pageBlock) ?? null - properties.content = pageBlock[id].value?.content ?? [] + properties.createdTime = formatDate(new Date(value.created_time).toString(), BLOG.LANG) + properties.lastEditedTime = formatDate(new Date(value?.last_edited_time).toString(), BLOG.LANG) + properties.fullWidth = value.format?.page_full_width ?? false + properties.page_cover = getPostCover(id, block) ?? null + 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/getNotionData.js b/lib/notion/getNotionData.js index 0b2d5ba1..d0021994 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -70,7 +70,7 @@ export async function getNotionPageData({ pageId, from }) { const cacheKey = 'page_block_' + pageId const data = await getDataFromCache(cacheKey) if (data && data.pageIds?.length > 0) { - console.log('[请求缓存]:', `from:${from}`, `root-page-id:${pageId}`) + console.log('[命中缓存]:', `from:${from}`, `root-page-id:${pageId}`) return data } const pageRecordMap = await getPageRecordMapByNotionAPI({ pageId, from }) @@ -211,13 +211,18 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) { } const collection = Object.values(pageRecordMap.collection)[0]?.value + const collectionId = rawMetadata?.collection_id const collectionQuery = pageRecordMap.collection_query + const collectionView = pageRecordMap.collection_view const schema = collection?.schema const tagOptions = getTagOptions(schema) const categoryOptions = getCategoryOptions(schema) - + const viewIds = rawMetadata?.view_ids const data = [] - const pageIds = getAllPageIds(collectionQuery) + const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds) + if (pageIds?.length === 0) { + console.error('获取到的文章列表为空,请检查notion模板', collectionQuery, collection, pageRecordMap) + } for (let i = 0; i < pageIds.length; i++) { const id = pageIds[i] const properties = (await getPageProperties(id, block, schema)) || null @@ -243,6 +248,9 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) { return { collection, collectionQuery, + collectionId, + collectionView, + viewIds, block, schema, tagOptions, diff --git a/lib/notion/getPostBlocks.js b/lib/notion/getPostBlocks.js index 1f58d94f..ebc770d9 100644 --- a/lib/notion/getPostBlocks.js +++ b/lib/notion/getPostBlocks.js @@ -7,13 +7,14 @@ export async function getPostBlocks(id, from, slice) { const cacheKey = 'page_block_' + id let pageBlock = await getDataFromCache(cacheKey) if (pageBlock) { - console.log('[请求缓存]:', `from:${from}`, cacheKey) + console.log('[命中缓存]:', `from:${from}`, cacheKey) return filterPostBlocks(id, pageBlock, slice) } - console.warn('[请求API]:', `from:${from}`, `id:${id}`) + const start = new Date().getTime() pageBlock = await getPageWithRetry(id, from) - console.warn('[请求完成]: 结果', `${pageBlock ? '成功' : '失败'}`, `from:${from}`, `id:${id}`) + const end = new Date().getTime() + console.log('[API耗时]', `${end - start}ms`) if (pageBlock) { await setDataToCache(cacheKey, pageBlock) @@ -29,22 +30,25 @@ export async function getPostBlocks(id, from, slice) { */ async function getPageWithRetry(id, from, retryAttempts = 3) { if (retryAttempts && retryAttempts > 0) { - console.error('[发起请求]', `from:${from}`, `id:${id}`, `剩余重试次数:${retryAttempts}`) + console.log('[请求API]', `from:${from}`, `id:${id}`, retryAttempts < 3 ? `剩余重试次数:${retryAttempts}` : '') try { const authToken = BLOG.NOTION_ACCESS_TOKEN || null const api = new NotionAPI({ authToken }) - return await api.getPage(id) + const pageData = await api.getPage(id) + console.info('[响应成功]:', `from:${from}`, `id:${id}`) + return pageData } catch (e) { await delay(1000) const cacheKey = 'page_block_' + id const pageBlock = await getDataFromCache(cacheKey) if (pageBlock) { - console.error('[重试获取缓存]', `from:${from}`, `id:${id}`) + console.log('[重试缓存]', `from:${from}`, `id:${id}`) return pageBlock } return await getPageWithRetry(id, from, retryAttempts - 1) } } else { + console.error('[请求失败]:', `from:${from}`, `id:${id}`) return null } } diff --git a/pages/404.js b/pages/404.js index 16f4206f..b5a3ca8d 100644 --- a/pages/404.js +++ b/pages/404.js @@ -15,11 +15,8 @@ const NoFound = props => { } export async function getStaticProps () { - const props = await getGlobalNotionData({ from: 'category-index-props', categoryCount: 0 }) - return { - props, - revalidate: 1 - } + const props = await getGlobalNotionData({ from: '404' }) || {} + return { props } } export default NoFound diff --git a/pages/index.js b/pages/index.js index fe8de2cb..1eb1f070 100644 --- a/pages/index.js +++ b/pages/index.js @@ -55,7 +55,7 @@ export async function getStaticProps() { meta, ...props }, - revalidate: 1 + revalidate: 5 } }