mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-06-01 15:11:00 +00:00
feat: minor fixes
This commit is contained in:
@@ -17,7 +17,7 @@ export const fathomConfig = fathomId
|
||||
}
|
||||
: undefined
|
||||
|
||||
// TODO: address duplication between server-side env and client-side config
|
||||
// TODO: fix duplication between server-side env and client-side config
|
||||
export const apiBaseUrl = `/api`
|
||||
export const api = {
|
||||
createPreviewImage: `${apiBaseUrl}/create-preview-image`,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import pMemoize from 'p-memoize'
|
||||
import { getAllPagesInSpace, getCanonicalPageId } from 'notion-utils'
|
||||
|
||||
import * as types from './types'
|
||||
import notion from './notion'
|
||||
|
||||
export const getAllPages = pMemoize(getAllPagesImpl, { maxAge: 60000 * 5 })
|
||||
@@ -8,16 +9,44 @@ export const getAllPages = pMemoize(getAllPagesImpl, { maxAge: 60000 * 5 })
|
||||
export async function getAllPagesImpl(
|
||||
rootNotionPageId: string,
|
||||
rootNotionSpaceId: string
|
||||
): Promise<string[]> {
|
||||
const pages = await getAllPagesInSpace(
|
||||
): Promise<Partial<types.SiteMap>> {
|
||||
const pageMap = await getAllPagesInSpace(
|
||||
rootNotionPageId,
|
||||
rootNotionSpaceId,
|
||||
notion.getPage.bind(notion)
|
||||
)
|
||||
|
||||
const canonicalPageIds = Object.keys(pages)
|
||||
.map((pageId) => getCanonicalPageId(pageId, pages[pageId]))
|
||||
.filter(Boolean)
|
||||
const canonicalPageMap = Object.keys(pageMap).reduce(
|
||||
(map, pageId: string) => {
|
||||
const recordMap = pageMap[pageId]
|
||||
const canonicalPageId = getCanonicalPageId(pageId, recordMap, {
|
||||
uuid: false
|
||||
})
|
||||
|
||||
return canonicalPageIds
|
||||
if (map[canonicalPageId]) {
|
||||
console.error(
|
||||
'duplicate canonical page id',
|
||||
canonicalPageId,
|
||||
pageId,
|
||||
map[canonicalPageId].pageId
|
||||
)
|
||||
|
||||
return map
|
||||
} else {
|
||||
return {
|
||||
...map,
|
||||
[canonicalPageId]: {
|
||||
pageId,
|
||||
recordMap
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{}
|
||||
)
|
||||
|
||||
return {
|
||||
pageMap,
|
||||
canonicalPageMap
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,14 +11,17 @@ export async function getSiteMaps(): Promise<types.SiteMap[]> {
|
||||
sites,
|
||||
async (site, index) => {
|
||||
try {
|
||||
console.log('getSiteMap', index, site)
|
||||
console.log(
|
||||
'getSiteMap',
|
||||
`${index + 1}/${sites.length}`,
|
||||
`(${(((index + 1) / sites.length) * 100) | 0}%)`,
|
||||
site
|
||||
)
|
||||
|
||||
return {
|
||||
site,
|
||||
pageIds: await getAllPages(
|
||||
site.rootNotionPageId,
|
||||
site.rootNotionSpaceId
|
||||
)
|
||||
}
|
||||
...(await getAllPages(site.rootNotionPageId, site.rootNotionSpaceId))
|
||||
} as types.SiteMap
|
||||
} catch (err) {
|
||||
console.warn('site build error', index, site, err)
|
||||
}
|
||||
|
||||
@@ -9,7 +9,10 @@ export const mapPageUrl = (
|
||||
if (uuidToId(pageId) === site.rootNotionPageId) {
|
||||
return createUrl('/', searchParams)
|
||||
} else {
|
||||
return createUrl(`/${getCanonicalPageId(pageId, recordMap)}`, searchParams)
|
||||
return createUrl(
|
||||
`/${getCanonicalPageId(pageId, recordMap, { uuid: false })}`,
|
||||
searchParams
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +25,9 @@ export const getCanonicalPageUrl = (
|
||||
if (uuidToId(pageId) === site.rootNotionPageId) {
|
||||
return `https://${site.domain}`
|
||||
} else {
|
||||
return `https://${site.domain}/${getCanonicalPageId(pageUuid, recordMap)}`
|
||||
return `https://${site.domain}/${getCanonicalPageId(pageUuid, recordMap, {
|
||||
uuid: false
|
||||
})}`
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import * as acl from './acl'
|
||||
import * as types from './types'
|
||||
import { parsePageId } from 'notion-utils'
|
||||
import { getPage } from './notion'
|
||||
import { getSiteMaps } from './get-site-maps'
|
||||
import { getSiteForDomain } from './get-site-for-domain'
|
||||
|
||||
export async function resolveNotionPage(domain: string, rawPageId?: string) {
|
||||
@@ -12,6 +13,24 @@ export async function resolveNotionPage(domain: string, rawPageId?: string) {
|
||||
if (rawPageId && rawPageId !== 'index') {
|
||||
pageId = parsePageId(rawPageId)
|
||||
|
||||
// handle mapping user-friendly canonical page paths to Notion page IDs
|
||||
// e.g., /developer-x-entrepreneur versus /71201624b204481f862630ea25ce62fe
|
||||
if (!pageId) {
|
||||
const siteMaps = await getSiteMaps()
|
||||
const siteMap = siteMaps[0]
|
||||
console.log(siteMap)
|
||||
pageId = siteMap.canonicalPageMap[pageId]?.pageId
|
||||
|
||||
if (!pageId) {
|
||||
return {
|
||||
error: {
|
||||
message: `Invalid notion page ID "${rawPageId}"`,
|
||||
statusCode: 404
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!pageId) {
|
||||
return {
|
||||
error: {
|
||||
|
||||
12
lib/types.ts
12
lib/types.ts
@@ -1,4 +1,4 @@
|
||||
import { Block, ExtendedRecordMap } from 'notion-types'
|
||||
import { ExtendedRecordMap, PageMap } from 'notion-types'
|
||||
|
||||
export * from 'notion-types'
|
||||
|
||||
@@ -47,7 +47,15 @@ export interface Site extends Model {
|
||||
|
||||
export interface SiteMap {
|
||||
site: Site
|
||||
pageIds: string[]
|
||||
pageMap: PageMap
|
||||
canonicalPageMap: CanonicalPageMap
|
||||
}
|
||||
|
||||
export interface CanonicalPageMap {
|
||||
[canonicalPagePath: string]: {
|
||||
pageId: string
|
||||
recordMap: ExtendedRecordMap | null
|
||||
}
|
||||
}
|
||||
|
||||
export interface PreviewImage {
|
||||
|
||||
Reference in New Issue
Block a user