Skip to content

Commit 46c1352

Browse files
ericallamTrigger.dev RepoOps
authored andcommitted
feat(docs): flag-gated docs pages and sections
Mono-RevId: 5342b481595dfc6eeb13215d460f54820818ae37
1 parent b96c4b5 commit 46c1352

35 files changed

Lines changed: 2021 additions & 99 deletions

‎.env.example‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@
2424
# NEXT_PUBLIC_DASHBOARD_URL pointing here). Comma-separated docs origins that may read the
2525
# signed-in reader with credentials; unset means https://trigger.dev, empty allows none.
2626
# DOCS_ORIGINS=http://localhost:43000
27+
# Signs the grant that lets a reader see docs gated on their orgs' feature flags: an Ed25519
28+
# private key, base64 PKCS#8 DER. The docs site gets the pair's public key as DOCS_GRANT_PUBLIC_KEY
29+
# (apps/docs/README.md has the command for a pair). Unset: no gated docs.
30+
# DOCS_GRANT_PRIVATE_KEY=

‎apps/docs/README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ Built and deployed from GitHub Actions only (the host's Git integration is off i
7575
| `NEXT_PUBLIC_POSTHOG_HOST` | build | optional | Events go to PostHog's EU host. |
7676
| `NEXT_PUBLIC_DASHBOARD_URL` | build | optional | Sign-in links and personalization use `https://cloud.trigger.dev`, which is right for production. |
7777
| `NEXT_PUBLIC_DOCS_PERSONALIZATION` | build | required | Every reader sees the anonymous docs: no project in code samples, no docs keys. |
78+
| `DOCS_GRANT_PUBLIC_KEY` | runtime | required once a page uses gated docs | No reader sees gated docs (`docs/AGENTS.md`, "Docs only some orgs can see"). The public half of the dashboard's `DOCS_GRANT_PRIVATE_KEY` pair, base64 SPKI DER; the command below prints a pair. |
79+
| `DOCS_GATED_FIXTURES` | build and check scripts | optional | Set to `1` only for the leak check's build (`scripts/check-gated.mjs`): it adds the gated fixtures. Never on a deployment. |
80+
81+
A grant key pair for gated docs (one per environment): `DOCS_GRANT_PRIVATE_KEY` goes in the dashboard's env, `DOCS_GRANT_PUBLIC_KEY` here.
82+
83+
```bash
84+
node -e 'const k = require("node:crypto").generateKeyPairSync("ed25519"); console.log("DOCS_GRANT_PRIVATE_KEY=" + k.privateKey.export({ format: "der", type: "pkcs8" }).toString("base64")); console.log("DOCS_GRANT_PUBLIC_KEY=" + k.publicKey.export({ format: "der", type: "spki" }).toString("base64"))'
85+
```
7886
| `DOCS_API_SERVER_URL` | build and runtime | optional | The API playground offers only the servers in the OpenAPI spec. Set it to add another (a local or staging API). |
7987
| `DOCS_TWOSLASH`, `DOCS_BUILD_CPUS` | build | optional | See "Dev server" and "SDK types in the docs". |
8088
| `DOCS_DEV_ORIGINS` | dev | optional | `next dev` answers only on localhost. Comma-separated hosts to allow another (a tailnet name, say). |

‎apps/docs/app/(docs)/layout.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ReactNode } from "react";
2-
import { DocsLayout } from "fumadocs-ui/layouts/notebook";
32
import { LayoutDashboard } from "lucide-react";
43
import { AskAIButton, AskAIPanel, PhoneSearchAndAsk } from "@/components/ask-ai";
4+
import { GatedDocsLayout } from "@/components/gated";
55
import { HeaderTabs } from "@/components/header-tabs";
66
import { Logo } from "@/components/logo";
77
import { ReaderMenu } from "@/components/reader-menu";
@@ -12,7 +12,8 @@ import { githubUrl } from "@/lib/source";
1212

