Nestjs swagger ui not loading styles when deployed to vercel but works well locally
console and network requests
I added vercel.json with the following configuration and deployed to vercel.
{
"version": 2,
"builds": [
{
"src": "src/main.ts",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "src/main.ts",
"methods": ["GET", "POST", "PUT", "PATCH", "DELETE"]
}
]
}
main.ts
const swaggerConfig = new DocumentBuilder()
.setTitle('Tansfun')
.setDescription('API for Tansfun')
.setVersion('1.0')
.addBearerAuth(
{
type: 'http',
scheme: 'bearer',
bearerFormat: 'APIKey',
name: 'APIKey',
description: 'Enter API Key',
in: 'header',
},
'APIKey-auth',
)
.build();
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const document = SwaggerModule.createDocument(app, swaggerConfig);
app.useGlobalPipes(new ValidationPipe());
SwaggerModule.setup('api', app, document);
await app.listen(port);
}
bootstrap();
I used @nestjs/swagger v6
try with this, set the custom js and css
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// swagger setup
const config = new DocumentBuilder()
.setTitle('Backend Generator')
.setDescription('Documentation API Test')
.setVersion('1.0')
.setBasePath('api/v1')
.addBearerAuth({ type: 'http', scheme: 'bearer', bearerFormat: 'JWT' })
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('swagger', app, document, {
customSiteTitle: 'Backend Generator',
customfavIcon: 'https://avatars.githubusercontent.com/u/6936373?s=200&v=4',
customJs: [
'https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui-bundle.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui-standalone-preset.min.js',
],
customCssUrl: [
'https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui.min.css',
'https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui-standalone-preset.min.css',
'https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui.css',
],
});
const cors = { ...CorsConfig };
app.enableCors(cors);
app.useGlobalPipes(new ValidationPipe({ whitelist: true }));
app.setGlobalPrefix('api/v1');
useContainer(app.select(AppModule), { fallbackOnErrors: true });
await app.listen(5000);
}
bootstrap();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With