mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-13 15:09:47 +00:00
feat: add caching for page URI to notion IDs
This commit is contained in:
@@ -26,18 +26,18 @@ export const rootNotionSpaceId: string | null = parsePageId(
|
||||
|
||||
export const pageUrlOverrides = cleanPageUrlMap(
|
||||
getSiteConfig('pageUrlOverrides', {}) || {},
|
||||
'pageUrlOverrides'
|
||||
{ label: 'pageUrlOverrides' }
|
||||
)
|
||||
|
||||
export const pageUrlAdditions = cleanPageUrlMap(
|
||||
getSiteConfig('pageUrlAdditions', {}) || {},
|
||||
{ label: 'pageUrlAdditions' }
|
||||
)
|
||||
|
||||
export const inversePageUrlOverrides = invertPageUrlOverrides(pageUrlOverrides)
|
||||
|
||||
export const pageUrlAdditions = cleanPageUrlMap(
|
||||
getSiteConfig('pageUrlAdditions', {}) || {},
|
||||
'pageUrlAdditions'
|
||||
)
|
||||
|
||||
export const isDev =
|
||||
process.env.NODE_ENV === 'development' || !process.env.NODE_ENV
|
||||
export const environment = process.env.NODE_ENV || 'development'
|
||||
export const isDev = environment === 'development'
|
||||
|
||||
// general site config
|
||||
export const name: string = getSiteConfig('name')
|
||||
@@ -128,7 +128,11 @@ export const fathomConfig = fathomId
|
||||
|
||||
function cleanPageUrlMap(
|
||||
pageUrlMap: PageUrlOverridesMap,
|
||||
label: string
|
||||
{
|
||||
label
|
||||
}: {
|
||||
label: string
|
||||
}
|
||||
): PageUrlOverridesMap {
|
||||
return Object.keys(pageUrlMap).reduce((acc, uri) => {
|
||||
const pageId = pageUrlMap[uri]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import Keyv from 'keyv'
|
||||
import KeyvRedis from '@keyv/redis'
|
||||
import Keyv from '@keyvhq/core'
|
||||
import KeyvRedis from '@keyvhq/redis'
|
||||
|
||||
import { isRedisEnabled, redisUrl, redisNamespace } from './config'
|
||||
|
||||
|
||||
@@ -50,8 +50,9 @@ async function createPreviewImage(
|
||||
if (cachedPreviewImage) {
|
||||
return cachedPreviewImage
|
||||
}
|
||||
} catch {
|
||||
} catch (err) {
|
||||
// ignore redis errors
|
||||
console.warn(`redis error get "${cacheKey}"`, err.message)
|
||||
}
|
||||
|
||||
const { body } = await got(url, { responseType: 'buffer' })
|
||||
@@ -66,8 +67,9 @@ async function createPreviewImage(
|
||||
|
||||
try {
|
||||
await db.set(cacheKey, previewImage)
|
||||
} catch {
|
||||
} catch (err) {
|
||||
// ignore redis errors
|
||||
console.warn(`redis error set "${cacheKey}"`, err.message)
|
||||
}
|
||||
|
||||
return previewImage
|
||||
|
||||
@@ -3,7 +3,8 @@ import { ExtendedRecordMap } from 'notion-types'
|
||||
|
||||
import * as acl from './acl'
|
||||
import * as types from './types'
|
||||
import { pageUrlOverrides, pageUrlAdditions } from './config'
|
||||
import { pageUrlOverrides, pageUrlAdditions, environment } from './config'
|
||||
import { db } from './db'
|
||||
import { getPage } from './notion'
|
||||
import { getSiteMaps } from './get-site-maps'
|
||||
import { getSiteForDomain } from './get-site-for-domain'
|
||||
@@ -17,7 +18,7 @@ export async function resolveNotionPage(domain: string, rawPageId?: string) {
|
||||
pageId = parsePageId(rawPageId)
|
||||
|
||||
if (!pageId) {
|
||||
// check if the site configuration provides an override of a fallback for
|
||||
// check if the site configuration provides an override or a fallback for
|
||||
// the page's URI
|
||||
const override =
|
||||
pageUrlOverrides[rawPageId] || pageUrlAdditions[rawPageId]
|
||||
@@ -27,14 +28,29 @@ export async function resolveNotionPage(domain: string, rawPageId?: string) {
|
||||
}
|
||||
}
|
||||
|
||||
const useUriToPageIdCache = true
|
||||
const cacheKey = `uri-to-page-id:${domain}:${environment}:${rawPageId}`
|
||||
// TODO: should we use a TTL for these mappings or make them permanent?
|
||||
// const cacheTTL = 8.64e7 // one day in milliseconds
|
||||
const cacheTTL = undefined // disable cache TTL
|
||||
|
||||
if (!pageId && useUriToPageIdCache) {
|
||||
try {
|
||||
// check if the database has a cached mapping of this URI to page ID
|
||||
pageId = await db.get(cacheKey)
|
||||
|
||||
// console.log(`redis get "${cacheKey}"`, pageId)
|
||||
} catch (err) {
|
||||
// ignore redis errors
|
||||
console.warn(`redis error get "${cacheKey}"`, err.message)
|
||||
}
|
||||
}
|
||||
|
||||
if (pageId) {
|
||||
const resources = await Promise.all([
|
||||
;[site, recordMap] = await Promise.all([
|
||||
getSiteForDomain(domain),
|
||||
getPage(pageId)
|
||||
])
|
||||
|
||||
site = resources[0]
|
||||
recordMap = resources[1]
|
||||
} else {
|
||||
// handle mapping of user-friendly canonical page paths to Notion page IDs
|
||||
// e.g., /developer-x-entrepreneur versus /71201624b204481f862630ea25ce62fe
|
||||
@@ -43,19 +59,29 @@ export async function resolveNotionPage(domain: string, rawPageId?: string) {
|
||||
pageId = siteMap?.canonicalPageMap[rawPageId]
|
||||
|
||||
if (pageId) {
|
||||
// TODO: we're not re-using the site from siteMaps because it is
|
||||
// TODO: we're not re-using the page recordMap from siteMaps because it is
|
||||
// cached aggressively
|
||||
// site = await getSiteForDomain(domain)
|
||||
// recordMap = siteMap.pageMap[pageId]
|
||||
|
||||
const resources = await Promise.all([
|
||||
;[site, recordMap] = await Promise.all([
|
||||
getSiteForDomain(domain),
|
||||
getPage(pageId)
|
||||
])
|
||||
|
||||
site = resources[0]
|
||||
recordMap = resources[1]
|
||||
if (useUriToPageIdCache) {
|
||||
try {
|
||||
// update the database mapping of URI to pageId
|
||||
await db.set(cacheKey, pageId, cacheTTL)
|
||||
|
||||
// console.log(`redis set "${cacheKey}"`, pageId, { cacheTTL })
|
||||
} catch (err) {
|
||||
// ignore redis errors
|
||||
console.warn(`redis error set "${cacheKey}"`, err.message)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// note: we're purposefully not caching URI to pageId mappings for 404s
|
||||
return {
|
||||
error: {
|
||||
message: `Not found "${rawPageId}"`,
|
||||
|
||||
17
package.json
17
package.json
@@ -26,27 +26,27 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@fisch0920/use-dark-mode": "^2.4.0",
|
||||
"@keyv/redis": "^2.2.3",
|
||||
"@keyvhq/core": "^1.6.9",
|
||||
"@keyvhq/redis": "^1.6.10",
|
||||
"classnames": "^2.3.1",
|
||||
"date-fns": "^2.28.0",
|
||||
"expiry-map": "^2.0.0",
|
||||
"fathom-client": "^3.4.1",
|
||||
"got": "^12.0.3",
|
||||
"isomorphic-unfetch": "^3.1.0",
|
||||
"keyv": "^4.1.1",
|
||||
"lqip-modern": "^1.2.0",
|
||||
"next": "^12.1.1",
|
||||
"node-fetch": "^2.6.1",
|
||||
"notion-client": "^6.5.0",
|
||||
"notion-types": "^6.5.0",
|
||||
"notion-utils": "^6.5.0",
|
||||
"notion-client": "^6.7.0-alpha.1",
|
||||
"notion-types": "^6.7.0-alpha.0",
|
||||
"notion-utils": "^6.7.0-alpha.1",
|
||||
"p-map": "^5.3.0",
|
||||
"p-memoize": "^6.0.1",
|
||||
"react": "^17.0.2",
|
||||
"react-body-classname": "^1.3.1",
|
||||
"react-dom": "^17.0.2",
|
||||
"react-icons": "^4.3.1",
|
||||
"react-notion-x": "^6.6.2",
|
||||
"react-notion-x": "^6.7.0-alpha.1",
|
||||
"react-static-tweets": "^0.7.1",
|
||||
"react-use": "^17.3.2",
|
||||
"static-tweets": "^0.7.1"
|
||||
@@ -66,5 +66,10 @@
|
||||
"npm-run-all": "^4.1.5",
|
||||
"prettier": "^2.4.1",
|
||||
"typescript": "^4.4.4"
|
||||
},
|
||||
"overrides": {
|
||||
"cacheable-request": {
|
||||
"keyv": "npm:@keyvhq/core@~1.6.6"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user