Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with index html resources paths when using a complex path as route prefix #180

Closed
2 tasks done
jpb06 opened this issue Oct 17, 2024 · 17 comments
Closed
2 tasks done

Comments

@jpb06
Copy link

jpb06 commented Oct 17, 2024

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.0.0

Plugin version

5.1.0

Node.js version

20.18

Operating system

macOS

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

15.0.1

Description

It's appears #178 is causing an issue when using a complex path in routePrefix, ie:

await this.instance.register(fastifySwaggerUI, {
  routePrefix: '/api/vi/$docs'
});

The resolved path looks like /api/v1/api/v1/$docs/static in that case.

image

See linked repo for MRE.

Link to code that reproduces the bug

https://github.com/jpb06/fastify-swagger-ui-routeprefix-issue-repro

Expected Behavior

index resources to load properly.

@mcollina
Copy link
Member

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

@jpb06
Copy link
Author

jpb06 commented Oct 18, 2024

Hello.

I spent some time yesterday trying to solve the issue while retaining the behavior wanted by @DavidTanner. Sadly I'm needed elsewhere so I won't have much time to investigate further.

@semy
Copy link

semy commented Nov 2, 2024

Please can you fix that? Have the same problem:
Instead of
const prefix = hasTrailingSlash ? .${opts.staticPrefix}:${hasLeadingSlash ? '.' : ''}${opts.prefix}${opts.staticPrefix}``

should be something like that...
const prefix = hasTrailingSlash ? .${opts.staticPrefix}:${hasLeadingSlash ? '' : '/'}${opts.prefix}${opts.staticPrefix}``

Sorry, but the code is pretty ambiguous anyway, I would generally go over it...

@ronielli
Copy link

ronielli commented Nov 4, 2024

How do I solve this problem, I still can't?

@NoisyFlowers
Copy link

I'm seeing the same problem.

Specifying

routePrefix: "/api/v1/help

and hitting localhost:5000/api/v1/help with browser yields a blank page.

Inspecting the network activity, the initial GET on http://localhost:5000/api/v1/help works, but the ensuing resource GETs all receive 404s because their paths look like this

/api/v1/api/v1/help/static/swagger-ui-bundle.js

Interestingly, adding a trailing "/" (e.g. localhost:5000/api/v1/help/) fixes the problem.

@DavidTanner
Copy link
Contributor

Correct me if I'm wrong, but it kind of seems like this is a bad setup of the server. Can someone please post a test example? I'm going to try recreating in the project as well.

My first thought is that the fastify instance is already using that route prefix, and then you are specifying it again in the swagger config. Again, that is my first thought and I could totally be wrong.

DavidTanner added a commit to DavidTanner/fastify-swagger-ui that referenced this issue Nov 6, 2024
@DavidTanner
Copy link
Contributor

I figured out the issue, and will create a PR to fix. I need to add a new parameter to specify a prefix that only affects the index.html file.

DavidTanner added a commit to DavidTanner/fastify-swagger-ui that referenced this issue Nov 6, 2024
@ronielli
Copy link

Good morning, everyone. Do you have an estimated time to approve this pull request?

@Fdawgs Fdawgs linked a pull request Nov 15, 2024 that will close this issue
4 tasks
rozzilla pushed a commit to rozzilla/fastify-swagger-ui that referenced this issue Dec 3, 2024
@rozzilla
Copy link
Contributor

rozzilla commented Dec 3, 2024

Good morning, everyone. Do you have an estimated time to approve this pull request?

@ronielli new PR merged, just now waiting for the new release 🚀

@rozzilla
Copy link
Contributor

rozzilla commented Dec 3, 2024

FYI => #189

@mcollina mcollina closed this as completed Dec 3, 2024
@ronielli
Copy link

ronielli commented Dec 4, 2024

@rozzilla @DavidTanner Thank you so much! I just tested it, and everything is working perfectly. Great job! 😊

@jpb06
Copy link
Author

jpb06 commented Dec 5, 2024

Resolved on our side as well, thank you!

@MikaKarjunen
Copy link

The previous version worked exactly as intended using relative paths. Now, I don’t quite understand how we’re supposed to proceed with this solution. If the application is run during development at, say, localhost:3000/help, and on the server at foo.com/myapi/v1/help, how can this combination be made to work? I don’t think it makes sense for the application to dictate where it is essentially 'parked' in terms of routing. Or am I missing something? I guess I am, could you fellows push me to a right direction?

@DavidTanner
Copy link
Contributor

@MikaKarjunen I have that same issue with dev vs production. I have an environment variable that I key off of for production that sets the prefix when deploying.

My first suggestion would be to set the indexPrefix to v1 using some env variable that is only set in the deployed environments.

@MikaKarjunen
Copy link

@MikaKarjunen I have that same issue with dev vs production. I have an environment variable that I key off of for production that sets the prefix when deploying.

My first suggestion would be to set the indexPrefix to v1 using some env variable that is only set in the deployed environments.

This is exactly what I was afraid of. We can make it work like this but it's not exactly what I would've liked to hear.

@DavidTanner
Copy link
Contributor

You get my wheels spinning. We could propose an additional config option for the index to use relative paths.

@MikaKarjunen
Copy link

MikaKarjunen commented Dec 20, 2024

You get my wheels spinning. We could propose an additional config option for the index to use relative paths.

That would solve this nicely David. We'd much appreciate it and I suppose so would other people as well. Ultimately, the best outcome would be one where the installation process doesn’t need to inform the application about how it has been routed.

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 a pull request may close this issue.

8 participants