Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(docs): reduce reference page response size (#50235)

Jordi Enricf3bd8ee7e384
diff --git a/apps/docs/features/ui/CodeBlock/CodeBlock.client.tsx b/apps/docs/features/ui/CodeBlock/CodeBlock.client.tsx
index be8c083119e..f51bda0be2e 100644
--- a/apps/docs/features/ui/CodeBlock/CodeBlock.client.tsx
+++ b/apps/docs/features/ui/CodeBlock/CodeBlock.client.tsx
@@ -6,12 +6,82 @@ import { type ThemedToken } from 'shiki'
import { type NodeHover } from 'twoslash'
import { Button, cn, copyToClipboard, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
+import { getFontStyle } from './CodeBlock.utils'
+
+type CodeAnnotation = Pick<NodeHover, 'text' | 'docs' | 'tags'>
+export type CodeToken = [
+ content: string,
+ color: ThemedToken['color'],
+ fontStyle: number,
+ annotation?: {
+ annotations: Array<CodeAnnotation>
+ htmlStyle: ThemedToken['htmlStyle']
+ },
+]
+
+export function CodeBlockTokens({
+ lines,
+ lineNumbers,
+}: {
+ lines: Array<Array<CodeToken>>
+ lineNumbers: boolean
+}) {
+ return (
+ <pre>
+ <code
+ className={cn(
+ lineNumbers && 'grid grid-cols-[auto_1fr] w-fit min-w-full py-3',
+ '[--row-rest:var(--background-200)]',
+ '[--row-hover:color-mix(in_srgb,var(--foreground)_3%,var(--background-200))]'
+ )}
+ >
+ {lineNumbers ? (
+ lines.map((line, idx) => (
+ <div key={idx} className="group/row contents">
+ <div aria-hidden="true" className="code-line-number">
+ {idx + 1}
+ </div>
+ <div className="code-content code-line-content">
+ <CodeLine tokens={line} />
+ </div>
+ </div>
+ ))
+ ) : (
+ <div className="code-content p-6">
+ {lines.map((line, idx) => (
+ <CodeLine key={idx} tokens={line} />
+ ))}
+ </div>
+ )}
+ </code>
+ </pre>
+ )
+}
+
+function CodeLine({ tokens }: { tokens: Array<CodeToken> }) {
+ return (
+ <span className="block min-h-5 leading-5">
+ {tokens.map(([content, color, fontStyle, annotation], idx) =>
+ annotation ? (
+ <AnnotatedSpan key={idx} content={content} {...annotation} />
+ ) : (
+ <span key={idx} style={{ color, ...getFontStyle(fontStyle) }}>
+ {content}
+ </span>
+ )
+ )}
+ </span>
+ )
+}
+
export function AnnotatedSpan({
- token,
+ content,
+ htmlStyle,
annotations,
}: {
- token: ThemedToken
- annotations: Array<NodeHover>
+ content: string
+ htmlStyle: ThemedToken['htmlStyle']
+ annotations: Array<CodeAnnotation>
}) {
const [open, setOpen] = useState(false)
@@ -45,13 +115,13 @@ export function AnnotatedSpan({
<TooltipTrigger asChild onClick={handleClick}>
<button
tabIndex={0}
- style={token.htmlStyle}
+ style={htmlStyle}
className={cn(
isTouchDevice &&
'underline underline-offset-4 decoration-dashed decoration-[rgba(from_currentColor_r_g_b/0.5)]'
)}
>
- {token.content}
+ {content}
</button>
</TooltipTrigger>
<TooltipContent className="max-w-[min(80vw,400px)] p-0 divide-y">
@@ -63,7 +133,7 @@ export function AnnotatedSpan({
)
}
-function Annotation({ annotation }: { annotation: NodeHover }) {
+function Annotation({ annotation }: { annotation: CodeAnnotation }) {
const { text, docs, tags } = annotation
return (
<div className="flex flex-col gap-2">
diff --git a/apps/docs/features/ui/CodeBlock/CodeBlock.tsx b/apps/docs/features/ui/CodeBlock/CodeBlock.tsx
index 1d1c4d3268d..84769bebe25 100644
--- a/apps/docs/features/ui/CodeBlock/CodeBlock.tsx
+++ b/apps/docs/features/ui/CodeBlock/CodeBlock.tsx
@@ -1,10 +1,10 @@
import { type PropsWithChildren } from 'react'
-import { bundledLanguages, createHighlighter, type BundledLanguage, type ThemedToken } from 'shiki'
+import { bundledLanguages, createHighlighter, type BundledLanguage } from 'shiki'
import { createTwoslasher, type ExtraFiles, type NodeHover } from 'twoslash'
import { cn } from 'ui'
-import { AnnotatedSpan, CodeBlockControls } from './CodeBlock.client'
-import { getCodeBlockLabel, getFontStyle } from './CodeBlock.utils'
+import { CodeBlockControls, CodeBlockTokens, type CodeToken } from './CodeBlock.client'
+import { getCodeBlockLabel } from './CodeBlock.utils'
import theme from './supabase-2.json' with { type: 'json' }
import denoTypes from './types/lib.deno.d.ts.include'
@@ -83,50 +83,22 @@ export async function CodeBlock({
aria-label={getCodeBlockLabel(lang, tokens.length)}
tabIndex={0}
>
- <pre>
- <code
- className={cn(
- lineNumbers && 'grid grid-cols-[auto_1fr] w-fit min-w-full py-3',
- '[--row-rest:var(--background-200)]',
- '[--row-hover:color-mix(in_srgb,var(--foreground)_3%,var(--background-200))]'
- )}
- >
- {lineNumbers ? (
- tokens.map((line, idx) => (
- <div key={idx} className="group/row contents">
- <div
- aria-hidden="true"
- className={cn(
- 'select-none text-right text-muted/70 pl-3 pr-2 min-h-5 leading-5',
- 'bg-[var(--row-rest)]',
- 'sticky left-0 z-10',
- 'group-hover/row:text-muted group-hover/row:bg-[var(--row-hover)]',
- 'after:pointer-events-none after:absolute after:inset-y-0 after:left-full after:w-4',
- '[--gutter-fade:var(--row-rest)] group-hover/row:[--gutter-fade:var(--row-hover)]',
- 'after:bg-[image:linear-gradient(to_right,var(--gutter-fade)_0%,color-mix(in_srgb,var(--gutter-fade)_85%,transparent)_25%,color-mix(in_srgb,var(--gutter-fade)_50%,transparent)_50%,color-mix(in_srgb,var(--gutter-fade)_15%,transparent)_75%,transparent_100%)]'
- )}
- >
- {idx + 1}
- </div>
- <div
- className={cn(
- 'code-content min-h-5 leading-5 pl-3 pr-18',
- 'group-hover/row:bg-[var(--row-hover)]'
- )}
- >
- <CodeLine tokens={line} twoslash={twoslashed?.get(idx)} />
- </div>
- </div>
- ))
- ) : (
- <div className="code-content p-6">
- {tokens.map((line, idx) => (
- <CodeLine key={idx} tokens={line} twoslash={twoslashed?.get(idx)} />
- ))}
- </div>
- )}
- </code>
- </pre>
+ <CodeBlockTokens
+ lineNumbers={lineNumbers}
+ lines={tokens.map((line, lineIndex) => {
+ let offset = 0
+ return line.map(({ content, color, fontStyle, htmlStyle }): CodeToken => {
+ const annotations = twoslashed
+ ?.get(lineIndex)
+ ?.get(offset)
+ ?.map(({ text, docs, tags }) => ({ text, docs, tags }))
+ offset += content.length
+ return annotations
+ ? [content, color, fontStyle || 0, { annotations, htmlStyle }]
+ : [content, color, fontStyle || 0]
+ })
+ })}
+ />
</div>
{/* After the code so the block is named before its controls, and outside the scroller so they stay pinned */}
{!hideControls && <CodeBlockControls content={code.trim()} />}
@@ -134,42 +106,6 @@ export async function CodeBlock({
)
}
-function CodeLine({
- tokens: rawTokens,
- twoslash,
-}: {
- tokens: Array<ThemedToken>
- twoslash?: Map<number, Array<NodeHover>>
-}) {
- let offset = 0
- const tokens = rawTokens.map((token) => {
- const newToken = { ...token, offset }
- offset += token.content.length
- return newToken
- })
-
- return (
- <span className="block min-h-5 leading-5">
- {tokens.map((token) =>
- twoslash?.has(token.offset) ? (
- <AnnotatedSpan
- key={token.offset}
- token={token}
- annotations={twoslash.get(token.offset)!}
- />
- ) : (
- <span
- key={token.offset}
- style={{ color: token.color, ...getFontStyle(token.fontStyle || 0) }}
- >
- {token.content}
- </span>
- )
- )}
- </span>
- )
-}
-
function extractCode(children: React.ReactNode): string {
if (typeof children === 'string') return children
const child = Array.isArray(children) ? children[0] : children
diff --git a/apps/docs/styles/globals.css b/apps/docs/styles/globals.css
index 3cc96c49c6c..6b15dd3e07a 100644
--- a/apps/docs/styles/globals.css
+++ b/apps/docs/styles/globals.css
@@ -34,6 +34,19 @@
--font-weight-normal: 450;
}
+@utility code-line-number {
+ @apply select-none text-right text-muted/70 pl-3 pr-2 min-h-5 leading-5;
+ @apply bg-[var(--row-rest)] sticky left-0 z-10;
+ @apply group-hover/row:text-muted group-hover/row:bg-[var(--row-hover)];
+ @apply after:pointer-events-none after:absolute after:inset-y-0 after:left-full after:w-4;
+ @apply [--gutter-fade:var(--row-rest)] group-hover/row:[--gutter-fade:var(--row-hover)];
+ @apply after:bg-[image:linear-gradient(to_right,var(--gutter-fade)_0%,color-mix(in_srgb,var(--gutter-fade)_85%,transparent)_25%,color-mix(in_srgb,var(--gutter-fade)_50%,transparent)_50%,color-mix(in_srgb,var(--gutter-fade)_15%,transparent)_75%,transparent_100%)];
+}
+
+@utility code-line-content {
+ @apply min-h-5 leading-5 pl-3 pr-18 group-hover/row:bg-[var(--row-hover)];
+}
+
@layer utilities {
.prose--remove-p-margin p {
margin: 0;