1313
export default function Layout({ children }: { children: ReactNode }) {
1414
return (
15-
<DocsLayout
15+
// Gated pages join the sidebar tree in the browser, for readers whose flags open them.
16+
<GatedDocsLayout
1617
tree={pageTree}
1718
tabMode="navbar"
1819
tabs={tabs}
@@ -50,6 +51,6 @@ export default function Layout({ children }: { children: ReactNode }) {
5051
>
5152
{children}
5253
{askConfigured() && <AskAIPanel />}
53-
</DocsLayout>
54+
</GatedDocsLayout>
5455
);
5556
}

‎apps/docs/app/(docs)/not-found.tsx‎

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
import { DocsBody, DocsPage, DocsTitle } from "fumadocs-ui/layouts/notebook/page";
2+
import { GatedPageOr } from "@/components/gated";
23
import { NotFoundSearch } from "@/components/not-found-search";
34
import { sectionTabs } from "@/lib/nav";
45
import { basePath } from "@/lib/base-path";
56

67
// Page-not-found inside the docs layout: the requested path, search seeded from its words,
7-
// and the three sections.
8+
// and the three sections. A gated page has no static page, so it renders here for readers whose
9+
// flags open it (components/gated.tsx).
810
export default function NotFound() {
911
return (
10-
<DocsPage toc={[]} full>
11-
<p className="font-mono text-sm text-fd-primary">404</p>
12-
<DocsTitle>This page doesn't exist</DocsTitle>
13-
<DocsBody>
14-
<NotFoundSearch />
15-
<h2 className="mt-10 text-base font-semibold">Or start from a section</h2>
16-
<div className="not-prose mt-3 grid gap-3 sm:grid-cols-3">
17-
{sectionTabs.map((t) => (
18-
<a
19-
key={t.url}
20-
href={`${basePath}${t.url}`}
21-
className="rounded-lg border bg-fd-card p-4 transition-colors hover:border-fd-primary/50 hover:bg-fd-accent"
22-
>
23-
<p className="text-sm font-medium text-fd-foreground">{t.title}</p>
24-
{t.description && (
25-
<p className="mt-1 text-sm text-fd-muted-foreground">{t.description}</p>
26-
)}
27-
</a>
28-
))}
29-
</div>
30-
</DocsBody>
31-
</DocsPage>
12+
<GatedPageOr>
13+
<DocsPage toc={[]} full>
14+
<p className="font-mono text-sm text-fd-primary">404</p>
15+
<DocsTitle>This page doesn't exist</DocsTitle>
16+
<DocsBody>
17+
<NotFoundSearch />
18+
<h2 className="mt-10 text-base font-semibold">Or start from a section</h2>
19+
<div className="not-prose mt-3 grid gap-3 sm:grid-cols-3">
20+
{sectionTabs.map((t) => (
21+
<a
22+
key={t.url}
23+
href={`${basePath}${t.url}`}
24+
className="rounded-lg border bg-fd-card p-4 transition-colors hover:border-fd-primary/50 hover:bg-fd-accent"
25+
>
26+
<p className="text-sm font-medium text-fd-foreground">{t.title}</p>
27+
{t.description && (
28+
<p className="mt-1 text-sm text-fd-muted-foreground">{t.description}</p>
29+
)}
30+
</a>
31+
))}
32+
</div>
33+
</DocsBody>
34+
</DocsPage>
35+
</GatedPageOr>
3236
);
3337
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { gatedNav, gatedPage, gatedSection } from "@/lib/gated";
2+
import { requestGrant, verifyGrant } from "@/lib/gated-grant";
3+
4+
// Gated docs for a reader whose grant (from the dashboard's /resources/docs/me) carries the flag:
5+
// GET /api/gated/nav the gated pages the grant opens, for the sidebar
6+
// GET /api/gated/page/<slug...> a gated page's title, description and HTML
7+
// GET /api/gated/section/<id> a gated section's HTML
8+
// A page or section the grant doesn't open answers like one that doesn't exist.
9+
10+
export const dynamic = "force-dynamic";
11+
12+
const headers = {
13+
"Cache-Control": "private, no-store",
14+
"X-Robots-Tag": "noindex",
15+
Vary: "Authorization",
16+
};
17+
18+
const json = (body: unknown, status = 200) => Response.json(body, { status, headers });
19+
20+
export async function GET(request: Request, { params }: { params: Promise<{ path: string[] }> }) {
21+
const grant = verifyGrant(requestGrant(request), process.env.DOCS_GRANT_PUBLIC_KEY);
22+
if (!grant) return json({ error: "No valid grant" }, 401);
23+
24+
const [kind, ...rest] = (await params).path;
25+
const body =
26+
kind === "nav" && rest.length === 0
27+
? gatedNav(grant.flags)
28+
: kind === "page"
29+
? await gatedPage(rest.join("/"), grant.flags)
30+
: kind === "section" && rest.length === 1
31+
? await gatedSection(rest[0]!, grant.flags)
32+
: null;
33+
return body ? json(body) : json({ error: "Not found" }, 404);
34+
}

‎apps/docs/app/embed/not-found.tsx‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
"use client";
22

3-
import { usePathname } from "next/navigation";
43
import { DocsTitle } from "fumadocs-ui/layouts/notebook/page";
5-
import { EmbedFrame } from "@/components/embed";
4+
import { EmbedGatedPageOr } from "@/components/gated";
65
import { NotFoundSearch } from "@/components/not-found-search";
76
import { basePath } from "@/lib/base-path";
87

98
// A missing page in the dashboard's docs panel: search seeded from the path, and a way back in.
10-
// EmbedFrame keeps these links inside the panel.
9+
// EmbedGatedPageOr's frame keeps these links inside the panel. A gated page renders here for
10+
// readers whose flags open it (components/gated.tsx).
1111
export default function EmbedNotFound() {
12-
const path = usePathname().replace(/^\/embed(?=\/)/, "");
1312
return (
14-
<EmbedFrame title="Not found" path={path}>
13+
<EmbedGatedPageOr title="Not found">
1514
<article className="mx-auto flex w-full max-w-[860px] flex-col gap-6 px-5 py-5">
1615
<header className="flex flex-col gap-2 border-b pb-5">
1716
<p className="font-mono text-sm text-fd-primary">404</p>
@@ -29,6 +28,6 @@ export default function EmbedNotFound() {
2928
.
3029
</p>
3130
</article>
32-
</EmbedFrame>
31+
</EmbedGatedPageOr>
3332
);
3433
}

‎apps/docs/components/embed.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export function EmbedFrame({
1919

2020
useEffect(() => {
2121
// Nothing private: the parent learns the page it already asked for.
22+
// No title yet (a URL that may be a gated page): the panel keeps what it shows.
23+
if (!title) return;
2224
const publish = () => postPage(title, path, window.location.hash);
2325
publish();
2426
// Back and Forward between sections of this page, and fragment changes outside the router.

‎apps/docs/components/gated.tsx‎

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
"use client";
2+
3+
import {
4+
useEffect,
5+
useMemo,
6+
useState,
7+
useSyncExternalStore,
8+
type ComponentProps,
9+
type CSSProperties,
10+
type ReactNode,
11+
} from "react";
12+
import { usePathname } from "next/navigation";
13+
import { DocsLayout } from "fumadocs-ui/layouts/notebook";
14+
import { DocsBody, DocsDescription, DocsPage, DocsTitle } from "fumadocs-ui/layouts/notebook/page";
15+
import { EmbedFrame } from "@/components/embed";
16+
import { gatedPageView, withGatedPages, withGatedTabs, type GatedNavItem } from "@/lib/gated-view";
17+
import { fetchGated, hasSignedInHint, useReader } from "@/lib/reader";
18+
19+
// Gated docs in the browser (lib/gated-content.ts): fetched from /api/gated with the reader's
20+
// grant after the page loads, so the static HTML, and everyone without the flag, never has them.
21+
// The HTML comes from our own MDX, compiled by lib/rehype-gated.ts with components refused.
22+
23+
type GatedPageData = { title: string; description: string | null; html: string };
24+
25+
/** The gated response at `path` for this reader: undefined until it answers, null if it found nothing. */
26+
function useGated<T>(path: string): T | null | undefined {
27+
const reader = useReader();
28+
const flags = reader.kind === "signed-in" ? reader.docsFlags.join() : "";
29+
const key = `${flags} ${path}`;
30+
const [got, setGot] = useState<{ key: string; data: T | null }>();
31+
useEffect(() => {
32+
if (!flags) return;
33+
let live = true;
34+
fetchGated(path)
35+
.then((res) => (res?.ok ? (res.json() as Promise<T>) : null))
36+
.catch(() => null)
37+
.then((data) => live && setGot({ key, data }));
38+
return () => {
39+
live = false;
40+
};
41+
}, [path, flags, key]);
42+
// Keyed, so another URL or reader never shows the last answer while its own loads.
43+
return got?.key === key ? got.data : undefined;
44+
}
45+
46+
const Html = ({ html }: { html: string }) => <div dangerouslySetInnerHTML={{ __html: html }} />;
47+
48+
// Gated MDX can't use components, so the site marks it as a private beta here, in the callout
49+
// "tip" tone (app/global.css), and authors never write the note themselves.
50+
const tone = {
51+
"--tone": "var(--callout-tip)",
52+
"--tint": "var(--callout-tip-tint)",
53+
} as CSSProperties;
54+
55+
function PrivateBetaBadge() {
56+
return (
57+
<div
58+
style={tone}
59+
className="not-prose flex flex-wrap items-center gap-x-2 gap-y-1 text-[0.8125rem] leading-snug"
60+
>
61+
{/* Pure --tone text is under 4.5:1 on the chip in dark mode; 30% foreground lifts it past 5:1 in both themes. */}
62+
<span className="rounded bg-[color-mix(in_srgb,var(--tone)_14%,transparent)] px-1.5 py-px text-[0.75rem] font-medium text-[color-mix(in_srgb,var(--tone)_70%,var(--color-fd-foreground))]">
63+
Private beta
64+
</span>
65+
<span className="text-fd-muted-foreground">
66+
Visible to your organization while this feature is in private beta
67+
</span>
68+
</div>
69+
);
70+
}
71+
72+
/** `<Gated id="..." />` in a page: the section's content for readers whose flags open it. */
73+
export function Gated({ id }: { id: string }) {
74+
const section = useGated<{ html: string }>(`section/${encodeURIComponent(id)}`);
75+
if (!section) return null;
76+
return (
77+
<section
78+
aria-label="Private beta"
79+
style={tone}
80+
className="my-6 overflow-hidden rounded-lg border border-[color-mix(in_srgb,var(--tone)_28%,transparent)]"
81+
>
82+
<div className="border-b border-[color-mix(in_srgb,var(--tone)_20%,transparent)] bg-(--tint) px-4 py-2">
83+
<PrivateBetaBadge />
84+
</div>
85+
<div className="px-4 [&>div>:first-child]:mt-4 [&>div>:last-child]:mb-4">
86+
<Html html={section.html} />
87+
</div>
88+
</section>
89+
);
90+
}
91+
92+
/** The docs layout, with the gated pages the reader's flags open placed in its sidebar tree and section tabs. */
93+
export function GatedDocsLayout(props: ComponentProps<typeof DocsLayout>) {
94+
const items = useGated<GatedNavItem[]>("nav");
95+
const tree = useMemo(() => withGatedPages(props.tree, items ?? []), [props.tree, items]);
96+
const tabs = useMemo(
97+
() => (Array.isArray(props.tabs) ? withGatedTabs(props.tabs, items ?? []) : props.tabs),
98+
[props.tabs, items]
99+
);
100+
return <DocsLayout {...props} tree={tree} tabs={tabs} />;
101+
}
102+
103+
const noSubscribe = () => () => {};
104+
105+
function useGatedPage() {
106+
const reader = useReader();
107+
const path = usePathname().replace(/^\/embed(?=\/)/, "");
108+
const page = useGated<GatedPageData>(`page${path}`);
109+
const hint = useSyncExternalStore(
110+
noSubscribe,
111+
() => hasSignedInHint(document.cookie),
112+
() => null
113+
);
114+
const view = gatedPageView(reader, hint, page);
115+
useEffect(() => {
116+
if (page) document.title = page.title;
117+
}, [page]);
118+
// "page" always has one; "wait" never does.
119+
return { view, page: page ?? null, path };
120+
}
121+
122+
/** A neutral page while it isn't known yet whether this URL is a gated page or a 404. */
123+
function Skeleton() {
124+
return (
125+
<div role="status" aria-label="Loading" className="flex animate-pulse flex-col gap-6">
126+
<div className="flex flex-col gap-3 border-b pb-6">
127+
<div className="h-8 w-1/2 rounded-md bg-fd-muted" />
128+
<div className="h-5 w-3/4 rounded-md bg-fd-muted" />
129+
</div>
130+
<div className="flex flex-col gap-3">
131+
{["w-full", "w-11/12", "w-full", "w-2/3"].map((w, i) => (
132+
<div key={i} className={`h-4 rounded bg-fd-muted ${w}`} />
133+
))}
134+
</div>
135+
</div>
136+
);
137+
}
138+
139+
/** The docs 404: the gated page at this URL when the reader's flags open one, else `children`. */
140+
export function GatedPageOr({ children }: { children: ReactNode }) {
141+
const { view, page } = useGatedPage();
142+
if (view === "404") return children;
143+
return (
144+
<DocsPage toc={[]}>
145+
{page ? (
146+
<>
147+
<header className="flex flex-col gap-2 border-b pb-6">
148+
<DocsTitle>{page.title}</DocsTitle>
149+
{page.description && (
150+
<DocsDescription className="mb-0">{page.description}</DocsDescription>
151+
)}
152+
<PrivateBetaBadge />
153+
</header>
154+
<DocsBody>
155+
<Html html={page.html} />
156+
</DocsBody>
157+
</>
158+
) : (
159+
<Skeleton />
160+
)}
161+
</DocsPage>
162+
);
163+
}
164+
165+
/**
166+
* The docs panel's 404: as GatedPageOr, in the panel's frame. The frame tells the dashboard the
167+
* page's title, so it says nothing until the browser knows (never "Not found" for a gated page).
168+
*/
169+
export function EmbedGatedPageOr({ title, children }: { title: string; children: ReactNode }) {
170+
const { view, page, path } = useGatedPage();
171+
const shown = view === "wait" ? "" : (page?.title ?? title);
172+
return (
173+
<EmbedFrame title={shown} path={path}>
174+
{view === "404" ? (
175+
children
176+
) : (
177+
<article className="mx-auto flex w-full max-w-[860px] flex-col gap-6 px-5 py-5">
178+
{page ? (
179+
<>
180+
<header className="flex flex-col gap-2 border-b pb-5">
181+
<DocsTitle className="text-2xl">{page.title}</DocsTitle>
182+
{page.description && (
183+
<DocsDescription className="mb-0 text-base">{page.description}</DocsDescription>
184+
)}
185+
<PrivateBetaBadge />
186+
</header>
187+
<DocsBody>
188+
<Html html={page.html} />
189+
</DocsBody>
190+
</>
191+
) : (
192+
<Skeleton />
193+
)}
194+
</article>
195+
)}
196+
</EmbedFrame>
197+
);
198+
}

0 commit comments

Comments
 (0)