https://github.com/waketzheng/fastapi-cdn-host
Auto choose the fastest CDN host for swagger-ui in /docs.
https://github.com/waketzheng/fastapi-cdn-host
fastapi
Last synced: 5 months ago
JSON representation
Auto choose the fastest CDN host for swagger-ui in /docs.
- Host: GitHub
- URL: https://github.com/waketzheng/fastapi-cdn-host
- Owner: waketzheng
- License: mit
- Created: 2023-12-03T10:12:08.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-11-28T10:11:15.000Z (7 months ago)
- Last Synced: 2025-11-28T11:52:43.723Z (7 months ago)
- Topics: fastapi
- Language: Python
- Homepage:
- Size: 3.78 MB
- Stars: 16
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# FastAPI CDN host Selector for docs ui

[](https://pypi.python.org/pypi/fastapi-cdn-host)
[](https://github.com/waketzheng/fastapi-cdn-host/actions?query=workflow:ci)
[](https://coveralls.io/github/waketzheng/fastapi-cdn-host?branch=main)
[](https://github.com/astral-sh/ruff)
[](https://github.com/PyCQA/bandit)
Auto find swagger-ui in local files, if exist use them.
Otherwise make concurrent http requests by httpx to find out which third part cdn host(cdn.jsdelivr.net/unpkg.com/cdnjs.cloudflare.com/cdn.staticfile.org) is the fastest one.
**English** | [中文](./README.zh.md)
## Install
```bash
pip install fastapi-cdn-host
```
## Usage
```py
import fastapi_cdn_host
from fastapi import FastAPI
app = FastAPI()
# include_routes ...
fastapi_cdn_host.patch_docs(app)
```
See more at:
- [examples/](https://github.com/waketzheng/fastapi-cdn-host/tree/main/examples)
- [tests/](https://github.com/waketzheng/fastapi-cdn-host/tree/main/tests)
## Detail
1. Let's say that the default docs CDN host https://cdn.jsdelivr.net is too slow in your network, while unpkg.com is much faster.
```py
import fastapi_cdn_host
from fastapi import FastAPI
app = FastAPI()
fastapi_cdn_host.patch_docs(app) # Will use `unpkg.com`(or other faster host) to replace the `cdn.jsdelivr.net/npm`
```
2. To support offline docs/, put swagger-ui asset files into local directory named `static`
```py
from pathlib import Path
fastapi_cdn_host.patch_docs(app, Path(__file__).parent.joinpath('static'))
```
This get the same result of the example in official document:
https://fastapi.tiangolo.com/how-to/custom-docs-ui-assets/?h=static#self-hosting-javascript-and-css-for-docs
3. If asset files are ready in private cdn
```py
from fastapi_cdn_host import AssetUrl
fastapi_cdn_host.patch_docs(
app,
cdn_host=AssetUrl(
js='http://my-cdn.com/swagger-ui.js',
css='http://my-cdn.com/swagger-ui.css',
redoc='http://my-cdn.com/redoc.standalone.js',
favicon='http://my-cdn.com/favicon.ico',
)
)
```
## License
[MIT](./LICENSE)