减小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

@@ -1,12 +1,12 @@
import { uuidToId } from 'notion-utils'
import { idToUuid } from 'notion-utils'
import { checkStrIsNotionId, getLastPartOfUrl, isBrowser } from '../utils'
/**
* 处理页面内连接跳转:
* 1. 若是本站域名则在当前窗口打开不开新窗口
* 2. 若是Notion笔记中的内链尝试转换成博客中现有的文章地址
* 1.若是本站域名则在当前窗口打开不开新窗口
* 2.url是notion-id转成站内文章链接
*/
export const mapPageUrl = allPages => {
export const convertInnerUrl = allPages => {
if (isBrowser) {
const currentURL = window.location.origin + window.location.pathname
const allAnchorTags = document.getElementsByTagName('a') // 或者使用 document.querySelectorAll('a') 获取 NodeList
@@ -16,7 +16,9 @@ export const mapPageUrl = allPages => {
// 如果url是一个Notion_id尝试匹配成博客的文章内链
const slug = getLastPartOfUrl(anchorTag.href)
if (checkStrIsNotionId(slug)) {
const slugPage = allPages?.find(page => uuidToId(page.id) === slug)
const slugPage = allPages?.find(page => {
return idToUuid(slug).indexOf(page.short_id) === 0
})
if (slugPage) {
anchorTag.href = slugPage?.href
}

View File

@@ -9,7 +9,7 @@
import { getDateValue, getTextContent } from 'notion-utils'
import { deepClone } from '../utils'
import getAllPageIds from './getAllPageIds'
import { getPostBlocks } from './getPostBlocks'
import { getPage } from './getPostBlocks'
/**
* 从Notion中读取Config配置表
@@ -41,12 +41,12 @@ export async function getConfigMapFromConfigPage(allPages) {
}
const configPageId = configPage.id
// console.log('[Notion配置]请求配置数据 ', configPage.id)
let pageRecordMap = await getPostBlocks(configPageId, 'config-table')
let pageRecordMap = await getPage(configPageId, 'config-table')
// console.log('配置中心Page', configPageId, pageRecordMap)
let content = pageRecordMap.block[configPageId].value.content
for (const table of ['Config-Table', 'CONFIG-TABLE']) {
if (content) break
pageRecordMap = await getPostBlocks(configPageId, table)
pageRecordMap = await getPage(configPageId, table)
content = pageRecordMap.block[configPageId].value.content
}

View File

@@ -2,7 +2,7 @@ import BLOG from '@/blog.config'
import { idToUuid } from 'notion-utils'
import { defaultMapImageUrl } from 'react-notion-x'
import formatDate from '../utils/formatDate'
import { getPostBlocks } from './getPostBlocks'
import { getPage } from './getPostBlocks'
/**
* 根据页面ID获取内容
@@ -10,7 +10,7 @@ import { getPostBlocks } from './getPostBlocks'
* @returns
*/
export async function getPost(pageId) {
const blockMap = await getPostBlocks(pageId, 'slug')
const blockMap = await getPage(pageId, 'slug')
if (!blockMap) {
return null
}

View File

@@ -10,7 +10,7 @@ import { deepClone, delay } from '../utils'
* @param {*} slice
* @returns
*/
export async function getPostBlocks(id, from, slice) {
export async function getPage(id, from, slice) {
const cacheKey = 'page_block_' + id
let pageBlock = await getDataFromCache(cacheKey)
if (pageBlock) {
@@ -27,28 +27,6 @@ export async function getPostBlocks(id, from, slice) {
return pageBlock
}
/**
* 针对在getDataBaseInfoByNotionAPI=>getPostBlocks中获取不到的溢出的block-id用此方法另外抓取
* @param {*} id
* @param {*} from
* @returns
*/
export async function getSingleBlock(id, from) {
const cacheKey = 'single_block_' + id
let pageBlock = await getDataFromCache(cacheKey)
if (pageBlock) {
// console.log('[API<<--缓存]', `from:${from}`, cacheKey)
return pageBlock
}
pageBlock = await getPageWithRetry(id, 'single_' + from)
if (pageBlock) {
await setDataToCache(cacheKey, pageBlock)
}
return pageBlock
}
/**
* 调用接口,失败会重试
* @param {*} id
@@ -162,6 +140,11 @@ function filterPostBlocks(id, blockMap, slice) {
* @returns
*/
export const fetchInBatches = async (ids, batchSize = 100) => {
// 如果 ids 不是数组,则将其转换为数组
if (!Array.isArray(ids)) {
ids = [ids]
}
const authToken = BLOG.NOTION_ACCESS_TOKEN || null
const api = new NotionAPI({
authToken,
@@ -171,7 +154,7 @@ export const fetchInBatches = async (ids, batchSize = 100) => {
let fetchedBlocks = {}
for (let i = 0; i < ids.length; i += batchSize) {
const batch = ids.slice(i, i + batchSize)
console.log('[API-->>请求] Fetching missing blocks', ids.length)
console.log('[API-->>请求] Fetching missing blocks', batch, ids.length)
const start = new Date().getTime()
const pageChunk = await api.getBlocks(batch)
const end = new Date().getTime()