Files
supabase/apps/docs/features/docs/Reference.generated.singleton.ts
T
Charis fc164b5d07 Refactor/app router refs (#28095)
Migrates client SDK References to App Router. (Management and CLI API references aren't migrated yet, nor are self-hosting config references.)

Some notes:

Big changes to the way crawler pages are built and individual section URLs (e.g., javascript/select) are served. All of these used to be SSG-generated pages, but the number of heavy pages was just too much to handle -- slow as molasses and my laptop sounded like it was taking off, and CI sometimes refuses to build it all at all.

Tried various tricks with caching and pre-generating data but no dice.

So I changed to only building one copy of each SDK+version page, then serving the sub-URLs through a response rewrite. That's for the actual user-visible pages.

For the bot pages, each sub-URL needs to be its own page, but prebuilding it doesn't work, and rendering on demand from React components is too slow (looking for super-fast response here for SEO). Instead I changed to using an API route that serves very minimal, hand-crafted HTML. It looks ugly, but it's purely for the search bots.

You can test what bots see by running curl --user-agent "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" <URL_OF_PAGE>

Also added some smoke tests to run against prod for the crawler routes, since we don't keep an eye on those regularly, and Vercel config changes could surprise-break them. Tested the meta images on Open Graph and all seems to work fine.

With this approach, full production builds are really fast: ~5 minutes

Starts using the new type spec handling, which is better at finding params automatically, so I could remove some of the manually written ones from the spec files.
2024-08-13 16:12:59 -04:00

101 lines
2.7 KiB
TypeScript

import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import type { ModuleTypes } from '~/features/docs/Reference.typeSpec'
import type { AbbrevCommonClientLibSection } from '~/features/docs/Reference.utils'
let typeSpec: Array<ModuleTypes>
async function _typeSpecSingleton() {
if (!typeSpec) {
const rawJson = await readFile(
join(process.cwd(), 'features/docs', './generated/typeSpec.json'),
'utf-8'
)
typeSpec = JSON.parse(rawJson, (key, value) => {
if (key === 'methods') {
return new Map(Object.entries(value))
} else {
return value
}
})
}
return typeSpec
}
export async function getTypeSpec(ref: string) {
const modules = await _typeSpecSingleton()
const delimiter = ref.indexOf('.')
const refMod = ref.substring(0, delimiter)
const mod = modules.find((mod) => mod.name === refMod)
return mod?.methods.get(ref)
}
const functionsList = new Map<string, Array<{ id: unknown }>>()
export async function getFunctionsList(sdkId: string, version: string) {
const key = `${sdkId}.${version}`
if (!functionsList.has(key)) {
const data = await readFile(
join(process.cwd(), 'features/docs', `./generated/${sdkId}.${version}.functions.json`),
'utf-8'
)
functionsList.set(key, JSON.parse(data))
}
return functionsList.get(key)
}
const referenceSections = new Map<string, Array<AbbrevCommonClientLibSection>>()
export async function getReferenceSections(sdkId: string, version: string) {
const key = `${sdkId}.${version}`
if (!referenceSections.has(key)) {
const data = await readFile(
join(process.cwd(), 'features/docs', `./generated/${sdkId}.${version}.sections.json`),
'utf-8'
)
referenceSections.set(key, JSON.parse(data))
}
return referenceSections.get(key)
}
const flatSections = new Map<string, Array<AbbrevCommonClientLibSection>>()
export async function getFlattenedSections(sdkId: string, version: string) {
const key = `${sdkId}.${version}`
if (!flatSections.has(key)) {
const data = await readFile(
join(process.cwd(), 'features/docs', `./generated/${sdkId}.${version}.flat.json`),
'utf-8'
)
flatSections.set(key, JSON.parse(data))
}
return flatSections.get(key)
}
const sectionsBySlug = new Map<string, Map<string, AbbrevCommonClientLibSection>>()
export async function getSectionsBySlug(sdkId: string, version: string) {
const key = `${sdkId}.${version}`
if (!sectionsBySlug.has(key)) {
const data = await readFile(
join(process.cwd(), 'features/docs', `./generated/${sdkId}.${version}.bySlug.json`),
'utf-8'
)
const asObject = JSON.parse(data)
sectionsBySlug.set(key, new Map(Object.entries(asObject)))
}
return sectionsBySlug.get(key)
}