Skip to content

Commit

Permalink
feat: add caching for page URI to notion IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Apr 4, 2022
1 parent 21e03be commit 8d9c756
Show file tree
Hide file tree
Showing 6 changed files with 735 additions and 744 deletions.
18 changes: 11 additions & 7 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ export const rootNotionSpaceId: string | null = parsePageId(

export const pageUrlOverrides = cleanPageUrlMap(
getSiteConfig('pageUrlOverrides', {}) || {},
'pageUrlOverrides'
{ label: 'pageUrlOverrides' }
)

export const inversePageUrlOverrides = invertPageUrlOverrides(pageUrlOverrides)

export const pageUrlAdditions = cleanPageUrlMap(
getSiteConfig('pageUrlAdditions', {}) || {},
'pageUrlAdditions'
{ label: 'pageUrlAdditions' }
)

export const isDev =
process.env.NODE_ENV === 'development' || !process.env.NODE_ENV
export const inversePageUrlOverrides = invertPageUrlOverrides(pageUrlOverrides)

export const environment = process.env.NODE_ENV || 'development'
export const isDev = environment === 'development'

// general site config
export const name: string = getSiteConfig('name')
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions lib/db.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
6 changes: 4 additions & 2 deletions lib/preview-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand All @@ -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
Expand Down
46 changes: 36 additions & 10 deletions lib/resolve-notion-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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]
Expand All @@ -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
Expand All @@ -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}"`,
Expand Down
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}
}
}
Loading

0 comments on commit 8d9c756

Please sign in to comment.