Didn't need any YAML file to reproduce this.
ReferenceError: window is not defined
I have experience this both with Next.js and Docusaurus.
Steps to reproduce the behavior:
yarn create next-app - I choose "swagger-test" as project name and Default templatecd swagger-testyarn add swagger-ui-reactindex.js to just serve SwaggerUI: import SwaggerUI from "swagger-ui-react";
export default function Home() {
return (
<SwaggerUI />)
}
yarn dev to start dev serverExpect SwaggerUI to work with frameworks/libraries like Next.JS and Docusaurus that have server code where window isn't defined.
@char0n Can i try this one too?
same error
same error
@naima-shk sure, go ahead
Having the same issue with Next.js dev server and "swagger-ui-react": "^3.28.0"
Right, I looks like this issue is basically adding a support for SSR (Server Side Rendering). I can see that we use window global and other global browser symbols directly in bodies of swagger-ui components. I don't fully understand next.js internals but I assume it tries to render <SwaggerUI /> component in node.js and then serve it to browser. As the node.js doesn't have any global window symbols it will fail on ReferenceError.
@char0n you're right the issue is SSR. I was able to solve this by disabling SSR and using next.js's dynamic import.
Standard SwaggerUI component in pages/docs/SwaggerUI.tsx:
import SwaggerUI from "swagger-ui-react"
export default function SwaggerDocs() {
return (
<SwaggerUI url="https://petstore.swagger.io/v2/swagger.json" />
)
}
Dynamically importing it in pages/docs/index.tsx:
const DynamicSwaggerUI = dynamic(() => import('./SwaggerUI'), {
ssr: false,
});
export default function DynamicSwaggerUIDocs() {
return (
<DynamicSwaggerUI />
)
}
Here is also a helpful resource: https://codeconqueror.com/blog/next-js-disable-ssr
@fowad-sohail thank you for the workaround!
Of course! It's a pleasure to help the community that's helped me countless times 馃槃
UPDATE:
The solution I provided above works locally. However, when I deploy to Vercel, it throws the same error. I haven't looked into fixing it too far in depth just yet, but if someone finds a solution please do post here.
Has anyone managed to make this work in Gatsby or Docusaurus?
@luizcieslak I tried to use this UI into a Docusaurus project, error is the same "Window is not defined"
Most helpful comment
@char0n you're right the issue is SSR. I was able to solve this by disabling SSR and using
next.js's dynamic import.Standard
SwaggerUIcomponent inpages/docs/SwaggerUI.tsx:Dynamically importing it in
pages/docs/index.tsx:Here is also a helpful resource: https://codeconqueror.com/blog/next-js-disable-ssr