mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
Closes DOCS-1520 ## Problem The original PR was only client-side. This is adding server-side integration. The docs-nav evals fetch each page's HTML with no JavaScript and add `?docs-ia-mvp-v1=option-1` to every request. The option-1 arm gets the control navigation anyway. Guide pages are statically generated, so the server can't read the query parameter. `useIaNavVariant` reads the override from the URL after the page loads, so the fetched HTML always has today's nav, hidden with an `invisible` class. The top nav dropdowns have a second gap. Their panels mount on the client, so their links are not in the server HTML for either variant. Every eval case starts at `/docs`, where those links are the only way into the Option 1 groups. ## Solution - **Rewrite option-1 requests:** `proxy.ts` sends `/docs` and guide pages with `?docs-ia-mvp-v1=option-1` to `/docs/ia/option-1/…`. Pages there render on the first request and are then cached, so nothing is added to the build. Requests without the parameter, and `.md` requests, are not rewritten. - **Read the variant from the route:** `IaNavRouteProvider`, in the root layout, reads the variant from the layout segments. `useIaNavVariant` returns it on the server and records no PostHog exposure for it. `useIaNavReady` is ready at once, so the nav has no `invisible` class. - **Strip the route prefix:** on the server, `usePathname` returns the rewritten path. The nav components, breadcrumbs, and `AiTools` use `useDocsPathname`, which strips the `/ia/option-1` prefix. - **Breadcrumbs follow the variant:** they resolve from the visitor's nav config, not always today's. - **Top nav subheads in the HTML:** a `<noscript>` block next to the top nav lists the dropdown links for the visitor's variant, inside an `sr-only` nav. Browsers with JavaScript ignore it. - **No index for the route:** direct visits to `/docs/ia/option-1/…` get `noindex`. Canonical URLs point to the real guide. **Not changed:** - **Reference pages:** they keep today's nav, even with the parameter. - **Config pages:** the five generated config pages under `self-hosting` and `local-development/cli` are not in the markdown manifest, so they keep today's nav too. - **Links:** they don't carry the query parameter, so a click in the browser leaves the forced variant. This was already the case before this PR. - **Conflicts with #52679:** it changes the same hooks. Whichever PR merges second needs a rebase. ## Review instructions `grep -c` counts the lines of server HTML that contain the text. A count of 1 or more means the text is in the HTML that an agent fetches without JavaScript. A count of 0 means it is not. 1. Run `curl -s "https://docs-git-docs-ia-option-1-server-render-supabase.vercel.app/docs/guides/auth/sessions?docs-ia-mvp-v1=option-1" | grep -c "Run and manage"`. The count is 1 or more. "Run and manage" is an Option 1 top nav label, so this checks that the parameter gets Option 1's nav from the server. 2. Run `curl -s "https://docs-git-docs-ia-option-1-server-render-supabase.vercel.app/docs/guides/auth/sessions" | grep -c "Run and manage"`. The count is 0. This checks that a request without the parameter still gets today's nav. 3. Run `curl -s "https://docs-git-docs-ia-option-1-server-render-supabase.vercel.app/docs?docs-ia-mvp-v1=option-1" | grep -c "guides/platform/ssl-enforcement"`. The count is 1 or more. The home page has no sidebar, and this link sits only in the "Run and manage" dropdown, so this checks that the top nav subhead links are in the server HTML. 4. Run `curl -s -o /dev/null -w "%{http_code}" "https://docs-git-docs-ia-option-1-server-render-supabase.vercel.app/docs/guides/auth.md?docs-ia-mvp-v1=option-1"`. It returns `200`. This checks that the markdown version still loads with the parameter. 5. Open [/docs/guides/auth](https://docs-git-docs-ia-option-1-server-render-supabase.vercel.app/docs/guides/auth) in a browser and hover over the top nav. You see today's nav and dropdowns. ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [ ] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
251 lines
9.9 KiB
TypeScript
251 lines
9.9 KiB
TypeScript
import { NextRequest } from 'next/server'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import { proxy } from './proxy'
|
|
|
|
// The real manifest is generated at build time by a content script; mocking
|
|
// it here decouples this test from that build step.
|
|
vi.mock('~/public/markdown/manifest.json', () => ({
|
|
default: ['auth'],
|
|
}))
|
|
|
|
// BASE_PATH defaults to '/docs' when NEXT_PUBLIC_BASE_PATH is unset, so test
|
|
// paths include the /docs prefix to match the proxy's GUIDES_PATH check.
|
|
function makeRequest(
|
|
path: string,
|
|
{ accept, userAgent }: { accept?: string; userAgent?: string } = {}
|
|
): NextRequest {
|
|
const headers: Record<string, string> = {}
|
|
if (accept) headers.accept = accept
|
|
if (userAgent) headers['user-agent'] = userAgent
|
|
return new NextRequest(new URL(path, 'https://supabase.com'), { headers })
|
|
}
|
|
|
|
const REWRITE_HEADER = 'x-middleware-rewrite'
|
|
const GUIDES_MD_REWRITE = (slug: string) => `https://supabase.com/docs/api/guides-md/${slug}`
|
|
|
|
describe('docs proxy — /guides/* content negotiation', () => {
|
|
it('rewrites to /api/guides-md/<slug> when Accept includes text/markdown', () => {
|
|
const req = makeRequest('/docs/guides/auth', { accept: 'text/markdown' })
|
|
expect(proxy(req).headers.get(REWRITE_HEADER)).toBe(GUIDES_MD_REWRITE('auth'))
|
|
})
|
|
|
|
it('rewrites /<slug>.md to /api/guides-md/<slug> regardless of Accept', () => {
|
|
for (const accept of [undefined, 'text/html']) {
|
|
const req = makeRequest('/docs/guides/auth.md', accept ? { accept } : {})
|
|
expect(proxy(req).headers.get(REWRITE_HEADER)).toBe(GUIDES_MD_REWRITE('auth'))
|
|
}
|
|
})
|
|
|
|
it('serves the markdown 404 for an unknown /<slug>.md, but not for an Accept-only miss', async () => {
|
|
const md = proxy(makeRequest('/docs/guides/doesnotexist.md'))
|
|
expect(md.status).toBe(404)
|
|
expect(md.headers.get('content-type')).toBe('text/markdown; charset=utf-8')
|
|
expect(md.headers.get('cache-control')).toBe('no-store')
|
|
const body = await md.text()
|
|
expect(body).toContain('<https://supabase.com/docs/guides/doesnotexist>')
|
|
expect(body).toContain('<https://supabase.com/llms.txt>')
|
|
const accept = makeRequest('/docs/guides/doesnotexist', { accept: 'text/markdown' })
|
|
expect(proxy(accept).headers.get(REWRITE_HEADER)).toBeNull()
|
|
})
|
|
|
|
it('serves HTML for browser-style Accept', () => {
|
|
const req = makeRequest('/docs/guides/auth', {
|
|
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
|
})
|
|
expect(proxy(req).headers.get(REWRITE_HEADER)).toBeNull()
|
|
})
|
|
|
|
it('serves markdown when md q-value beats html', () => {
|
|
const req = makeRequest('/docs/guides/auth', {
|
|
accept: 'text/html;q=0.5, text/markdown;q=1.0',
|
|
})
|
|
expect(proxy(req).headers.get(REWRITE_HEADER)).toBe(GUIDES_MD_REWRITE('auth'))
|
|
})
|
|
|
|
it('falls through to HTML when no Accept header is sent', () => {
|
|
expect(proxy(makeRequest('/docs/guides/auth')).headers.get(REWRITE_HEADER)).toBeNull()
|
|
})
|
|
|
|
it('falls through to HTML for bare Accept: */* (no explicit md preference)', () => {
|
|
const req = makeRequest('/docs/guides/auth', { accept: '*/*' })
|
|
expect(proxy(req).headers.get(REWRITE_HEADER)).toBeNull()
|
|
})
|
|
|
|
it('does not serve markdown when client explicitly rejects it (q=0)', () => {
|
|
const req = makeRequest('/docs/guides/auth', {
|
|
accept: 'text/markdown;q=0, text/html;q=1.0',
|
|
})
|
|
expect(proxy(req).headers.get(REWRITE_HEADER)).toBeNull()
|
|
})
|
|
|
|
it('tolerates OWS in q-params and clamps out-of-range q-values', () => {
|
|
// OWS around q: html wins.
|
|
const ows = makeRequest('/docs/guides/auth', {
|
|
accept: 'text/html ; q = 1.0, text/markdown ; q = 0.5',
|
|
})
|
|
expect(proxy(ows).headers.get(REWRITE_HEADER)).toBeNull()
|
|
// q=2.0 is out-of-range, falls back to default 1.0; tie -> markdown.
|
|
const oor = makeRequest('/docs/guides/auth', {
|
|
accept: 'text/html;q=2.0, text/markdown;q=1.0',
|
|
})
|
|
expect(proxy(oor).headers.get(REWRITE_HEADER)).toBe(GUIDES_MD_REWRITE('auth'))
|
|
})
|
|
|
|
it('returns 406 with Cache-Control: no-store and Vary: Accept when Accept excludes every type', () => {
|
|
const req = makeRequest('/docs/guides/auth', {
|
|
accept: 'application/x-content-negotiation-probe',
|
|
})
|
|
const res = proxy(req)
|
|
expect(res.status).toBe(406)
|
|
expect(res.headers.get('Cache-Control')).toBe('no-store')
|
|
expect(res.headers.get('Vary')).toBe('Accept')
|
|
})
|
|
|
|
it('does not 406 for .md suffix paths even with a probe Accept', () => {
|
|
const md = makeRequest('/docs/guides/auth.md', {
|
|
accept: 'application/x-content-negotiation-probe',
|
|
})
|
|
expect(proxy(md).status).not.toBe(406)
|
|
expect(proxy(md).headers.get(REWRITE_HEADER)).toBe(GUIDES_MD_REWRITE('auth'))
|
|
})
|
|
|
|
it('returns 406 for a probe Accept header regardless of user agent', () => {
|
|
const req = makeRequest('/docs/guides/auth', {
|
|
accept: 'application/x-content-negotiation-probe',
|
|
userAgent: 'Claude-User/1.0',
|
|
})
|
|
expect(proxy(req).status).toBe(406)
|
|
})
|
|
|
|
it('does not 406 on /reference/* (negotiation contract is /guides/* only)', () => {
|
|
const req = makeRequest('/docs/reference/javascript/introduction', {
|
|
accept: 'application/x-content-negotiation-probe',
|
|
})
|
|
expect(proxy(req).status).not.toBe(406)
|
|
})
|
|
|
|
it('does not rewrite for agent user agents without a markdown Accept preference', () => {
|
|
for (const ua of [
|
|
'Claude-User (claude-code/2.1.119; +https://support.anthropic.com/)',
|
|
'Claude-Web/1.0',
|
|
'PerplexityBot/1.0',
|
|
]) {
|
|
const req = makeRequest('/docs/guides/auth', { userAgent: ua })
|
|
expect(proxy(req).headers.get(REWRITE_HEADER)).toBeNull()
|
|
}
|
|
})
|
|
|
|
it('still serves ChatGPT-User markdown when Accept asks for it', () => {
|
|
const req = makeRequest('/docs/guides/auth', {
|
|
accept: 'text/markdown',
|
|
userAgent: 'Mozilla/5.0 (compatible; ChatGPT-User/1.0)',
|
|
})
|
|
expect(proxy(req).headers.get(REWRITE_HEADER)).toBe(GUIDES_MD_REWRITE('auth'))
|
|
})
|
|
|
|
it('serves HTML when Accept prefers HTML, regardless of user agent', () => {
|
|
const req = makeRequest('/docs/guides/auth', {
|
|
accept: 'text/html;q=1.0, text/markdown;q=0.1',
|
|
userAgent: 'Claude-User/1.0',
|
|
})
|
|
expect(proxy(req).headers.get(REWRITE_HEADER)).toBeNull()
|
|
})
|
|
|
|
it('falls through for non-LLM UAs (browsers, training crawlers, excluded agents, substring embeds)', () => {
|
|
for (const ua of [
|
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
|
|
'GPTBot/1.0',
|
|
'ClaudeBot/1.0',
|
|
'CCBot/2.0',
|
|
'Mozilla/5.0 (compatible; ChatGPT-User/1.0)',
|
|
'chatgpt-userscript/2.0',
|
|
'NotPerplexityBot',
|
|
]) {
|
|
const req = makeRequest('/docs/guides/auth', { userAgent: ua })
|
|
expect(proxy(req).headers.get(REWRITE_HEADER)).toBeNull()
|
|
}
|
|
})
|
|
|
|
it('does not apply LLM UA rewrite to /reference/* (guides-only)', () => {
|
|
const req = makeRequest('/docs/reference/javascript/introduction', {
|
|
userAgent: 'Claude-User/1.0',
|
|
})
|
|
const rewrite = proxy(req).headers.get(REWRITE_HEADER) ?? ''
|
|
expect(rewrite).not.toContain('/api/guides-md/')
|
|
})
|
|
})
|
|
|
|
describe('docs proxy — reference paths', () => {
|
|
it('does not mistake listv2 for a version segment', () => {
|
|
const path = '/docs/reference/javascript/file-buckets-listv2'
|
|
expect(proxy(makeRequest(path, { userAgent: 'Googlebot' })).headers.get(REWRITE_HEADER)).toBe(
|
|
'https://supabase.com/docs/api/crawlers'
|
|
)
|
|
expect(proxy(makeRequest(path)).headers.get(REWRITE_HEADER)).toBe(
|
|
'https://supabase.com/docs/reference/javascript'
|
|
)
|
|
})
|
|
|
|
it('serves the markdown 404 for .md guesses for every user agent, before the crawler rewrite', async () => {
|
|
for (const userAgent of [
|
|
'curl/8.7.1',
|
|
'Googlebot',
|
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
|
|
]) {
|
|
const res = proxy(makeRequest('/docs/reference/javascript/select.md', { userAgent }))
|
|
expect(res.status).toBe(404)
|
|
expect(res.headers.get(REWRITE_HEADER)).toBeNull()
|
|
const body = await res.text()
|
|
expect(body).toContain('<https://supabase.com/docs/reference/javascript/select>')
|
|
expect(body).toContain('<https://supabase.com/llms/js.txt>')
|
|
}
|
|
})
|
|
|
|
it('links the full markdown reference for cli and api .md guesses', async () => {
|
|
const cli = await proxy(makeRequest('/docs/reference/cli/supabase-init.md')).text()
|
|
expect(cli).toContain('<https://supabase.com/llms/cli.txt>')
|
|
const api = await proxy(makeRequest('/docs/reference/api/v1-list-all-projects.md')).text()
|
|
expect(api).toContain('<https://supabase.com/llms/api.txt>')
|
|
})
|
|
|
|
it('sends the Kotlin root to the crawler renderer for bots', () => {
|
|
expect(
|
|
proxy(makeRequest('/docs/reference/kotlin', { userAgent: 'Googlebot' })).headers.get(
|
|
REWRITE_HEADER
|
|
)
|
|
).toBe('https://supabase.com/docs/api/crawlers')
|
|
})
|
|
})
|
|
|
|
describe('docs proxy — forced IA nav variant', () => {
|
|
const OPTION_1 = '?docs-ia-mvp-v1=option-1'
|
|
|
|
it('renders option-1 guides and the home page from the option-1 route', () => {
|
|
expect(proxy(makeRequest(`/docs/guides/auth${OPTION_1}`)).headers.get(REWRITE_HEADER)).toBe(
|
|
`https://supabase.com/docs/ia/option-1/guides/auth${OPTION_1}`
|
|
)
|
|
expect(proxy(makeRequest(`/docs${OPTION_1}`)).headers.get(REWRITE_HEADER)).toBe(
|
|
`https://supabase.com/docs/ia/option-1${OPTION_1}`
|
|
)
|
|
})
|
|
|
|
it('still serves markdown for an option-1 .md request', () => {
|
|
expect(proxy(makeRequest(`/docs/guides/auth.md${OPTION_1}`)).headers.get(REWRITE_HEADER)).toBe(
|
|
`${GUIDES_MD_REWRITE('auth')}${OPTION_1}`
|
|
)
|
|
})
|
|
|
|
it("leaves requests without option-1 on today's route", () => {
|
|
for (const path of [
|
|
'/docs/guides/auth',
|
|
'/docs',
|
|
'/docs/guides/auth?docs-ia-mvp-v1=control',
|
|
'/docs/guides/auth?docs-ia-mvp-v1=nope',
|
|
`/docs/guides/doesnotexist${OPTION_1}`,
|
|
]) {
|
|
expect(proxy(makeRequest(path)).headers.get(REWRITE_HEADER)).toBeNull()
|
|
}
|
|
})
|
|
})
|