Skip to content

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

@andreas-soroko

Description

@andreas-soroko

Prerequisites

  • I have written a descriptive issue title
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions