fastify / fastify/fastify-swagger-ui

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

Open
#240 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
191
Forks
46
Avg merge
12h 12m
Merged PRs (30d)
2

Description

### 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:

```ts
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

```html


```

#### `https://api.something.io/service/swagger/` ( with slash at the end )
the page is loading and the urls are:
```html


```

#### Additional info:
the forward headers are set correct, but looks like the [code](https://github.com/fastify/fastify-swagger-ui/blob/e23e06d62776e68b138a200e221da745dada1ff9/lib/index-html.js#L10) 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.

Contributor guide

Open the contributing guide

Research direction

Start with lib/index-html.js, which the issue identifies as the source of the generated asset links, and reproduce the two /service/swagger URLs with the listed forwarded headers. Done means the generated links include x-forwarded-prefix when it is present while direct access at /swagger continues to work; add or update coverage where the existing behavior is tested.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.