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

Does not build the document on the index.html route #3116

Open
3 of 4 tasks
0iq opened this issue Oct 22, 2024 · 1 comment
Open
3 of 4 tasks

Does not build the document on the index.html route #3116

0iq opened this issue Oct 22, 2024 · 1 comment

Comments

@0iq
Copy link

0iq commented Oct 22, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I believe there is a routing bug in @nestjs/swagger where it doesn't have a proper handler for */index.html, making it fall back to the default Swagger template (Petstore). related file

Minimum reproduction code

https://github.com/nestjs/nest/tree/master/sample/11-swagger

Steps to reproduce

  1. npm i
  2. npm run start:dev
  3. visit http://127.0.0.1:3000/api/ - it works perfectly
    image
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Swagger UI</title>
  <link rel="stylesheet" type="text/css" href="./api/swagger-ui.css" >
  <link rel="icon" type="image/png" href="./api/favicon-32x32.png" sizes="32x32" /><link rel="icon" type="image/png" href="./api/favicon-16x16.png" sizes="16x16" />
  <style>
    /* removed for simplicity...! */
  </style>
</head>

<body>

<svg>
  <!-- removed for simplicity...! -->
</svg>

<div id="swagger-ui"></div>

<script src="./api/swagger-ui-bundle.js"> </script>
<script src="./api/swagger-ui-standalone-preset.js"> </script>
<script src="./api/swagger-ui-init.js"> </script> <!-- 👈 notice the difference on this line -->

<style>
/* removed for simplicity...! */
</style>
</body>
</html>
  1. visit http://127.0.0.1:3000/api/index.html - does not build the document (fallbacks to the Petstore example)
    image
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Swagger UI</title>
    <link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
    <link rel="stylesheet" type="text/css" href="index.css" />
    <link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
    <link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
  </head>

  <body>
    <div id="swagger-ui"></div>
    <script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
    <script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
    <script src="./swagger-initializer.js" charset="UTF-8"> </script> <!-- 👈 notice the difference on this line -->
  </body>
</html>
  1. visit http://127.0.0.1:3000/api/swagger-initializer.js - this is the route where it returns the Petstore example
window.onload = function() {
  window.ui = SwaggerUIBundle({
    url: "https://petstore.swagger.io/v2/swagger.json",
    dom_id: '#swagger-ui',
    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  });
};

Expected behavior

The current route handling logic to determine whether the document should be built and inserted into the HTML response is super strict on the / path and has to be more flexible when the request has the /index.html postfix.

Package version

7.3.0

NestJS version

10.3.2

Node.js version

v20.17.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

This problem has persisted for over two years, has been reported twice as a GitHub issue, and has been discussed in several Discord posts.
Kamil closed #2475 & #1048 with the following quote:

Please clear your browser cache

I hope this well-detailed report convinces you this is not a client-side issue. You can see the differences between the Swagger initializer Javascript code and the HTML response. The minimal reproduction code was taken from the official NestJS samples, so no further changes were made on the developer side to make this happen.

@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants