fastify-swagger-ui icon indicating copy to clipboard operation
fastify-swagger-ui copied to clipboard

asset links are incorrect / x-forwarded-prefix header not respected

Open andreas-soroko opened this issue 1 month ago • 2 comments

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] I have searched existing issues to ensure the bug has not already been reported

Fastify version

5.6.1

Plugin version

5.2.3

Node.js version

22.x

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

26.0.1

Description

Just noticed that the ui is not correctly loading when its behind a reverse proxy. The plugin is configured as follow:

import fastifySwaggerUi from '@fastify/swagger-ui'

fastify.register(fastifySwaggerUi, { routePrefix: '/swagger' })

When i try to visit the url

https://api.something.io/service/swagger

the index site was returned but contains not the prefix, which leads to 404

  <link rel="stylesheet" type="text/css" href="/swagger/static/swagger-ui.css">
  <link rel="stylesheet" type="text/css" href="/swagger/static/index.css">

https://api.something.io/service/swagger/ ( with slash at the end )

the page is loading and the urls are:

  <link rel="stylesheet" type="text/css" href="./static/swagger-ui.css">
  <link rel="stylesheet" type="text/css" href="./static/index.css">

Additional info:

the forward headers are set correct, but looks like the code does not consider them

'x-forwarded-host': 'api.something.io',
'x-forwarded-port': '443',
'x-forwarded-prefix': '/service',
'x-forwarded-proto': 'https'

tried to use indexPrefix with /service which fixed the issue when its behind the proxy, but breaks if you visit directly the service https://service.something.io/swagger

Link to code that reproduces the bug

Expected Behavior

if x-forwarded-prefix is defined, it should be used.

andreas-soroko avatar Oct 08 '25 09:10 andreas-soroko

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina avatar Oct 11 '25 16:10 mcollina

Hello,

Normally, yes—but there are some cases where I can't make sense of why it was done that way (unfortunately, this is not explained in the comments either), and I don't want to just slap my use case on top.

as example:

  • routePrefix
    • can contain /doc which will render as /doc/... - fine
    • when it contains doc it will render doc/... - why it's not normalized to just /doc/.. ?
  • indexPrefix
    • I found another issue that sounds more or less like my problem — but it was solved with this property (which is not ideal - in my opinion because it breaks the local env).
  • hasTrailingSlash
    • don't even understand why this one is needed
    • skips routePrefix and indexPrefix if true

If you can enlighten me, I'll take another look at it. 👍🏼

andreas-soroko avatar Oct 13 '25 19:10 andreas-soroko