Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP speed up site resolving #83

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: speed up site resolving
  • Loading branch information
transitive-bullshit committed Apr 6, 2021
commit 829de1811021b07cffdb5e3d5c5fe1182b160dba
6 changes: 3 additions & 3 deletions api/sitemap.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextApiRequest, NextApiResponse } from 'next'

import { SiteMap } from '../lib/types'
import { host } from '../lib/config'
import { getSiteMaps } from '../lib/get-site-maps'
import { getSiteMap } from '../lib/get-site-map'

export default async (
req: NextApiRequest,
Expand All @@ -12,15 +12,15 @@ export default async (
return res.status(405).send({ error: 'method not allowed' })
}

const siteMaps = await getSiteMaps()
const siteMap = await getSiteMap()

// cache sitemap for up to one hour
res.setHeader(
'Cache-Control',
'public, s-maxage=3600, max-age=3600, stale-while-revalidate=3600'
)
res.setHeader('Content-Type', 'text/xml')
res.write(createSitemap(siteMaps[0]))
res.write(createSitemap(siteMap))
res.end()
}

Expand Down
22 changes: 19 additions & 3 deletions lib/get-all-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@ export const getAllPages = pMemoize(getAllPagesImpl, { maxAge: 60000 * 5 })

export async function getAllPagesImpl(
rootNotionPageId: string,
rootNotionSpaceId: string
): Promise<Partial<types.SiteMap>> {
rootNotionSpaceId: string,
{
concurrency = 4,
pageConcurrency = 3,
full = false
}: {
concurrency?: number
pageConcurrency?: number
full?: boolean
} = {}
): Promise<types.PartialSiteMap> {
const pageMap = await getAllPagesInSpace(
rootNotionPageId,
rootNotionSpaceId,
notion.getPage.bind(notion)
(pageId: string) =>
notion.getPage(pageId, {
signFileUrls: full,
concurrency: pageConcurrency
}),
{
concurrency
}
)

const canonicalPageMap = Object.keys(pageMap).reduce(
Expand Down
34 changes: 20 additions & 14 deletions lib/get-preview-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,28 @@ export async function getPreviewImages(
}

const imageDocs = await db.db.getAll(...imageDocRefs)
const results = await pMap(imageDocs, async (model, index) => {
if (model.exists) {
return model.data() as types.PreviewImage
} else {
const json = {
url: images[index],
id: model.id
}
console.log('createPreviewImage server-side', json)
const results = await pMap(
imageDocs,
async (model, index) => {
if (model.exists) {
return model.data() as types.PreviewImage
} else {
const json = {
url: images[index],
id: model.id
}
console.log('createPreviewImage server-side', json)

// TODO: should we fire and forget here to speed up builds?
return got
.post(api.createPreviewImage, { json })
.json() as Promise<types.PreviewImage>
// TODO: should we fire and forget here to speed up builds?
return got
.post(api.createPreviewImage, { json })
.json() as Promise<types.PreviewImage>
}
},
{
concurrency: 16
}
})
)

return results
.filter(Boolean)
Expand Down
31 changes: 31 additions & 0 deletions lib/get-site-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getAllPages } from './get-all-pages'
import { getSiteForDomain } from './get-site-for-domain'
import * as config from './config'
import * as types from './types'

export async function getSiteMap({
concurrency = 4,
pageConcurrency = 3,
full = false
}: {
concurrency?: number
pageConcurrency?: number
full?: boolean
} = {}): Promise<types.SiteMap> {
const site = await getSiteForDomain(config.domain)

const siteMap = await getAllPages(
site.rootNotionPageId,
site.rootNotionSpaceId,
{
concurrency,
pageConcurrency,
full
}
)

return {
site,
...siteMap
}
}
35 changes: 0 additions & 35 deletions lib/get-site-maps.ts

This file was deleted.

7 changes: 0 additions & 7 deletions lib/get-sites.ts

This file was deleted.

9 changes: 4 additions & 5 deletions lib/resolve-notion-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as acl from './acl'
import * as types from './types'
import { pageUrlOverrides, pageUrlAdditions } from './config'
import { getPage } from './notion'
import { getSiteMaps } from './get-site-maps'
import { getSiteMap } from './get-site-map'
import { getSiteForDomain } from './get-site-for-domain'

export async function resolveNotionPage(domain: string, rawPageId?: string) {
Expand Down Expand Up @@ -37,10 +37,9 @@ export async function resolveNotionPage(domain: string, rawPageId?: string) {
recordMap = resources[1]
} else {
// handle mapping of user-friendly canonical page paths to Notion page IDs
// e.g., /developer-x-entrepreneur versus /71201624b204481f862630ea25ce62fe
const siteMaps = await getSiteMaps()
const siteMap = siteMaps[0]
pageId = siteMap.canonicalPageMap[rawPageId]
// e.g., /foo versus /71201624b204481f862630ea25ce62fe
const siteMap = await getSiteMap()
pageId = siteMap?.canonicalPageMap?.[rawPageId]

if (pageId) {
// TODO: we're not re-using the site from siteMaps because it is
Expand Down
5 changes: 5 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export interface SiteMap {
canonicalPageMap: CanonicalPageMap
}

export interface PartialSiteMap {
pageMap: PageMap
canonicalPageMap: CanonicalPageMap
}

export interface CanonicalPageMap {
[canonicalPageId: string]: string
}
Expand Down
24 changes: 10 additions & 14 deletions pages/[pageId].tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { isDev, domain } from 'lib/config'
import { getSiteMaps } from 'lib/get-site-maps'
import { getSiteMap } from 'lib/get-site-map'
import { resolveNotionPage } from 'lib/resolve-notion-page'
import { NotionPage } from 'components'

Expand Down Expand Up @@ -36,22 +36,18 @@ export async function getStaticPaths() {
}
}

const siteMaps = await getSiteMaps()
const siteMap = await getSiteMap()
const paths = Object.keys(siteMap.canonicalPageMap).map((pageId) => ({
params: {
pageId
}
}))
console.log(paths)

const ret = {
paths: siteMaps.flatMap((siteMap) =>
Object.keys(siteMap.canonicalPageMap).map((pageId) => ({
params: {
pageId
}
}))
),
// paths: [],
return {
paths,
fallback: true
}

console.log(ret.paths)
return ret
}

export default function NotionDomainDynamicPage(props) {
Expand Down
5 changes: 5 additions & 0 deletions site.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ module.exports = {
// variables specified in .env.example
isPreviewImageSupportEnabled: false,

// whether or not to include notion IDs as suffixes in URLs
// NOTE: this will make incremental SSG much faster with the downside of
// having less pretty URLs
includeNotionIdInUrls: false,

// map of notion page IDs to URL paths (optional)
// any pages defined here will override their default URL paths
// example:
Expand Down