@@ -11,8 +11,8 @@ import BrowserOnly from "@docusaurus/BrowserOnly";
11
11
import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment" ;
12
12
import { HtmlClassNameProvider } from "@docusaurus/theme-common" ;
13
13
import useDocusaurusContext from "@docusaurus/useDocusaurusContext" ;
14
+ import useIsBrowser from "@docusaurus/useIsBrowser" ;
14
15
import type { Props } from "@theme/DocItem" ;
15
- import clsx from "clsx" ;
16
16
import { ServerObject } from "docusaurus-plugin-openapi-docs/lib/openapi/types" ;
17
17
import type { ApiItem as ApiItemType } from "docusaurus-plugin-openapi-docs/lib/types" ;
18
18
import { ParameterObject } from "docusaurus-plugin-openapi-docs/src/openapi/types" ;
@@ -24,13 +24,11 @@ import { createAuth } from "../ApiDemoPanel/Authorization/slice";
24
24
import { createPersistanceMiddleware } from "../ApiDemoPanel/persistanceMiddleware" ;
25
25
import DocItemLayout from "./Layout" ;
26
26
import DocItemMetadata from "./Metadata" ;
27
- import { createStoreWithState } from "./store" ;
27
+ import { createStoreWithoutState , createStoreWithState } from "./store" ;
28
28
29
29
const { DocProvider } = require ( "@docusaurus/theme-common/internal" ) ;
30
30
31
- let ApiDemoPanel = ( _ : { item : any ; infoPath : any } ) => (
32
- < div style = { { marginTop : "3.5em" } } />
33
- ) ;
31
+ let ApiDemoPanel = ( _ : { item : any ; infoPath : any } ) => < div /> ;
34
32
35
33
if ( ExecutionEnvironment . canUseDOM ) {
36
34
ApiDemoPanel = require ( "@theme/ApiDemoPanel" ) . default ;
@@ -49,8 +47,19 @@ export default function ApiItem(props: Props): JSX.Element {
49
47
const { siteConfig } = useDocusaurusContext ( ) ;
50
48
const themeConfig = siteConfig . themeConfig as ThemeConfig ;
51
49
const options = themeConfig . api ;
50
+ const isBrowser = useIsBrowser ( ) ;
52
51
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 ) {
54
63
const acceptArray = Array . from (
55
64
new Set (
56
65
Object . values ( api ?. responses ?? { } )
@@ -79,20 +88,20 @@ export default function ApiItem(props: Props): JSX.Element {
79
88
securitySchemes : api ?. securitySchemes ,
80
89
options,
81
90
} ) ;
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");
84
94
const server = window ?. sessionStorage . getItem ( "server" ) ;
85
95
const serverObject = ( JSON . parse ( server ! ) as ServerObject ) ?? { } ;
86
96
87
- const persistanceMiddleware = createPersistanceMiddleware ( options ) ;
88
- const store2 = createStoreWithState (
97
+ store2 = createStoreWithState (
89
98
{
90
99
accept : {
91
- value : acceptValue || acceptArray [ 0 ] ,
100
+ value : acceptArray [ 0 ] ,
92
101
options : acceptArray ,
93
102
} ,
94
103
contentType : {
95
- value : contentTypeValue || contentTypeArray [ 0 ] ,
104
+ value : contentTypeArray [ 0 ] ,
96
105
options : contentTypeArray ,
97
106
} ,
98
107
server : {
@@ -106,50 +115,42 @@ export default function ApiItem(props: Props): JSX.Element {
106
115
} ,
107
116
[ persistanceMiddleware ]
108
117
) ;
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
+ }
125
119
126
120
if ( api ) {
127
- // TODO: determine if there's a way to SSR and hydrate ApiItem/ApiDemoPanel
128
121
return (
129
122
< DocProvider content = { props . content } >
130
123
< HtmlClassNameProvider className = { docHtmlClassName } >
131
124
< DocItemMetadata />
132
125
< 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 >
140
140
</ DocItemLayout >
141
141
</ HtmlClassNameProvider >
142
142
</ DocProvider >
143
143
) ;
144
144
}
145
+
145
146
// Non-API docs
146
147
return (
147
148
< DocProvider content = { props . content } >
148
149
< HtmlClassNameProvider className = { docHtmlClassName } >
149
150
< DocItemMetadata />
150
151
< DocItemLayout >
151
152
< div className = "row" >
152
- < div className = { clsx ( "col col--12" ) } >
153
+ < div className = "col col--12" >
153
154
< MDXComponent />
154
155
</ div >
155
156
</ div >
0 commit comments