Skip to content

Commit

Permalink
完善API获取
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed May 9, 2022
1 parent adac140 commit 37465ca
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 25 deletions.
11 changes: 3 additions & 8 deletions lib/notion/getAllPageIds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion lib/notion/getNotionData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
20 changes: 10 additions & 10 deletions lib/notion/getPostBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}
Expand Down
7 changes: 2 additions & 5 deletions pages/404.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function getStaticProps() {
meta,
...props
},
revalidate: 1
revalidate: 5
}
}

Expand Down

0 comments on commit 37465ca

Please sign in to comment.