@@ -9,44 +9,76 @@ import {
99 type CSSProperties ,
1010 type ReactNode ,
1111} from "react" ;
12- import { usePathname } from "next/navigation" ;
12+ import { unstable_isUnrecognizedActionError , usePathname } from "next/navigation" ;
1313import { DocsLayout } from "fumadocs-ui/layouts/notebook" ;
1414import { DocsBody , DocsDescription , DocsPage , DocsTitle } from "fumadocs-ui/layouts/notebook/page" ;
1515import { EmbedFrame } from "@/components/embed" ;
1616import { gatedPageView , withGatedPages , withGatedTabs , type GatedNavItem } from "@/lib/gated-view" ;
17- import { fetchGated , hasSignedInHint , useReader } from "@/lib/reader" ;
17+ import { loadGated } from "@/lib/gated-action" ;
18+ import { fetchGated , hasSignedInHint , useReader , withGrant } from "@/lib/reader" ;
1819
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.
20+ // Gated docs in the browser (lib/gated-content.ts), asked for with the reader's grant after the
21+ // page loads, so the static HTML, and everyone without the flag, never has them: the sidebar entries
22+ // from /api/gated/nav, page and section bodies from the loadGated server action, rendered on the
23+ // server with the public pages' MDX components.
2224
23- type GatedPageData = { title : string ; description : string | null ; html : string } ;
25+ type Loaded = Exclude < Awaited < ReturnType < typeof loadGated > > , "refused" | null > ;
26+ type GatedSection = Pick < Loaded , "body" > ;
27+ type GatedPageData = Extract < Loaded , { title : string } > ;
2428
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 {
29+ /** Gives up on a slow answer, as fetchGated does, so a page shows its 404 instead of a skeleton. */
30+ const within10s = < T , > ( p : Promise < T > ) =>
31+ Promise . race ( [ p , new Promise < null > ( ( done ) => setTimeout ( ( ) => done ( null ) , 10_000 ) ) ] ) ;
32+
33+ function ask ( kind : "nav" | "page" | "section" , id : string ) : Promise < unknown > {
34+ if ( kind === "nav" ) return fetchGated ( "nav" ) . then ( ( res ) => ( res ?. ok ? res . json ( ) : null ) ) ;
35+ return within10s (
36+ withGrant (
37+ ( grant ) => loadGated ( grant , kind , id ) ,
38+ ( a ) => a === "refused"
39+ )
40+ ) . then ( ( a ) => ( a === "refused" ? null : a ) , reloadIfRedeployed ) ;
41+ }
42+
43+ const RELOADED = "trigger-docs:reloaded" ;
44+
45+ /** A deploy since this page loaded retires its action ids. Reload into the new build, keeping the
46+ * skeleton meanwhile; at most once a minute, so a rollout serving both builds can't loop. */
47+ function reloadIfRedeployed ( error : unknown ) : Promise < never > {
48+ if ( unstable_isUnrecognizedActionError ( error ) ) {
49+ try {
50+ if ( Date . now ( ) - Number ( sessionStorage . getItem ( RELOADED ) ) > 60_000 ) {
51+ sessionStorage . setItem ( RELOADED , String ( Date . now ( ) ) ) ;
52+ location . reload ( ) ;
53+ return new Promise ( ( ) => { } ) ;
54+ }
55+ } catch { }
56+ }
57+ return Promise . reject ( error ) ;
58+ }
59+
60+ /** The gated answer for this reader: undefined until it comes, null if it found nothing. */
61+ function useGated < T > ( kind : "nav" | "page" | "section" , id = "" ) : T | null | undefined {
2762 const reader = useReader ( ) ;
2863 const flags = reader . kind === "signed-in" ? reader . docsFlags . join ( ) : "" ;
29- const key = `${ flags } ${ path } ` ;
64+ const key = `${ flags } ${ kind } ${ id } ` ;
3065 const [ got , setGot ] = useState < { key : string ; data : T | null } > ( ) ;
3166 useEffect ( ( ) => {
3267 if ( ! flags ) return ;
3368 let live = true ;
34- fetchGated ( path )
35- . then ( ( res ) => ( res ?. ok ? ( res . json ( ) as Promise < T > ) : null ) )
69+ ask ( kind , id )
3670 . catch ( ( ) => null )
37- . then ( ( data ) => live && setGot ( { key, data } ) ) ;
71+ . then ( ( data ) => live && setGot ( { key, data : data as T | null } ) ) ;
3872 return ( ) => {
3973 live = false ;
4074 } ;
41- } , [ path , flags , key ] ) ;
75+ } , [ kind , id , flags , key ] ) ;
4276 // Keyed, so another URL or reader never shows the last answer while its own loads.
4377 return got ?. key === key ? got . data : undefined ;
4478}
4579
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.
80+ // The site marks gated content as a private beta here, in the callout "tip" tone (app/global.css),
81+ // and authors never write the note themselves.
5082const tone = {
5183 "--tone" : "var(--callout-tip)" ,
5284 "--tint" : "var(--callout-tip-tint)" ,
@@ -71,7 +103,7 @@ function PrivateBetaBadge() {
71103
72104/** `<Gated id="..." />` in a page: the section's content for readers whose flags open it. */
73105export function Gated ( { id } : { id : string } ) {
74- const section = useGated < { html : string } > ( ` section/ ${ encodeURIComponent ( id ) } ` ) ;
106+ const section = useGated < GatedSection > ( " section" , id ) ;
75107 if ( ! section ) return null ;
76108 return (
77109 < section
@@ -83,7 +115,7 @@ export function Gated({ id }: { id: string }) {
83115 < PrivateBetaBadge />
84116 </ div >
85117 < div className = "px-4 [&>div>:first-child]:mt-4 [&>div>:last-child]:mb-4" >
86- < Html html = { section . html } / >
118+ < div > { section . body } </ div >
87119 </ div >
88120 </ section >
89121 ) ;
@@ -102,10 +134,19 @@ export function GatedDocsLayout(props: ComponentProps<typeof DocsLayout>) {
102134
103135const noSubscribe = ( ) => ( ) => { } ;
104136
137+ const decodePath = ( p : string ) => {
138+ try {
139+ return decodeURIComponent ( p ) ;
140+ } catch {
141+ return p ; // a malformed escape can't name a gated page; it stays a 404
142+ }
143+ } ;
144+
105145function useGatedPage ( ) {
106146 const reader = useReader ( ) ;
107147 const path = usePathname ( ) . replace ( / ^ \/ e m b e d (? = \/ ) / , "" ) ;
108- const page = useGated < GatedPageData > ( `page${ path } ` ) ;
148+ // usePathname keeps percent escapes; gated pages are keyed by their file names.
149+ const page = useGated < GatedPageData > ( "page" , decodePath ( path . slice ( 1 ) ) ) ;
109150 const hint = useSyncExternalStore (
110151 noSubscribe ,
111152 ( ) => hasSignedInHint ( document . cookie ) ,
@@ -141,7 +182,7 @@ export function GatedPageOr({ children }: { children: ReactNode }) {
141182 const { view, page } = useGatedPage ( ) ;
142183 if ( view === "404" ) return children ;
143184 return (
144- < DocsPage toc = { [ ] } >
185+ < DocsPage toc = { page ?. toc ?? [ ] } >
145186 { page ? (
146187 < >
147188 < header className = "flex flex-col gap-2 border-b pb-6" >
@@ -151,9 +192,7 @@ export function GatedPageOr({ children }: { children: ReactNode }) {
151192 ) }
152193 < PrivateBetaBadge />
153194 </ header >
154- < DocsBody >
155- < Html html = { page . html } />
156- </ DocsBody >
195+ < DocsBody > { page . body } </ DocsBody >
157196 </ >
158197 ) : (
159198 < Skeleton />
@@ -184,9 +223,7 @@ export function EmbedGatedPageOr({ title, children }: { title: string; children:
184223 ) }
185224 < PrivateBetaBadge />
186225 </ header >
187- < DocsBody >
188- < Html html = { page . html } />
189- </ DocsBody >
226+ < DocsBody > { page . body } </ DocsBody >
190227 </ >
191228 ) : (
192229 < Skeleton />
0 commit comments