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

Allow serving static assets from baseDir #108

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,50 @@ fastify.register('@fastify/swagger-ui', {

Note that this behavior is disabled by default in `@fastify/swagger-ui`.

### Bundling

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.
<details>
<summary>Copy files with esbuild</summary>

```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'],
},
}),
],
})
```

</details>

<details>
<summary>Copy files with docker</summary>

```Dockerfile
COPY ./node_modules/@fastify/swagger-ui/static /app/static
```

</details>

#### 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'),
})
```

<a name="license"></a>
## License

Expand Down
2 changes: 1 addition & 1 deletion lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
27 changes: 27 additions & 0 deletions test/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down