Skip to content

Conversation

@spandios
Copy link

@spandios spandios commented Nov 8, 2025

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #3617

When users access Swagger UI with a trailing slash (e.g., /api-docs/), the browser requests static assets using incorrect paths like /api-docs/api-docs/swagger-ui.css, resulting in 404 errors.

What is the new behavior?

Browsers treat URLs differently based on trailing slash:

  • /api-docs → treated as a file, relative paths resolve from parent directory /
  • /api-docs/ → treated as a directory, relative paths resolve from /api-docs/

The Swagger UI now serves correct HTML with appropriate baseUrl based on this behavior:

  • /api-docs → uses ./api-docs/ baseUrl (resolves to /api-docs/swagger-ui.css)
  • /api-docs/ → uses ./ baseUrl (resolves to /api-docs/swagger-ui.css)

Both URL patterns now correctly load static assets regardless of trailing slash presence.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Browsers treat URLs with trailing slash as directories

- URLs with trailing slash use './' baseUrl

This ensures assets load correctly regardless of how users access the Swagger UI endpoint.

Fixes nestjs#3617
res.type('text/html');

const url = httpAdapter.getRequestUrl(req);
const hasTrailingSlash = url.endsWith('/');
Copy link

@lukekluky lukekluky Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this issue also occur when you set the path without a trailing slash. For example, SwaggerModule.setup('api', ...) ?

Copy link

@invaderb invaderb Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kamilmysliwiec
Copy link
Member

Could you add corresponding tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants