Skip to content

Commit

Permalink
perf(swagger-ui): use jsdelivr instead of unpkg to serve swagger-ui a…
Browse files Browse the repository at this point in the history
…ssets (#701)

* perf: use jsdelivr instead of unpkg to serve swagger-ui assets

* format

* add changeset
  • Loading branch information
rwv authored Aug 25, 2024
1 parent f4030cb commit 32abe11
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-snails-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/swagger-ui': patch
---

perf: use jsdelivr instead of unpkg to serve swagger-ui assets
4 changes: 3 additions & 1 deletion packages/swagger-ui/src/swagger/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ type ResourceConfig = {
}

export const remoteAssets = ({ version }: ResourceConfig): AssetURLs => {
const url = `https://unpkg.com/swagger-ui-dist${version !== undefined ? `@${version}` : ''}`
const url = `https://cdn.jsdelivr.net/npm/swagger-ui-dist${
version !== undefined ? `@${version}` : ''
}`

return {
css: [`${url}/swagger-ui.css`],
Expand Down
16 changes: 8 additions & 8 deletions packages/swagger-ui/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('SwaggerUI Rendering', () => {
expect(SwaggerUI({ url }).toString()).toEqual(`
<div>
<div id="swagger-ui"></div>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist/swagger-ui.css" />
<script src="https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui.css" />
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui-bundle.js" crossorigin="anonymous"></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
Expand All @@ -25,8 +25,8 @@ describe('SwaggerUI Rendering', () => {
expect(SwaggerUI({ url, version: '5.0.0' }).toString()).toEqual(`
<div>
<div id="swagger-ui"></div>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.0.0/swagger-ui.css" />
<script src="https://unpkg.com/swagger-ui-dist@5.0.0/swagger-ui-bundle.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.0.0/swagger-ui.css" />
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.0.0/swagger-ui-bundle.js" crossorigin="anonymous"></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
Expand All @@ -51,8 +51,8 @@ describe('SwaggerUI Rendering', () => {
).toEqual(`
<div>
<div id="swagger-ui"></div>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist/swagger-ui.css" />
<script src="https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui.css" />
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui-bundle.js" crossorigin="anonymous"></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
Expand Down Expand Up @@ -89,8 +89,8 @@ describe('SwaggerUI Rendering', () => {
`
<div>
<div id="swagger-ui-manually"></div>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist/swagger-ui.css" />
<script src="https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui.css" />
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui-bundle.js" crossorigin="anonymous"></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
Expand Down
12 changes: 8 additions & 4 deletions packages/swagger-ui/test/remote-assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import { remoteAssets } from '../src/swagger/resource'
describe('remoteAssets', () => {
it('should return default assets when no version is provided', () => {
const assets = remoteAssets({})
expect(assets.css).toEqual(['https://unpkg.com/swagger-ui-dist/swagger-ui.css'])
expect(assets.js).toEqual(['https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js'])
expect(assets.css).toEqual(['https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui.css'])
expect(assets.js).toEqual(['https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui-bundle.js'])
})

it('should return assets with version when version is provided', () => {
const version = '1.2.3'
const assets = remoteAssets({ version })
expect(assets.css).toEqual([`https://unpkg.com/swagger-ui-dist@${version}/swagger-ui.css`])
expect(assets.js).toEqual([`https://unpkg.com/swagger-ui-dist@${version}/swagger-ui-bundle.js`])
expect(assets.css).toEqual([
`https://cdn.jsdelivr.net/npm/swagger-ui-dist@${version}/swagger-ui.css`,
])
expect(assets.js).toEqual([
`https://cdn.jsdelivr.net/npm/swagger-ui-dist@${version}/swagger-ui-bundle.js`,
])
})
})

0 comments on commit 32abe11

Please sign in to comment.