mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
Coding agents guess docs URLs that don't exist: markdown variants of pages that have no markdown (`/docs/reference/<sdk>/<fn>.md`, `/docs/guides/<slug>.md`) and `/docs/llms.txt`. Reference guesses get an empty-body 404 from the crawler renderer for bot user agents and the full HTML reference for browsers, and the rest get the HTML 404 page, so the agent never learns where the content lives. Reference `.md` guesses are about half of coding-agent docs 4xx, and guide `.md` guesses about a tenth. **Changed:** - **Reference `.md` guesses**: the docs proxy answers every `/docs/reference/**.md` request itself, for any user agent and before the crawler rewrite, with a markdown 404 (`no-store`) linking the HTML page and the SDK's full markdown reference (`/llms/js.txt`, `/llms/dart.txt`, and so on), or `/llms.txt` for references without one. - **Unknown guide `.md` guesses**: guide `.md` requests whose slug has no markdown variant get the same markdown 404 instead of the HTML 404. The body says there is no markdown version of the page, because a few real guide pages (CLI config, self-hosting config, troubleshooting indexes) are TSX-only. Known guides and Accept-negotiated requests without `.md` are unchanged. - **`/docs/llms.txt`**: I added a permanent www redirect to `/llms.txt`, next to the existing `/docs/reference/llms.md` redirect. **Note:** the `.md` check in `proxy.ts` catches every reference `.md` URL, so it has to be narrowed when per-function reference markdown ships, or it will 404 those files. ## To test Tested on the PR previews (docs deployment for the 404s, www deployment for the redirect): - [x] `curl -sD - -A 'curl/8.7.1' <docs-preview>/docs/reference/javascript/select.md`: expect 404, `content-type: text/markdown`, `cache-control: no-store`, and a body linking `https://supabase.com/docs/reference/javascript/select` and `https://supabase.com/llms/js.txt` - [x] Repeat for `dart/select.md`, `python/select.md`, `cli/supabase-init.md` and `api/v1-list-all-projects.md`: expect the matching `/llms/<sdk>.txt` - [x] Repeat `javascript/select.md` with a browser user agent: expect the same markdown 404 - [x] `curl -sD - <docs-preview>/docs/guides/doesnotexist.md`: expect 404 markdown with `no-store`, linking `https://supabase.com/llms.txt` - [x] `curl -s <docs-preview>/docs/guides/local-development/cli/config.md`: expect 404 markdown linking `https://supabase.com/docs/guides/local-development/cli/config` - [x] `curl -sD - <docs-preview>/docs/guides/database/overview.md`: expect 200 markdown, unchanged - [x] Open `<docs-preview>/docs/guides/doesnotexist` in a browser: expect the branded HTML 404, unchanged - [x] `curl -sIL -o /dev/null -w '%{num_redirects} %{http_code}\n' <www-preview>/docs/llms.txt`, once with a browser user agent and once with `Googlebot/2.1`: expect `1 200` ## Linear - fixes GROWTH-1325
129 lines
4.3 KiB
TypeScript
129 lines
4.3 KiB
TypeScript
import { clientSdkIds } from '~/content/navigation.references'
|
|
import { BASE_PATH, PROD_URL } from '~/lib/constants'
|
|
import { MARKDOWN_CONTENT_TYPE } from '~/lib/md-alternates'
|
|
import MARKDOWN_SLUGS from '~/public/markdown/manifest.json'
|
|
import { negotiateMarkdown } from 'common/markdown-negotiation'
|
|
import { isbot } from 'isbot'
|
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
|
|
const REFERENCE_PATH = `${BASE_PATH ?? ''}/reference`
|
|
const GUIDES_PATH = `${BASE_PATH ?? ''}/guides`
|
|
const GUIDES_MARKDOWN_SLUGS = new Set(MARKDOWN_SLUGS)
|
|
const REFERENCE_MARKDOWN_LIBS = new Set([...clientSdkIds, 'cli', 'api'])
|
|
const LLMS_FILE_NAMES: Record<string, string> = { javascript: 'js' }
|
|
|
|
function markdownUrl(section: string, lib: string | undefined) {
|
|
if (section !== 'reference' || !lib || !REFERENCE_MARKDOWN_LIBS.has(lib)) {
|
|
return 'https://supabase.com/llms.txt'
|
|
}
|
|
return `https://supabase.com/llms/${LLMS_FILE_NAMES[lib] ?? lib}.txt`
|
|
}
|
|
|
|
function markdownNotFound(pathname: string) {
|
|
const relativePath = pathname.replace(`${BASE_PATH}/`, '').replace(/\.md$/, '')
|
|
const [section, lib] = relativePath.split('/')
|
|
|
|
const markdown = `# 404 Not Found
|
|
|
|
There is no markdown version of \`${BASE_PATH}/${relativePath}\`.
|
|
|
|
- HTML page: <${PROD_URL}/${relativePath}>
|
|
- Markdown: <${markdownUrl(section, lib)}>
|
|
`
|
|
|
|
return new NextResponse(markdown, {
|
|
status: 404,
|
|
headers: {
|
|
'Content-Type': MARKDOWN_CONTENT_TYPE,
|
|
'Cache-Control': 'no-store',
|
|
'X-Content-Type-Options': 'nosniff',
|
|
},
|
|
})
|
|
}
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const url = new URL(request.url)
|
|
const { pathname } = url
|
|
|
|
if (pathname.startsWith(GUIDES_PATH + '/')) {
|
|
const isMdSuffix = pathname.endsWith('.md')
|
|
const slug = pathname.replace(`${GUIDES_PATH}/`, '').replace(/\.md$/, '')
|
|
const decision = negotiateMarkdown(
|
|
{ acceptHeader: request.headers.get('accept') ?? '' },
|
|
{ hasMarkdownVariant: GUIDES_MARKDOWN_SLUGS.has(slug), isMarkdownSuffix: isMdSuffix }
|
|
)
|
|
|
|
if (decision === 'not-acceptable') {
|
|
return new NextResponse('Not Acceptable', {
|
|
status: 406,
|
|
headers: { 'Cache-Control': 'no-store', Vary: 'Accept' },
|
|
})
|
|
}
|
|
|
|
if (decision === 'markdown') {
|
|
const rewriteUrl = new URL(url)
|
|
rewriteUrl.pathname = `${BASE_PATH ?? ''}/api/guides-md/${slug}`
|
|
return NextResponse.rewrite(rewriteUrl)
|
|
}
|
|
}
|
|
|
|
// Reference pages have no per-page markdown yet, so every reference .md is a guess.
|
|
// Narrow this when per-page reference markdown ships, or it will 404 those files.
|
|
if (pathname.endsWith('.md')) {
|
|
return markdownNotFound(pathname)
|
|
}
|
|
|
|
if (!pathname.startsWith(REFERENCE_PATH)) {
|
|
return NextResponse.next()
|
|
}
|
|
|
|
if (isbot(request.headers.get('user-agent'))) {
|
|
let [, lib, maybeVersion, ...slug] = pathname.replace(REFERENCE_PATH, '').split('/')
|
|
|
|
if (clientSdkIds.includes(lib)) {
|
|
const version = /^v\d+$/.test(maybeVersion) ? maybeVersion : undefined
|
|
if (!version) {
|
|
slug = [maybeVersion, ...slug]
|
|
}
|
|
|
|
if (slug.length > 0) {
|
|
const rewriteUrl = new URL(url)
|
|
rewriteUrl.pathname = (BASE_PATH ?? '') + '/api/crawlers'
|
|
return NextResponse.rewrite(rewriteUrl)
|
|
}
|
|
}
|
|
}
|
|
|
|
const [, lib, maybeVersion] = pathname.replace(REFERENCE_PATH, '').split('/')
|
|
|
|
if (lib === 'cli') {
|
|
const rewritePath = [REFERENCE_PATH, 'cli'].join('/')
|
|
return NextResponse.rewrite(new URL(rewritePath, request.url))
|
|
}
|
|
|
|
// Spike (DOCS-1268): only the bare /reference/api needs normalizing now.
|
|
// /reference/api/<slug> has its own statically generated page — don't
|
|
// collapse it back to the monolith.
|
|
if (lib === 'api' && !maybeVersion) {
|
|
const rewritePath = [REFERENCE_PATH, 'api'].join('/')
|
|
return NextResponse.rewrite(new URL(rewritePath, request.url))
|
|
}
|
|
|
|
if (lib?.startsWith('self-hosting-')) {
|
|
const rewritePath = [REFERENCE_PATH, lib].join('/')
|
|
return NextResponse.rewrite(new URL(rewritePath, request.url))
|
|
}
|
|
|
|
if (clientSdkIds.includes(lib)) {
|
|
const version = /^v\d+$/.test(maybeVersion) ? maybeVersion : null
|
|
const rewritePath = [REFERENCE_PATH, lib, version].filter(Boolean).join('/')
|
|
return NextResponse.rewrite(new URL(rewritePath, request.url))
|
|
}
|
|
|
|
return NextResponse.next()
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ['/reference/:path*', '/guides/:path*'],
|
|
}
|