mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-13 15:09:47 +00:00
feat: preview image improvements
This commit is contained in:
@@ -95,7 +95,8 @@ export const includeNotionIdInUrls: boolean = getSiteConfig(
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// Optional redis instance for persisting preview images
|
||||
export const isRedisEnabled: boolean = getSiteConfig('isRedisEnabled', false)
|
||||
export const isRedisEnabled: boolean =
|
||||
getSiteConfig('isRedisEnabled', false) || !!getEnv('REDIS_ENABLED', null)
|
||||
|
||||
// (if you want to enable redis, only REDIS_HOST and REDIS_PASSWORD are required)
|
||||
// we recommend that you store these in a local `.env` file
|
||||
|
||||
@@ -3,7 +3,7 @@ import lqip from 'lqip-modern'
|
||||
import pMap from 'p-map'
|
||||
import pMemoize from 'p-memoize'
|
||||
import { ExtendedRecordMap, PreviewImage, PreviewImageMap } from 'notion-types'
|
||||
import { getPageImageUrls } from 'notion-utils'
|
||||
import { getPageImageUrls, normalizeUrl } from 'notion-utils'
|
||||
|
||||
import { defaultPageIcon, defaultPageCover } from './config'
|
||||
import { db } from './db'
|
||||
@@ -18,22 +18,32 @@ import { mapImageUrl } from './map-image-url'
|
||||
export async function getPreviewImageMap(
|
||||
recordMap: ExtendedRecordMap
|
||||
): Promise<PreviewImageMap> {
|
||||
const urls: string[] = getPageImageUrls(recordMap, { mapImageUrl })
|
||||
const urls: string[] = getPageImageUrls(recordMap, {
|
||||
mapImageUrl
|
||||
})
|
||||
.concat([defaultPageIcon, defaultPageCover])
|
||||
.filter(Boolean)
|
||||
|
||||
const previewImagesMap = Object.fromEntries(
|
||||
await pMap(urls, async (url) => [url, await getPreviewImage(url)], {
|
||||
concurrency: 8
|
||||
})
|
||||
await pMap(
|
||||
urls,
|
||||
async (url) => {
|
||||
const cacheKey = normalizeUrl(url)
|
||||
return [cacheKey, await getPreviewImage(url, { cacheKey })]
|
||||
},
|
||||
{
|
||||
concurrency: 8
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
return previewImagesMap
|
||||
}
|
||||
|
||||
async function createPreviewImage(url: string): Promise<PreviewImage | null> {
|
||||
const cacheKey = url
|
||||
|
||||
async function createPreviewImage(
|
||||
url: string,
|
||||
{ cacheKey }: { cacheKey: string }
|
||||
): Promise<PreviewImage | null> {
|
||||
try {
|
||||
const cachedPreviewImage = await db.get(cacheKey)
|
||||
if (cachedPreviewImage) {
|
||||
@@ -42,7 +52,7 @@ async function createPreviewImage(url: string): Promise<PreviewImage | null> {
|
||||
|
||||
const { body } = await got(url, { responseType: 'buffer' })
|
||||
const result = await lqip(body)
|
||||
console.log('lqip', result.metadata)
|
||||
console.log('lqip', { ...result.metadata, url, cacheKey })
|
||||
|
||||
const previewImage = {
|
||||
originalWidth: result.metadata.originalWidth,
|
||||
|
||||
Reference in New Issue
Block a user