mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-13 23:16:47 +00:00
完善API获取
This commit is contained in:
@@ -3,26 +3,21 @@ export default function getAllPageIds (collectionQuery, collectionId, collection
|
||||
if (!collectionQuery && !collectionView) {
|
||||
return []
|
||||
}
|
||||
let views = collectionQuery[collectionId]
|
||||
if (!views) {
|
||||
views = collectionView
|
||||
}
|
||||
let pageIds = []
|
||||
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('从collectionQuery获取博客', collectionQuery)
|
||||
console.log('PageIds: 从collectionQuery获取', collectionQuery)
|
||||
} else if (viewIds && viewIds.length > 0) {
|
||||
const ids = collectionView[viewIds[0]].value.page_sort
|
||||
console.log('从viewId获取博客 ', viewIds)
|
||||
console.log('PageIds: 从viewId获取', viewIds)
|
||||
for (const id of ids) {
|
||||
pageIds.push(id)
|
||||
}
|
||||
}
|
||||
console.log('文章列表', pageIds)
|
||||
return pageIds
|
||||
}
|
||||
|
||||
@@ -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 })
|
||||
|
||||
@@ -7,17 +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 = performance.now()
|
||||
pageBlock = await getPageWithRetry(id, from)
|
||||
if (pageBlock) {
|
||||
console.info('[请求成功]:', `from:${from}`, `id:${id}`)
|
||||
} else {
|
||||
console.error('[请求失败]:', `from:${from}`, `id:${id}`)
|
||||
}
|
||||
const end = performance.now()
|
||||
console.log('[API耗时]', `${end - start}ms`)
|
||||
|
||||
if (pageBlock) {
|
||||
await setDataToCache(cacheKey, pageBlock)
|
||||
@@ -33,22 +30,25 @@ export async function getPostBlocks(id, from, slice) {
|
||||
*/
|
||||
async function getPageWithRetry(id, from, retryAttempts = 3) {
|
||||
if (retryAttempts && retryAttempts > 0) {
|
||||
console.log('[发起请求]', `from:${from}`, `id:${id}`, retryAttempts < 3 ? `剩余重试次数:${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}`, pageData)
|
||||
return pageData
|
||||
} catch (e) {
|
||||
await delay(1000)
|
||||
const cacheKey = 'page_block_' + id
|
||||
const pageBlock = await getDataFromCache(cacheKey)
|
||||
if (pageBlock) {
|
||||
console.log('[重试获取缓存]', `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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -55,7 +55,7 @@ export async function getStaticProps() {
|
||||
meta,
|
||||
...props
|
||||
},
|
||||
revalidate: 1
|
||||
revalidate: 5
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user