Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Block上限】修复部分文章无法加载的问题 #1723

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .env.local
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion lib/notion/getAllPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
42 changes: 35 additions & 7 deletions lib/notion/getNotionData.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
}))
}

/**
Expand All @@ -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: {},
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 文件
Expand All @@ -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) })
// 旧的菜单
Expand Down
13 changes: 6 additions & 7 deletions lib/notion/getPageProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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' }
Expand Down
19 changes: 19 additions & 0 deletions lib/notion/getPostBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pages/_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class MyDocument extends Document {
</>}

{BLOG.FONT_URL?.map((fontUrl, index) => {
console.log(fontUrl)
if (fontUrl.endsWith('.css')) {
return <link key={index} rel="stylesheet" href={fontUrl} />
} else {
Expand Down