-
Notifications
You must be signed in to change notification settings - Fork 293
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
cant use with swagger #406
Comments
Hello @yeli19950109 Can you create an example that fails with |
I'm experiencing the same issue, the path to the Swagger UI assets is no longer correct after building the app with After some playing around, however, I've seem to found a workaround: Serving the // ...
SwaggerModule.setup('/swagger', app, document);
app.useStaticAssets(join(__dirname, '/static'), {
prefix: '/swagger',
});
// ... Plus, I had to install This all works for now, but surely is not a very pretty solution ... |
The real issue is probably how swagger-ui itself gets built and served, and not necessarily how |
Any updates on this one ?? p.s: @dominique-mueller I have tried your proposed solution and it seems that I might be missing something out. Could you be kind enough to help me out with this issue ?? |
@mohammadzainabbas Sure, I took the time to prepare a repository here: https://github.com/dominique-mueller/ncc-nestjs-swagger-experiment. It's a working example, so you can clone it and play around with it. I also wrote a quick explanation in the README about what has to be done to get Swagger working in an existing ncc-NestJS project. Hope it helps! |
I have issues with |
So my understanding of the issue is that swagger-ui works like this:
"normal" build (i.e. without ncc)installed packages
dependencies
RuntimeWhen the server is running and the browser visits the swagger URL (e.g.
"ncc" build:installed packagesNone: in this case we don't have access to RuntimeSo when we don't do anything special the swagger-html page will not work, because the requests for the FixTo fix this we must somehow provide these files. This requires 2 steps
copy filesThis depends on your build tools: e.g. we use angular-cli, thus we can simply use assets to copy the relevant files, e.g. "assets": [
{
"glob": "**/*.{js,css,html,png}",
"input": "./node_modules/swagger-ui-dist/",
"output": "./dist/assets/swagger-ui-dist/"
},
....
] Where serve filesWhen we use NestJs we can use this: app.useStaticAssets(path.join(__dirname, 'assets/swagger-ui-dist/'), {
prefix: '/swagger'
}); where Why installing "fastify-swagger" seems to workThis refers to the explanation in the ncc-nestjs-swagger-setup test project I guess the reason is this: When ncc analyses our code, it finds And thus it will copy these files to the I think this may not work reliably, because there is no guarantee that the So I think it's better to
|
I was experiencing the same issue. |
I wrote a patch to solve this problem. use https://unpkg.com/[email protected]/ as publicUrl diff --git a/node_modules/@nestjs/swagger/dist/swagger-ui/constants.js b/node_modules/@nestjs/swagger/dist/swagger-ui/constants.js
index 6f47648..8776139 100644
--- a/node_modules/@nestjs/swagger/dist/swagger-ui/constants.js
+++ b/node_modules/@nestjs/swagger/dist/swagger-ui/constants.js
@@ -1,8 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.jsTemplateString = exports.htmlTemplateString = exports.favIconHtml = void 0;
-exports.favIconHtml = '<link rel="icon" type="image/png" href="<% baseUrl %>favicon-32x32.png" sizes="32x32" />' +
- '<link rel="icon" type="image/png" href="<% baseUrl %>favicon-16x16.png" sizes="16x16" />';
+exports.favIconHtml = '<link rel="icon" type="image/png" href="<% publicUrl %>favicon-32x32.png" sizes="32x32" />' +
+ '<link rel="icon" type="image/png" href="<% publicUrl %>favicon-16x16.png" sizes="16x16" />';
exports.htmlTemplateString = `
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
@@ -10,7 +10,7 @@ exports.htmlTemplateString = `
<head>
<meta charset="UTF-8">
<title><% title %></title>
- <link rel="stylesheet" type="text/css" href="<% baseUrl %>swagger-ui.css" >
+ <link rel="stylesheet" type="text/css" href="<% publicUrl %>swagger-ui.css" >
<% favIconString %>
<style>
html
@@ -71,8 +71,8 @@ exports.htmlTemplateString = `
<div id="swagger-ui"></div>
-<script src="<% baseUrl %>swagger-ui-bundle.js"> </script>
-<script src="<% baseUrl %>swagger-ui-standalone-preset.js"> </script>
+<script src="<% publicUrl %>swagger-ui-bundle.js"> </script>
+<script src="<% publicUrl %>swagger-ui-standalone-preset.js"> </script>
<script src="<% baseUrl %>swagger-ui-init.js"> </script>
<% customJs %>
<% customJsStr %>
diff --git a/node_modules/@nestjs/swagger/dist/swagger-ui/swagger-ui.js b/node_modules/@nestjs/swagger/dist/swagger-ui/swagger-ui.js
index c067725..d60f616 100644
--- a/node_modules/@nestjs/swagger/dist/swagger-ui/swagger-ui.js
+++ b/node_modules/@nestjs/swagger/dist/swagger-ui/swagger-ui.js
@@ -55,6 +55,7 @@ function buildSwaggerHTML(baseUrl, swaggerDoc, customOptions = {}) {
.replace('<% explorerCss %>', explorerCss)
.replace('<% favIconString %>', favIconString)
.replace(/<% baseUrl %>/g, baseUrl)
+ .replace(/<% publicUrl %>/g, 'https://unpkg.com/[email protected]/')
.replace('<% customJs %>', toTags(customJs, toExternalScriptTag))
.replace('<% customJsStr %>', toTags(customJsStr, toInlineScriptTag))
.replace('<% customCssUrl %>', toTags(customCssUrl, toExternalStylesheetTag)) Patch file |
My swagger UI now shows with the correct CSS/JS after relocating it. But it still shows up as with the example "Petstore". Is there some kind of document (json) that's missing? |
cant use with swagger for nestjs
The text was updated successfully, but these errors were encountered: