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)
149 lines
5.0 KiB
TypeScript
149 lines
5.0 KiB
TypeScript
import { clientSdkIds } from '~/content/navigation.references'
|
|
import { IA_NAV_FLAG, iaNavRoutePrefix, isIaNavVariant } from '~/features/IaNav/constants'
|
|
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',
|
|
},
|
|
})
|
|
}
|
|
|
|
function forcedVariantRewrite(url: URL, path: string) {
|
|
const variant = url.searchParams.get(IA_NAV_FLAG)
|
|
if (!isIaNavVariant(variant) || variant === 'control') return null
|
|
|
|
const rewriteUrl = new URL(url)
|
|
rewriteUrl.pathname = `${BASE_PATH ?? ''}${iaNavRoutePrefix(variant)}${path}`
|
|
return NextResponse.rewrite(rewriteUrl)
|
|
}
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const url = new URL(request.url)
|
|
const { pathname } = url
|
|
|
|
if (pathname === (BASE_PATH || '/')) {
|
|
return forcedVariantRewrite(url, '') ?? NextResponse.next()
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
const variantRewrite =
|
|
!isMdSuffix && GUIDES_MARKDOWN_SLUGS.has(slug)
|
|
? forcedVariantRewrite(url, `/guides/${slug}`)
|
|
: null
|
|
if (variantRewrite) return variantRewrite
|
|
}
|
|
|
|
// 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*'],
|
|
}
|