@@ -11,8 +11,8 @@ import BrowserOnly from "@docusaurus/BrowserOnly";
1111import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment" ;
1212import { HtmlClassNameProvider } from "@docusaurus/theme-common" ;
1313import useDocusaurusContext from "@docusaurus/useDocusaurusContext" ;
14+ import useIsBrowser from "@docusaurus/useIsBrowser" ;
1415import type { Props } from "@theme/DocItem" ;
15- import clsx from "clsx" ;
1616import { ServerObject } from "docusaurus-plugin-openapi-docs/lib/openapi/types" ;
1717import type { ApiItem as ApiItemType } from "docusaurus-plugin-openapi-docs/lib/types" ;
1818import { ParameterObject } from "docusaurus-plugin-openapi-docs/src/openapi/types" ;
@@ -24,13 +24,11 @@ import { createAuth } from "../ApiDemoPanel/Authorization/slice";
2424import { createPersistanceMiddleware } from "../ApiDemoPanel/persistanceMiddleware" ;
2525import DocItemLayout from "./Layout" ;
2626import DocItemMetadata from "./Metadata" ;
27- import { createStoreWithState } from "./store" ;
27+ import { createStoreWithoutState , createStoreWithState } from "./store" ;
2828
2929const { DocProvider } = require ( "@docusaurus/theme-common/internal" ) ;
3030
31- let ApiDemoPanel = ( _ : { item : any ; infoPath : any } ) => (
32- < div style = { { marginTop : "3.5em" } } />
33- ) ;
31+ let ApiDemoPanel = ( _ : { item : any ; infoPath : any } ) => < div /> ;
3432
3533if ( ExecutionEnvironment . canUseDOM ) {
3634 ApiDemoPanel = require ( "@theme/ApiDemoPanel" ) . default ;
@@ -49,8 +47,19 @@ export default function ApiItem(props: Props): JSX.Element {
4947 const { siteConfig } = useDocusaurusContext ( ) ;
5048 const themeConfig = siteConfig . themeConfig as ThemeConfig ;
5149 const options = themeConfig . api ;
50+ const isBrowser = useIsBrowser ( ) ;
5251
53- const ApiDocContent = ( ) => {
52+ // Define store2
53+ let store2 : any = { } ;
54+ const persistanceMiddleware = createPersistanceMiddleware ( options ) ;
55+
56+ // Init store for SSR
57+ if ( ! isBrowser ) {
58+ store2 = createStoreWithoutState ( { } , [ persistanceMiddleware ] ) ;
59+ }
60+
61+ // Init store for CSR to hydrate components
62+ if ( isBrowser ) {
5463 const acceptArray = Array . from (
5564 new Set (
5665 Object . values ( api ?. responses ?? { } )
@@ -79,20 +88,20 @@ export default function ApiItem(props: Props): JSX.Element {
7988 securitySchemes : api ?. securitySchemes ,
8089 options,
8190 } ) ;
82- const acceptValue = window ?. sessionStorage . getItem ( "accept" ) ;
83- const contentTypeValue = window ?. sessionStorage . getItem ( "contentType" ) ;
91+ // TODO: determine way to rehydrate without flashing
92+ // const acceptValue = window?.sessionStorage.getItem("accept");
93+ // const contentTypeValue = window?.sessionStorage.getItem("contentType");
8494 const server = window ?. sessionStorage . getItem ( "server" ) ;
8595 const serverObject = ( JSON . parse ( server ! ) as ServerObject ) ?? { } ;
8696
87- const persistanceMiddleware = createPersistanceMiddleware ( options ) ;
88- const store2 = createStoreWithState (
97+ store2 = createStoreWithState (
8998 {
9099 accept : {
91- value : acceptValue || acceptArray [ 0 ] ,
100+ value : acceptArray [ 0 ] ,
92101 options : acceptArray ,
93102 } ,
94103 contentType : {
95- value : contentTypeValue || contentTypeArray [ 0 ] ,
104+ value : contentTypeArray [ 0 ] ,
96105 options : contentTypeArray ,
97106 } ,
98107 server : {
@@ -106,50 +115,42 @@ export default function ApiItem(props: Props): JSX.Element {
106115 } ,
107116 [ persistanceMiddleware ]
108117 ) ;
109-
110- return (
111- < Provider store = { store2 } >
112- < div className = "row" >
113- < div className = { clsx ( "col" , api ? "col--7" : "col--12" ) } >
114- < MDXComponent />
115- </ div >
116- { api && (
117- < div className = "col col--5" >
118- < ApiDemoPanel item = { api } infoPath = { infoPath } />
119- </ div >
120- ) }
121- </ div >
122- </ Provider >
123- ) ;
124- } ;
118+ }
125119
126120 if ( api ) {
127- // TODO: determine if there's a way to SSR and hydrate ApiItem/ApiDemoPanel
128121 return (
129122 < DocProvider content = { props . content } >
130123 < HtmlClassNameProvider className = { docHtmlClassName } >
131124 < DocItemMetadata />
132125 < DocItemLayout >
133- {
134- < BrowserOnly fallback = { < div /> } >
135- { ( ) => {
136- return < ApiDocContent /> ;
137- } }
138- </ BrowserOnly >
139- }
126+ < Provider store = { store2 } >
127+ < div className = "row" >
128+ < div className = "col col--7" >
129+ < MDXComponent />
130+ </ div >
131+ < div className = "col col--5" >
132+ < BrowserOnly fallback = { < div > Loading...</ div > } >
133+ { ( ) => {
134+ return < ApiDemoPanel item = { api } infoPath = { infoPath } /> ;
135+ } }
136+ </ BrowserOnly >
137+ </ div >
138+ </ div >
139+ </ Provider >
140140 </ DocItemLayout >
141141 </ HtmlClassNameProvider >
142142 </ DocProvider >
143143 ) ;
144144 }
145+
145146 // Non-API docs
146147 return (
147148 < DocProvider content = { props . content } >
148149 < HtmlClassNameProvider className = { docHtmlClassName } >
149150 < DocItemMetadata />
150151 < DocItemLayout >
151152 < div className = "row" >
152- < div className = { clsx ( "col col--12" ) } >
153+ < div className = "col col--12" >
153154 < MDXComponent />
154155 </ div >
155156 </ div >
0 commit comments