From 1c1ccbdbcc433b5681ae18be16c4c81bceea4185 Mon Sep 17 00:00:00 2001 From: David Jebing <29679909+davidjbng@users.noreply.github.com> Date: Wed, 6 Dec 2023 12:26:36 +0100 Subject: [PATCH 1/4] feat: serve static files from baseDir --- lib/routes.js | 2 +- test/route.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/routes.js b/lib/routes.js index 3e5ae45..e8ca8ce 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -202,7 +202,7 @@ function fastifySwagger (fastify, opts, done) { // serve swagger-ui with the help of @fastify/static fastify.register(fastifyStatic, { - root: path.join(__dirname, '..', 'static'), + root: opts.baseDir || path.join(__dirname, '..', 'static'), prefix: staticPrefix, decorateReply: false }) diff --git a/test/route.test.js b/test/route.test.js index 08e9051..3907522 100644 --- a/test/route.test.js +++ b/test/route.test.js @@ -373,6 +373,33 @@ test('/documentation/static/:file should send back the correct file', async (t) } }) +test('/documentation/static/:file should send back file from baseDir', async (t) => { + t.plan(2) + const fastify = Fastify() + + const uiConfig = { + baseDir: resolve(__dirname, '..', 'examples', 'static') + } + + await fastify.register(fastifySwagger, swaggerOption) + await fastify.register(fastifySwaggerUi, uiConfig) + + { + const res = await fastify.inject({ + method: 'GET', + url: '/documentation/static/example-logo.svg' + }) + t.equal(res.statusCode, 200) + t.equal( + res.payload, + readFileSync( + resolve(__dirname, '..', 'examples', 'static', 'example-logo.svg'), + 'utf8' + ) + ) + } +}) + test('/documentation/static/:file 404', async (t) => { t.plan(2) const fastify = Fastify() From 6926403f4705eb7c723b6b35123422fc87c46358 Mon Sep 17 00:00:00 2001 From: David Jebing <29679909+davidjbng@users.noreply.github.com> Date: Wed, 6 Dec 2023 12:57:22 +0100 Subject: [PATCH 2/4] docs: how to use swagger-ui with bundling --- README.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/README.md b/README.md index b33950c..5eee023 100644 --- a/README.md +++ b/README.md @@ -319,6 +319,51 @@ fastify.register('@fastify/swagger-ui', { Note that this behavior is disabled by default in `@fastify/swagger-ui`. +### Bundling + +If you want to bundle Swagger UI with your application, you need to copy the swagger-ui static files to your server yourself and set the `baseDir` option to point to your folder. + +
+Copy files with esbuild + +```js +import { build } from 'esbuild' +import { copy } from 'esbuild-plugin-copy' + +await build({ + // ... + plugins: [ + copy({ + resolveFrom: 'cwd', + assets: { + from: ['node_modules/@fastify/swagger-ui/static/*'], + to: ['dist/static'], + }, + }), + ], +}) +``` + +
+ +
+Copy files with docker + +```Dockerfile +COPY ./node_modules/@fastify/swagger-ui/static /app/static +``` + +
+ +#### Configure Swagger UI to use a custom baseDir +Set the `baseDir` option to point to your folder. + +```js +await fastify.register(require('@fastify/swagger-ui'), { + baseDir: isDev ? undefined : path.resolve('static'), +}) +``` + ## License From 8dda4f57e1c4feac5536810364ba849b0c86d6de Mon Sep 17 00:00:00 2001 From: David Jebing <29679909+davidjbng@users.noreply.github.com> Date: Fri, 8 Dec 2023 10:04:58 +0100 Subject: [PATCH 3/4] fix lint: no-trailing-spaces --- test/route.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/route.test.js b/test/route.test.js index 3907522..a55c19a 100644 --- a/test/route.test.js +++ b/test/route.test.js @@ -376,7 +376,7 @@ test('/documentation/static/:file should send back the correct file', async (t) test('/documentation/static/:file should send back file from baseDir', async (t) => { t.plan(2) const fastify = Fastify() - + const uiConfig = { baseDir: resolve(__dirname, '..', 'examples', 'static') } From 7956f8471574b65830f507f76ab887fceba164b0 Mon Sep 17 00:00:00 2001 From: David Jebing <29679909+davidjbng@users.noreply.github.com> Date: Wed, 13 Dec 2023 09:01:27 +0100 Subject: [PATCH 4/4] docs: update wording about bundling Co-authored-by: Frazer Smith --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 5eee023..1ae2e17 100644 --- a/README.md +++ b/README.md @@ -321,8 +321,7 @@ Note that this behavior is disabled by default in `@fastify/swagger-ui`. ### Bundling -If you want to bundle Swagger UI with your application, you need to copy the swagger-ui static files to your server yourself and set the `baseDir` option to point to your folder. - +To bundle Swagger UI with your application, the swagger-ui static files need to be copied to the server and the `baseDir` option set to point to the file directory.
Copy files with esbuild