Files
supabase/apps/docs/components/Breadcrumbs.tsx
Charis fc164b5d07 Refactor/app router refs (#28095)
Migrates client SDK References to App Router. (Management and CLI API references aren't migrated yet, nor are self-hosting config references.)

Some notes:

Big changes to the way crawler pages are built and individual section URLs (e.g., javascript/select) are served. All of these used to be SSG-generated pages, but the number of heavy pages was just too much to handle -- slow as molasses and my laptop sounded like it was taking off, and CI sometimes refuses to build it all at all.

Tried various tricks with caching and pre-generating data but no dice.

So I changed to only building one copy of each SDK+version page, then serving the sub-URLs through a response rewrite. That's for the actual user-visible pages.

For the bot pages, each sub-URL needs to be its own page, but prebuilding it doesn't work, and rendering on demand from React components is too slow (looking for super-fast response here for SEO). Instead I changed to using an API route that serves very minimal, hand-crafted HTML. It looks ugly, but it's purely for the search bots.

You can test what bots see by running curl --user-agent "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" <URL_OF_PAGE>

Also added some smoke tests to run against prod for the crawler routes, since we don't keep an eye on those regularly, and Vercel config changes could surprise-break them. Tested the meta images on Open Graph and all seems to work fine.

With this approach, full production builds are really fast: ~5 minutes

Starts using the new type spec handling, which is better at finding params automatically, so I could remove some of the manually written ones from the spec files.
2024-08-13 16:12:59 -04:00

162 lines
5.5 KiB
TypeScript

'use client'
import Link from 'next/link'
import { usePathname } from 'next/navigation'
import React, { Fragment } from 'react'
import { useBreakpoint } from 'common'
import {
Breadcrumb_Shadcn_ as Breadcrumb,
BreadcrumbList_Shadcn_ as BreadcrumbList,
BreadcrumbItem_Shadcn_ as BreadcrumbItem,
BreadcrumbLink_Shadcn_ as BreadcrumbLink,
BreadcrumbSeparator_Shadcn_ as BreadcrumbSeparator,
BreadcrumbPage_Shadcn_ as BreadcrumbPage,
BreadcrumbEllipsis_Shadcn_ as BreadcrumbEllipsis,
Button,
cn,
Drawer,
DrawerClose,
DrawerContent,
DrawerFooter,
DrawerTrigger,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from 'ui'
import * as NavItems from '~/components/Navigation/NavigationMenu/NavigationMenu.constants'
import { getMenuId } from '~/components/Navigation/NavigationMenu/NavigationMenu.utils'
const Breadcrumbs = ({ className }: { className?: string }) => {
const pathname = usePathname()
const menuId = getMenuId(pathname)
const menu = NavItems[menuId]
const breadcrumbs = findMenuItemByUrl(menu, pathname, [])
const [open, setOpen] = React.useState(false)
const isMobile = useBreakpoint('md')
const ITEMS_TO_DISPLAY = isMobile ? 4 : 3
if (!breadcrumbs?.length || breadcrumbs?.length === 1) return null
const appendedBreadcrumbs = breadcrumbs?.slice(-ITEMS_TO_DISPLAY + 1, isMobile ? -1 : undefined)
return (
<Breadcrumb className={cn(className)}>
<BreadcrumbList className="text-foreground-lighter p-0">
{breadcrumbs.length >= ITEMS_TO_DISPLAY && (
<>
<BreadcrumbItem>
{breadcrumbs[0].url ? (
<BreadcrumbLink href={`/docs${breadcrumbs[0].url}`}>
{breadcrumbs[0].title || breadcrumbs[0].name}
</BreadcrumbLink>
) : (
<BreadcrumbPage>{breadcrumbs[0].title || breadcrumbs[0].name}</BreadcrumbPage>
)}
</BreadcrumbItem>
<BreadcrumbSeparator />
</>
)}
{breadcrumbs.length > ITEMS_TO_DISPLAY && (
<>
<BreadcrumbItem>
{!isMobile ? (
<DropdownMenu open={open} onOpenChange={setOpen}>
<DropdownMenuTrigger className="flex items-center gap-1" aria-label="Toggle menu">
<BreadcrumbEllipsis className="h-4 w-4" />
</DropdownMenuTrigger>
<DropdownMenuContent align="start">
{breadcrumbs.slice(1, -2).map((crumb, index) => (
<DropdownMenuItem
key={index}
className={cn(!crumb.url && 'pointer-events-none')}
>
{crumb.url ? (
<Link href={`/docs${crumb.url}`}>{crumb.title || crumb.name}</Link>
) : (
crumb.title || crumb.name
)}
</DropdownMenuItem>
))}
</DropdownMenuContent>
</DropdownMenu>
) : (
<Drawer open={open} onOpenChange={setOpen}>
<DrawerTrigger aria-label="Toggle Menu">
<BreadcrumbEllipsis className="h-4 w-4" />
</DrawerTrigger>
<DrawerContent showHandle={false}>
<div className="grid gap-1 px-4">
{breadcrumbs
.slice(1, -2)
.map((crumb) =>
crumb.url ? (
<Link href={`/docs${crumb.url}`}>{crumb.title || crumb.name}</Link>
) : (
crumb.title || crumb.name
)
)}
</div>
<DrawerFooter className="pt-4">
<DrawerClose asChild>
<Button type="outline">Close</Button>
</DrawerClose>
</DrawerFooter>
</DrawerContent>
</Drawer>
)}
</BreadcrumbItem>
<BreadcrumbSeparator />
</>
)}
{appendedBreadcrumbs?.map((crumb, i) => (
<Fragment key={crumb.url}>
<BreadcrumbItem
className={cn(
'flex items-center overflow-hidden',
i === appendedBreadcrumbs.length - 1 && 'md:text-foreground-light'
)}
>
{crumb.url ? (
<BreadcrumbLink href={`/docs${crumb.url}`}>
{crumb.title || crumb.name}
</BreadcrumbLink>
) : (
<BreadcrumbPage>{crumb.title || crumb.name}</BreadcrumbPage>
)}
</BreadcrumbItem>
<BreadcrumbSeparator
className={cn(i === appendedBreadcrumbs.length - 1 && 'md:hidden')}
/>
</Fragment>
))}
</BreadcrumbList>
</Breadcrumb>
)
}
function findMenuItemByUrl(menu, targetUrl, parents = []) {
// If the menu has items, recursively search through them
if (menu.items) {
for (let item of menu.items) {
const result = findMenuItemByUrl(item, targetUrl, [...parents, menu])
if (result) {
return result
}
}
}
// Check if the current menu object itself has the target URL
if (menu.url === targetUrl) {
return [...parents, menu]
}
// If the URL is not found, return null
return null
}
export default Breadcrumbs