Skip to content

fix: resolve static asset loading with trailing slash#3618

Merged
kamilmysliwiec merged 2 commits intonestjs:masterfrom
spandios:fix/trailing-slash-static-assets
Jan 14, 2026
Merged

fix: resolve static asset loading with trailing slash#3618
kamilmysliwiec merged 2 commits intonestjs:masterfrom
spandios:fix/trailing-slash-static-assets

Conversation

@spandios
Copy link
Copy Markdown
Contributor

@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
Copy Markdown

@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
Copy Markdown

@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
Copy Markdown
Member

Could you add corresponding tests?

@kamilmysliwiec
Copy link
Copy Markdown
Member

Ping

@BitForger
Copy link
Copy Markdown

Bump, this is causing a problem for our docs pages. Is this okay to be merged?

@kamilmysliwiec kamilmysliwiec merged commit e17b9ff into nestjs:master Jan 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants