-
Notifications
You must be signed in to change notification settings - Fork 1
RFC: Backend Rendering of Swagger UI during Next.js Build Phase (Production Container)
Currently, the Swagger UI is rendered on the frontend as a Single Page Application (SPA) using React components. The Swagger schema is fetched and patched dynamically during development and runtime.
Our production build is based on a multi-stage Docker setup that:
- Fetches and patches the Swagger schema via scripts (
fetchSwaggerSchema.mjs,patchSwaggerServer.mjs) - Runs
next buildandnext exportto produce a static site - Serves static assets from the
/outdirectory
However, the Swagger UI is still rendered dynamically in the browser. This does not align with the static site generation (SSG) model and results in a heavier client bundle and delayed content rendering.
Render the Swagger on the Next.js build phase, replacing the current dynamic SPA-based approach. This should be applied to the production container only.
Introduce a backend-rendered Swagger UI by generating a fully static HTML page during the production Docker build process. This static page will embed the Swagger schema and replace the current React-based dynamic Swagger component.
-
Update the Dockerfile
buildstage to:- Run the existing scripts:
node scripts/fetchSwaggerSchema.mjs && node scripts/patchSwaggerServer.mjs - Add a new step to generate a static Swagger HTML page from the patched schema using
swagger-ui-distor a similar tool. - Place the generated
swagger.htmlinto the/publicdirectory (or directly into/outafter export). - Proceed with:
npx next build && npx next export
- Run the existing scripts:
-
Update routing or links on the frontend to reference
/swagger.htmlinstead of rendering<SwaggerUI />dynamically.
To ensure compatibility with both development (SPA-based Swagger) and production (static HTML), we recommend introducing a conditional switch in the Swagger component.
Add a public environment variable:
# In .env.production
NEXT_PUBLIC_STATIC_SWAGGER=true
# In .env.development
NEXT_PUBLIC_STATIC_SWAGGER=falseUpdate the Swagger component logic:
if (process.env.NEXT_PUBLIC_STATIC_SWAGGER === 'true') {
if (typeof window !== 'undefined') {
window.location.href = '/swagger.html';
}
return null;
}This allows:
-
The dev container to continue using dynamic Swagger UI with hot reloading (this can be changed if needed).
-
The prod container to redirect to a statically pre-generated Swagger page.
-
swagger-ui-dist: For generating a self-contained Swagger UI HTML page.
-
Node.js script (e.g., generateSwaggerStaticUI.js) to combine the patched schema with a Swagger UI template.
-
Swagger documentation is available immediately as static content.
-
Reduces frontend JavaScript bundle size.
-
Fully compatible with SSG and CDN caching strategies.
-
No runtime fetch or React rendering required for API docs.
-
Swagger documentation becomes static — schema updates require a new build.
-
Any runtime customization must be shifted to build-time.
- Create
generateSwaggerStaticUI.jsscript - Update the Dockerfile
buildstage - Update Next.js links/routes to point to the static Swagger page
- Update Swagger React component for both environments – development and production
- (Optional) Add a CI step to validate the presence of
/swagger.htmlin the exported output