减小NEXT_DATA体积

This commit is contained in:
tangly1024.com
2024-05-16 11:04:35 +08:00
parent 84f36a712f
commit 71701fa314
7 changed files with 120 additions and 67 deletions

View File

@@ -5,9 +5,9 @@ import getAllPageIds from '@/lib/notion/getAllPageIds'
import { getAllTags } from '@/lib/notion/getAllTags'
import { getConfigMapFromConfigPage } from '@/lib/notion/getNotionConfig'
import getPageProperties, {
adjustPageProperties
adjustPageProperties
} from '@/lib/notion/getPageProperties'
import { fetchInBatches, getPostBlocks } from '@/lib/notion/getPostBlocks'
import { fetchInBatches, getPage } from '@/lib/notion/getPostBlocks'
import { compressImage, mapImgUrl } from '@/lib/notion/mapImage'
import { deepClone } from '@/lib/utils'
import { idToUuid } from 'notion-utils'
@@ -16,7 +16,7 @@ import { extractLangId, extractLangPrefix } from '../utils/pageId'
export { getAllTags } from '../notion/getAllTags'
export { getPost } from '../notion/getNotionPost'
export { getPostBlocks } from '../notion/getPostBlocks'
export { getPage as getPostBlocks } from '../notion/getPostBlocks'
/**
* 获取博客数据; 基于Notion实现
@@ -77,7 +77,16 @@ export async function getNotionPageData({ pageId, from }) {
}
// 返回给前端的数据做处理
const db = deepClone(data)
return compressData(deepClone(data))
}
/**
* 减少返回给前端的数据
* 并脱敏
* @param {*} db
*/
function compressData(db) {
// 清理多余数据
delete db.block
delete db.schema
delete db.rawMetadata
@@ -95,38 +104,96 @@ export async function getNotionPageData({ pageId, from }) {
if (db?.post) {
db.post = cleanBlock(db?.post)
}
db.tagOptions = cleanIds(db?.tagOptions)
db.categoryOptions = cleanIds(db?.categoryOptions)
db.customMenu = cleanIds(db?.customMenu)
db.latestPosts = cleanIds(db?.latestPosts)
db.allNavPages = shortenIds(db?.allNavPages)
// db.allPages = cleanBlocks(db?.allPages)
return db
}
/**
* 清理一组数据的id
* @param {*} items
* @returns
*/
function shortenIds(items) {
if (items && Array.isArray(items)) {
return deepClone(
items.map(item => {
item.short_id = getFirstPart(item.id)
delete item.id
return item
})
)
}
return items
}
function getFirstPart(uuid) {
if (!uuid || uuid.indexOf('-') < 0) {
return uuid
}
// 找到第一个 '-' 的位置
const index = uuid.indexOf('-')
// 截取从开始到第一个 '-' 之前的部分
return uuid.substring(0, index)
}
/**
* 清理一组数据的id
* @param {*} items
* @returns
*/
function cleanIds(items) {
if (items && Array.isArray(items)) {
return deepClone(
items.map(item => {
delete item.id
return item
})
)
}
return items
}
/**
* 清理block数据
*/
function cleanBlock(post) {
function cleanBlock(item) {
const post = deepClone(item)
const pageBlock = post?.blockMap?.block
for (const i in pageBlock) {
pageBlock[i] = cleanBlock(pageBlock[i])
delete pageBlock[i]?.role
delete pageBlock[i]?.value?.version
delete pageBlock[i]?.value?.created_by_table
delete pageBlock[i]?.value?.created_by_id
delete pageBlock[i]?.value?.last_edited_by_table
delete pageBlock[i]?.value?.last_edited_by_id
delete pageBlock[i]?.value?.space_id
delete pageBlock[i]?.value?.version
delete pageBlock[i]?.value?.format?.copied_from_pointer
delete pageBlock[i]?.value?.format?.block_locked_by
delete pageBlock[i]?.value?.parent_table
delete pageBlock[i]?.value?.copied_from_pointer
delete pageBlock[i]?.value?.copied_from
delete pageBlock[i]?.value?.created_by_table
delete pageBlock[i]?.value?.created_by_id
delete pageBlock[i]?.value?.last_edited_by_table
delete pageBlock[i]?.value?.last_edited_by_id
delete pageBlock[i]?.value?.permissions
delete pageBlock[i]?.value?.alive
}
delete post?.id
// delete post?.blockMap?.collection
if (pageBlock) {
for (const i in pageBlock) {
pageBlock[i] = cleanBlock(pageBlock[i])
delete pageBlock[i]?.role
delete pageBlock[i]?.value?.version
delete pageBlock[i]?.value?.created_by_table
delete pageBlock[i]?.value?.created_by_id
delete pageBlock[i]?.value?.last_edited_by_table
delete pageBlock[i]?.value?.last_edited_by_id
delete pageBlock[i]?.value?.space_id
delete pageBlock[i]?.value?.version
delete pageBlock[i]?.value?.format?.copied_from_pointer
delete pageBlock[i]?.value?.format?.block_locked_by
delete pageBlock[i]?.value?.parent_table
delete pageBlock[i]?.value?.copied_from_pointer
delete pageBlock[i]?.value?.copied_from
delete pageBlock[i]?.value?.created_by_table
delete pageBlock[i]?.value?.created_by_id
delete pageBlock[i]?.value?.last_edited_by_table
delete pageBlock[i]?.value?.last_edited_by_id
delete pageBlock[i]?.value?.permissions
delete pageBlock[i]?.value?.alive
}
}
return post
}
@@ -320,7 +387,7 @@ async function getNotice(post) {
return null
}
post.blockMap = await getPostBlocks(post.id, 'data-notice')
post.blockMap = await getPage(post.id, 'data-notice')
return post
}
@@ -372,7 +439,7 @@ const EmptyData = pageId => {
*/
async function getDataBaseInfoByNotionAPI({ pageId, from }) {
console.log('[Fetching Data]', pageId, from)
const pageRecordMap = await getPostBlocks(pageId, from)
const pageRecordMap = await getPage(pageId, from)
if (!pageRecordMap) {
console.error('can`t get Notion Data ; Which id is: ', pageId)
return {}