Files
supabase/apps/docs/scripts/llms.ts
Alaister Young cb34acd45c [GROWTH-773] chore(www): serve llms.txt content via API routes (#44897)
The docs build had a fragile implicit dependency on www's filesystem
(`../../../apps/www/public/llms`), flagged by the docs team in #44670.
Rather than formalising that dependency with a shared package, this PR
eliminates it entirely by making www the sole owner of llms content
assembly.

**How it works now:**

`/llms/[slug]` handles all `/llms/*.txt` requests via a 3-step cascade:
1. Dynamic content — `pricing.txt` generated at request time from
`shared-data` imports
2. Local file — product overviews read from `data/llms/`
3. Docs proxy — reference docs (guides, js, dart, etc.) fetched from the
docs app

No hardcoded slug lists, so adding new content just works.

**What changed:**

- `apps/docs/scripts/llms.ts` trimmed to only generate per-source
reference files — www now owns `llms.txt`, `llms-full.txt`, and product
overviews
- Removed `generateLlmsPricing.mjs` build script — pricing generated
dynamically from `shared-data`
- Removed llms rewrites from `rewrites.js` — routes handle everything
with consistent `Cache-Control: public, s-maxage=3600,
stale-while-revalidate=86400`
- Product overview `.txt` files moved from `public/llms/` → `data/llms/`
so all requests go through routes for consistent caching

**Docs team concerns from GROWTH-773:**

| Concern | Resolution |
|---------|-----------|
| Docs build depends on www files at a fragile relative path | Path
removed — docs no longer reads from www |
| www restructuring breaks docs with no obvious connection | Eliminated
— no cross-app filesystem dependency |
| No build order enforcement between www and docs | Not needed — docs
doesn't depend on www's build output |

## To test

- `curl <preview>/llms.txt` — markdown index with doc + product overview
links
- `curl <preview>/llms-full.txt` — combined product overviews + docs
content
- `curl <preview>/llms/pricing.txt` — dynamically generated pricing
tables
- `curl <preview>/llms/auth.txt` — product overview from local file
- `curl <preview>/llms/guides.txt` — proxied from docs app
- `curl <preview>/llms/nonexistent.txt` — 404
- Verify `Cache-Control` header on all responses

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-04-15 22:45:01 +09:00

145 lines
3.7 KiB
TypeScript

import './utils/dotenv.js'
import 'dotenv/config'
import fs from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { isFeatureEnabled } from '../../../packages/common/enabled-features/index.js'
import {
fetchCliLibReferenceSource,
fetchCSharpLibReferenceSource,
fetchDartLibReferenceSource,
fetchGuideSources,
fetchJsLibReferenceSource,
fetchKtLibReferenceSource,
fetchPythonLibReferenceSource,
fetchSwiftLibReferenceSource,
type SearchSource,
} from './search/sources/index.js'
interface Source {
title: string
/**
* Path relative to https://supabase.com. No leading slash
*/
relPath: string
fetch: () => Promise<SearchSource[]>
enabled: boolean
}
const {
sdkCsharp: sdkCsharpEnabled,
sdkDart: sdkDartEnabled,
sdkKotlin: sdkKotlinEnabled,
sdkPython: sdkPythonEnabled,
sdkSwift: sdkSwiftEnabled,
} = isFeatureEnabled(['sdk:csharp', 'sdk:dart', 'sdk:kotlin', 'sdk:python', 'sdk:swift'])
const SOURCES: Source[] = [
{
title: 'Supabase Guides',
relPath: 'llms/guides.txt',
fetch: fetchGuideSources,
enabled: true,
},
{
title: 'Supabase Reference (JavaScript)',
relPath: 'llms/js.txt',
fetch: async () =>
(await fetchJsLibReferenceSource()).filter(
(item): item is SearchSource => item !== undefined
),
enabled: true,
},
{
title: 'Supabase Reference (Dart)',
relPath: 'llms/dart.txt',
fetch: async () =>
(await fetchDartLibReferenceSource()).filter(
(item): item is SearchSource => item !== undefined
),
enabled: sdkDartEnabled,
},
{
title: 'Supabase Reference (Swift)',
relPath: 'llms/swift.txt',
fetch: async () =>
(await fetchSwiftLibReferenceSource()).filter(
(item): item is SearchSource => item !== undefined
),
enabled: sdkSwiftEnabled,
},
{
title: 'Supabase Reference (Kotlin)',
relPath: 'llms/kotlin.txt',
fetch: async () =>
(await fetchKtLibReferenceSource()).filter(
(item): item is SearchSource => item !== undefined
),
enabled: sdkKotlinEnabled,
},
{
title: 'Supabase Reference (Python)',
relPath: 'llms/python.txt',
fetch: async () =>
(await fetchPythonLibReferenceSource()).filter(
(item): item is SearchSource => item !== undefined
),
enabled: sdkPythonEnabled,
},
{
title: 'Supabase Reference (C#)',
relPath: 'llms/csharp.txt',
fetch: async () =>
(await fetchCSharpLibReferenceSource()).filter(
(item): item is SearchSource => item !== undefined
),
enabled: sdkCsharpEnabled,
},
{
title: 'Supabase CLI Reference',
relPath: 'llms/cli.txt',
fetch: async () =>
(await fetchCliLibReferenceSource()).filter(
(item): item is SearchSource => item !== undefined
),
enabled: true,
},
]
async function generateLlmsTxt() {
try {
await fs.mkdir('public/llms', { recursive: true })
const enabledSources = SOURCES.filter((source) => source.enabled !== false)
const fetchedSources = await Promise.all(
enabledSources.map(async (sourceDefn) => {
const source = await sourceDefn.fetch()
const sourceText = source
.map((section) => {
section.process()
return section.extractIndexedContent()
})
.join('\n\n')
return { defn: sourceDefn, text: sourceText }
})
)
await Promise.all(
fetchedSources.map(({ defn, text }) =>
fs.writeFile(`public/${defn.relPath}`, `${defn.title}\n\n${text}`)
)
)
} catch (err) {
console.error(err)
throw err
}
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {
generateLlmsTxt()
}
export { generateLlmsTxt, SOURCES }