Files
supabase/apps/docs/components/Navigation/NavigationMenu/NavigationMenuGuideListItems.tsx
T
Miranda Limonczenko f59c795eb5 feat(docs): render Option 1 on the server when the variant is forced (#52735)
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)
2026-11-06 19:46:37 +00:00

289 lines
9.1 KiB
TypeScript

import {
NavSectionCaret,
NavSectionContent,
NavSectionList,
} from '~/components/Navigation/NavSection'
import { useDocsPathname } from '~/features/IaNav'
import { useTheme } from 'next-themes'
import Image from 'next/legacy/image'
import Link from 'next/link'
import { Accordion } from 'radix-ui'
import React, { useEffect, useRef } from 'react'
import MenuIconPicker from './MenuIconPicker'
type NavAccordionItem = {
name?: string
url?: string
enabled?: boolean
items?: NavAccordionItem[]
}
function hasActiveDescendant(item: NavAccordionItem, pathname: string): boolean {
if (item.enabled === false) return false
if (item.url === pathname) return true
return item.items?.some((child) => hasActiveDescendant(child, pathname)) ?? false
}
function isRenderable(item: NavAccordionItem): boolean {
if (item.enabled === false) return false
if (item.url) return true
return item.items?.some(isRenderable) ?? false
}
function groupLinks(item: NavAccordionItem): Array<{ name: string; url: string }> {
return (item.items ?? [])
.filter((child) => isRenderable(child))
.flatMap((child) => [
...(child.url && child.name ? [{ name: child.name, url: child.url }] : []),
...groupLinks(child),
])
}
const HeaderLink = React.memo(function HeaderLink(props: {
title: string
id: string
url: string
}) {
const pathname = useDocsPathname()
return (
<span
className={[
' ',
!props.title && 'capitalize',
props.url === pathname ? 'text-primary' : 'hover:text-primary text-foreground',
].join(' ')}
>
{props.title ?? props.id}
</span>
)
})
const ContentAccordionLink = React.memo(function ContentAccordionLink(props: any) {
const pathname = useDocsPathname()
const { resolvedTheme } = useTheme()
const childItems = props.subItem.items ?? []
const activeItem =
props.subItem.url === pathname &&
!childItems.some((child: NavAccordionItem) => child.url === pathname)
const activeItemRef = useRef<HTMLLIElement>(null)
const enabledChildren = childItems.filter(isRenderable)
const hasChildren = enabledChildren.length > 0
const isChildActive = enabledChildren.some((child: NavAccordionItem) =>
hasActiveDescendant(child, pathname)
)
const accordionValue = props.subItem.url || props.subItem.name
const LinkContainer = (props) => {
const isExternal = props.url.startsWith('https://')
return (
<Link
href={props.url}
className={props.className}
target={isExternal ? '_blank' : undefined}
rel={isExternal ? 'noopener noreferrer' : undefined}
>
{props.children}
</Link>
)
}
useEffect(() => {
// scroll to active item
if (activeItem && activeItemRef.current) {
// this is a hack, but seems a common one on Stackoverflow
setTimeout(() => {
activeItemRef.current?.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
}, 0)
}
})
if (!hasChildren && !props.subItem.url) return null
return (
<li ref={!hasChildren && activeItem ? activeItemRef : null}>
{hasChildren ? (
<Accordion.Root
collapsible
type="single"
className="space-y-0.5"
defaultValue={isChildActive ? accordionValue : undefined}
>
<Accordion.Item key={accordionValue} value={accordionValue}>
<Accordion.Trigger
className={[
'flex items-center gap-2 w-full',
'cursor-pointer transition text-sm',
'focus-inset rounded-md',
activeItem
? 'text-primary font-medium'
: 'hover:text-foreground text-foreground-lighter',
].join(' ')}
>
<span className="flex items-center justify-between w-full">
<div className="flex items-center gap-2">
{props.subItem.icon && (
<Image
alt={props.subItem.name}
src={`${props.subItem.icon}${!resolvedTheme?.includes('dark') ? '-light' : ''}.svg`}
width={15}
height={15}
/>
)}
{props.subItem.name}
</div>
<NavSectionCaret />
</span>
</Accordion.Trigger>
<Accordion.Content asChild>
<NavSectionContent>
<NavSectionList>
{enabledChildren.map((child) => {
if (child.items && child.items.length > 0) {
return <ContentAccordionLink key={child.name} subItem={child} />
}
return (
<li key={`${props.subItem.name}-${child.url}`}>
<Link
href={child.url}
className={[
'relative block py-1.25 pl-1 -ml-1 cursor-pointer transition text-sm',
'focus-inset rounded-md',
child.url === pathname
? 'text-primary'
: 'hover:text-primary text-foreground-lighter',
].join(' ')}
>
{child.url === pathname && (
<span
aria-hidden
className="absolute left-[-13px] top-1/2 h-[1em] w-px -translate-y-1/2 bg-current"
/>
)}
{child.name}
</Link>
</li>
)
})}
</NavSectionList>
</NavSectionContent>
</Accordion.Content>
<ul hidden>
{groupLinks(props.subItem).map((child) => (
<li key={`${child.name}-${child.url}`}>
<Link href={child.url}>{child.name}</Link>
</li>
))}
</ul>
</Accordion.Item>
</Accordion.Root>
) : (
<LinkContainer
url={props.subItem.url}
className={[
'flex items-center gap-2',
'cursor-pointer transition text-sm',
'focus-inset rounded-md',
activeItem
? 'text-primary font-medium'
: 'hover:text-foreground text-foreground-lighter',
].join(' ')}
parent={props.subItem.parent}
>
<div className="flex items-center gap-2">
{props.subItem.icon && (
<Image
alt={props.subItem.name}
src={`${props.subItem.icon}${!resolvedTheme?.includes('dark') ? '-light' : ''}.svg`}
width={15}
height={15}
/>
)}
{props.subItem.name}
</div>
</LinkContainer>
)}
</li>
)
})
const ContentLink = React.memo(function ContentLink(props: any) {
const pathname = useDocsPathname()
return (
<li className="mb-1.5">
<Link
href={props.url}
className={[
'cursor-pointer transition text-sm',
'focus-inset rounded-md',
props.url === pathname ? 'text-primary' : 'hover:text-foreground text-foreground-lighter',
].join(' ')}
>
{props.icon && (
<Image alt={props.icon} width={12} height={12} src={`${pathname}${props.icon}`} />
)}
{props.name}
</Link>
</li>
)
})
const Content = (props) => {
const { menu, id } = props
if (menu.enabled === false) {
return null
}
return (
<div className="relative w-full flex flex-col gap-0 pb-5">
<Link href={menu.url ?? ''} className="max-lg:hidden">
<div className="flex items-center gap-3 my-3 text-primary">
<MenuIconPicker icon={menu.icon} />
<HeaderLink title={menu.title} url={menu.url} id={id} />
</div>
</Link>
<ul data-testid="docs-guide-navigation-list" className="flex flex-col gap-0">
{menu.items.map((entry) => {
if (entry.enabled === false) return null
if (entry.items && entry.items.length > 0) {
const enabledItems = entry.items.filter(isRenderable)
if (enabledItems.length === 0) return null
return (
<li key={entry.name}>
<div className="flex flex-col gap-2.5">
<div className="h-px w-full bg-border my-3"></div>
<span className="font-mono text-xs uppercase text-foreground font-medium tracking-wider">
{entry.name}
</span>
<ul className="flex flex-col gap-2.5">
{enabledItems.map((subItem) => (
<ContentAccordionLink key={subItem.name} subItem={subItem} />
))}
</ul>
</div>
</li>
)
}
return entry.url ? (
<ContentLink url={entry.url} icon={entry.icon} name={entry.name} key={entry.name} />
) : null
})}
</ul>
</div>
)
}
export default React.memo(Content)