Files
supabase/apps/docs/app/api/crawlers/route.test.ts
Miranda Limonczenko e44837c256 feat(docs): point every docs page at llms.txt in HTML and markdown (#53965)
Closes DOCS-1756

## Problem

No docs page tells an agent that `llms.txt` exists or that a markdown
version is available. Agents that send `Accept: text/markdown` already
get markdown, but agents that fetch HTML have no signal to try the
markdown path.


## Solution

- **HTML:** add a visually hidden directive as the first child of
`<body>` in the root layout, so it comes before the navigation on every
guide and reference page. It links to
`https://supabase.com/docs/llms.txt` and says guide pages have a
markdown version at the same path with `.md` appended, before any query
string or fragment. It's `sr-only` and `aria-hidden`, and the link has
`tabindex="-1"`.
- **Crawler HTML:** bot requests for SDK reference pages are rewritten
to `/api/crawlers`, which skips the root layout. The same directive goes
before its `<nav>`.
- **Markdown:** add a sentence linking `llms.txt` to the "Note for AI
agents" blockquote in the guides markdown route. The agent feedback page
skips the feedback text to avoid linking to itself, but it keeps the
`llms.txt` line.
- Add `LLMS_TXT_URL` and `LLMS_TXT_DIRECTIVE_HTML` to
`common/markdown-affordance`, and reuse `LLMS_TXT_URL` in `proxy.ts`.

The directive limits the `.md` claim to guides, because reference `.md`
requests 404. It doesn't say `llms.txt` lists every page.

The link targets `/docs/llms.txt`, the docs index that AFDocs scores for
the `/docs` base URL. #53968 makes it a real file. Until that merges,
`/docs/llms.txt` 308-redirects to the site-wide `/llms.txt`, so the link
works in either merge order.

## Manual testing

1. Open a Vercel preview guide page and view source. Confirm the
`llms.txt` directive comes before the navigation markup.
2. Open a reference page and confirm the same.
3. Confirm the directive isn't visible and isn't tabbable.
4. Request a reference section page with a bot user agent, such as `curl
-A Googlebot`, and confirm the directive comes before `<nav>`.
5. Request a guide with `.md` appended and confirm the opening
blockquote links `llms.txt`.
6. Request `/docs/guides/ai-tools/agent-feedback.md` and confirm the
opening blockquote links `llms.txt`.
7. Run AFDocs against the preview and confirm `llms-txt-directive-html`
and `llms-txt-directive-md` pass.
2026-11-24 15:02:23 -08:00

136 lines
4.9 KiB
TypeScript

import { LLMS_TXT_URL } from 'common/markdown-affordance'
import { beforeEach, describe, expect, it, vi } from 'vitest'
const reference = vi.hoisted(() => ({
sections: [] as Array<{ id: string; slug: string; title: string; type: string }>,
}))
vi.mock('~/features/docs/Reference.generated.singleton', () => ({
getFlattenedSections: async () => reference.sections,
getFunctionsList: async () => [],
getTypeSpec: async () => undefined,
}))
vi.mock('~/features/docs/Reference.mdx', () => ({
getRefMarkdown: async () => 'Reference content',
}))
vi.mock('next/navigation', () => ({
notFound: () => {
throw new Error('notFound')
},
}))
import { GET } from './route'
function section(slug: string, title: string, type = 'markdown') {
return { id: slug, slug, title, type }
}
async function html(path: string) {
const response = await GET(new Request(`https://supabase.com${path}`))
expect(response.status).toBe(200)
return response.text()
}
beforeEach(() => {
reference.sections = []
})
describe('crawler reference html', () => {
it('points agents at llms.txt before the navigation', async () => {
reference.sections = [section('introduction', 'Introduction')]
const page = await html('/reference/javascript/introduction')
expect(page).toContain(`href="${LLMS_TXT_URL}"`)
expect(page.indexOf(LLMS_TXT_URL)).toBeLessThan(page.indexOf('<nav>'))
})
})
describe('crawler reference aliases', () => {
it('resolves unique bare JavaScript modifier and filter slugs', async () => {
reference.sections = [
section('using-modifiers-order', 'Order'),
section('using-filters-or', 'Or'),
section('using-modifiers-maybesingle', 'Maybe single'),
]
for (const [requested, canonical] of [
['order', 'using-modifiers-order'],
['or', 'using-filters-or'],
['maybeSingle', 'using-modifiers-maybesingle'],
]) {
expect(await html(`/reference/javascript/${requested}`)).toContain(
`href="https://supabase.com/docs/reference/javascript/${canonical}"`
)
}
})
it('resolves storage slugs and keeps an exact versioned slug', async () => {
reference.sections = [
section('file-buckets-createbucket', 'Create bucket'),
section('file-buckets-upload', 'Upload'),
section('file-buckets-createsignedurl', 'Create signed URL'),
section('file-buckets-updatebucket', 'Update bucket'),
section('file-buckets-listv2', 'List files v2'),
]
for (const [requested, canonical] of [
['storage-createbucket', 'file-buckets-createbucket'],
['storage-from-upload', 'file-buckets-upload'],
['storage-from-createsignedurl', 'file-buckets-createsignedurl'],
['storage-updatebucket', 'file-buckets-updatebucket'],
['file-buckets-listv2', 'file-buckets-listv2'],
]) {
expect(await html(`/reference/javascript/${requested}`)).toContain(
`href="https://supabase.com/docs/reference/javascript/${canonical}"`
)
}
})
it('resolves SDK-specific user and root aliases', async () => {
reference.sections = [
section('introduction', 'Introduction'),
section('auth-admin', 'Admin'),
section('auth-getuser', 'Get user'),
section('auth-currentuser', 'Current user'),
]
expect(await html('/reference/javascript')).toContain(
'href="https://supabase.com/docs/reference/javascript/introduction"'
)
expect(await html('/reference/javascript/start')).toContain('JavaScript: Introduction')
expect(await html('/reference/javascript/admin-api')).toContain('JavaScript: Admin')
expect(await html('/reference/swift/get-user')).toContain(
'href="https://supabase.com/docs/reference/swift/auth-getuser"'
)
expect(await html('/reference/dart/get-user')).toContain(
'href="https://supabase.com/docs/reference/dart/auth-currentuser"'
)
expect(await html('/reference/kotlin')).toContain('Kotlin: Introduction')
})
it('keeps exact matches ahead of aliases and rejects ambiguous bare slugs', async () => {
reference.sections = [
section('delete', 'Delete'),
section('auth-passkey-list', 'Passkeys'),
section('file-buckets-list', 'Files'),
]
expect(await html('/reference/javascript/delete')).toContain('JavaScript: Delete')
expect(await html('/reference/javascript/storage-list')).toContain('JavaScript: Files')
await expect(html('/reference/javascript/list')).rejects.toThrow('notFound')
})
it('uses canonical paths for explicit versions', async () => {
reference.sections = [section('auth-update', 'Update')]
expect(await html('/reference/javascript/v1/auth-update')).toContain(
'href="https://supabase.com/docs/reference/javascript/v1/auth-update"'
)
reference.sections = [section('auth-updateuser', 'Update')]
expect(await html('/reference/javascript/v2/auth-updateuser')).toContain(
'href="https://supabase.com/docs/reference/javascript/auth-updateuser"'
)
})
})