swagger-ui: Swagger-ui-react not working with nextjs since 5.3.2
Q&A (please complete the following information)
- OS: Windows 10
- Browser: Firefox
- Version: 117.0.1
- Method of installation: npm
- Swagger-UI version: 5.7.2 (or any other > 5.3.2)
- Swagger/OpenAPI version: Swagger 2.0, OpenAPI 3.0
Content & configuration
Example Swagger/OpenAPI definition: Not relevant (it’s never loaded).
Swagger-UI configuration options:
<SwaggerUI
url={schemaUrl}
requestSnippetsEnabled={true}
defaultModelExpandDepth={1000}
defaultModelsExpandDepth={1000}
/>
Describe the bug you’re encountering
After updating to swagger-ui-react to any version higher than 5.3.2 I get the error: TypeError: Class extends value undefined is not a constructor or null along with a stacktrace when compiling the page.
Stacktrace (with redacted project path):
Server Error
TypeError: Class extends value undefined is not a constructor or null
This might be caused by a React Class Component being rendered in a Server Component, React Class Components only works in Client Components. Read more: https://nextjs.org/docs/messages/class-component-in-server-component
This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
File
node_modules\swagger-ui-react\swagger-ui-bundle.js (2:813819)
eval
node_modules\swagger-ui-react\swagger-ui-bundle.js (2:1383722)
eval
node_modules\swagger-ui-react\swagger-ui-bundle.js (2:1383738)
s
node_modules\swagger-ui-react\swagger-ui-bundle.js (2:113)
eval
node_modules\swagger-ui-react\swagger-ui-bundle.js (2:244)
./node_modules/swagger-ui-react/swagger-ui-bundle.js
file:/PROJECT_DIR/.next/server/vendor-chunks/swagger-ui-react.js (20:1)
__webpack_require__
file:/PROJECT_DIR/.next/server/webpack-runtime.js (33:42)
eval
webpack-internal:///./node_modules/swagger-ui-react/index.mjs (7:69)
./node_modules/swagger-ui-react/index.mjs
file:/PROJECT_DIR/.next/server/vendor-chunks/swagger-ui-react.js (41:1)
__webpack_require__
file:/PROJECT_DIR/.next/server/webpack-runtime.js (33:42)
eval
webpack-internal:///./src/components/views/Swagger.tsx (11:74)
./src/components/views/Swagger.tsx
file:/PROJECT_DIR/.next/server/pages/index.js (74:1)
__webpack_require__
file:/PROJECT_DIR/.next/server/webpack-runtime.js (33:42)
eval
webpack-internal:///./src/pages/index.tsx (13:83)
__webpack_require__.a
file:/PROJECT_DIR/.next/server/webpack-runtime.js (97:13)
To reproduce…
Steps to reproduce the behavior:
- Use any version of
swagger-ui-reactafter 5.3.2 in a project running nextjs 13.
Expected behavior
The module should compile without issues.
Screenshots
The stacktrace window, full stacktrace can be found above.
Additional context or thoughts
swagger-viewer-react worked fine with nextjs until updating to the newer versions (downgrading to 5.3.2 fixes the issue).
I have tried following the nextjs instructions provided in the stacktrace but the only one I could affect (the use client option) didn’t work.
Since the problem is specific to certain versions of swagger-ui (that doesn’t seem to mention next-js or any relevant info in the changelog) the problem appears to be at this end.
About this issue
- Original URL
- State: closed
- Created 9 months ago
- Reactions: 13
- Comments: 20 (9 by maintainers)
Commits related to this issue
- fix(example): try to build swagger ui with nextjs More details on https://github.com/swagger-api/swagger-ui/issues/9243 — committed to jellydn/next-swagger-doc by jellydn 8 months ago
- feat(swagger-ui-react): rewrite into SSR compatible func component Refs #9243 — committed to swagger-api/swagger-ui by char0n 2 months ago
- feat(swagger-ui-react): rewrite into SSR compatible function component (#9855) Refs #9243 — committed to swagger-api/swagger-ui by char0n 2 months ago
For next you can use class componnets only for client components you can import with dynamic and this fix the problem
Hi @afikrim,
Added new table row to https://github.com/swagger-api/swagger-ui/wiki/Seamless-Integration-with-Bundlers-&-Test-Tools#nextjs.
There is now a POC repo containing recipe how to get Next.js + TypeScript + swagger-ui-react get working with Node.js >= 16.
I think this is an issue with the underlying swagger-js library extending the
Fileclass: https://github.com/swagger-api/swagger-js/blob/c70b071a1cb27e60d129816dd9c94af4a44033ca/src/http/index.js#L200When using the
<SwaggerUI />React component in a Next.js project, the project launched and built as a static site successfully on my development machine with Node v20.7.0. When I copied the same code to another machine that only had Node v18.15.0, I started getting the error described in this issue.From the working machine,
nodeallows me to instantiate anew Fileclass. From the non-working Node v18 machine,new Filereturns the errorUncaught ReferenceError: File is not defined.MDN: File indicates that this class has been available in the browser for quite a while, so my guess is the issue is with the version of Node used to build Next.js site. Alternatively, the file I linked above looks like it’s under active development, maybe this issue will go away with the implementation of
undicifor the Fetch polyfill: https://github.com/swagger-api/swagger-js/commit/f341994f19cfbd775b17d55c254f962d93c61e4b#diff-df2ea73a8b28954468661416b485a053b8f9da01e163f0d2fa45787ef1874ecbI’ve just tried to use your polyfil file and it works! Thanks a lot. I will upgrade node to v20 asap after the release since it’s optimal solution.
HI @davulrich,
Solution described in https://github.com/swagger-api/swagger-ui/issues/9243#issuecomment-1768027619 must work for you on Node.js >= 16. But upgrading to Node.js >=20 is the easiest option anyway.
Therefor we must using swagger-ui-react@5.3.2, otherwise,
npm run buildwill throw eror in nextjs 13. Is right?Hi everybody,
Please have a look at: https://github.com/swagger-api/swagger-ui/wiki/Seamless-Integration-with-Bundlers-&-Test-Tools#nextjs
The POC repo contains recipe how to get Next.js get working with Node.js >= 16.