Performance: Swagger API documentation
This affects the initial page load speed and bundle size by adding metadata and UI assets for API docs.
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; const config = new DocumentBuilder() .setTitle('API') .setDescription('API docs') .setVersion('1.0') .build(); const document = SwaggerModule.createDocument(app, config); if (process.env.NODE_ENV === 'development') { SwaggerModule.setup('api', app, document); }
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; const config = new DocumentBuilder() .setTitle('API') .setDescription('API docs') .setVersion('1.0') .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('api', app, document);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full Swagger UI always loaded | High (many UI nodes) | Multiple reflows on load | High paint cost due to UI | [X] Bad |
| Swagger UI loaded only in development | Low (no UI in prod) | No reflows in prod | Minimal paint cost in prod | [OK] Good |
const config = new DocumentBuilder()
.setTitle('Cats API')
.setDescription('API for cats')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api-docs', app, document);const config = new DocumentBuilder()
.setTitle('Dogs API')
.setDescription('API for dogs')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(config, app);
SwaggerModule.setup('docs', app, document);Pass the test and this pattern moves unknown