django-hopcdn/README.md
2025-07-03 16:35:08 +02:00

123 lines
2.4 KiB
Markdown
Executable File

# HopCDN Client for Django
**django-hopcdn** is a lightweight Django library that simplifies loading CSS frameworks from your own CDN. It reads a manifest file served by the CDN to resolve the correct hashed filenames and generates `<link>` tags using a simple template tag.
---
## 📚 Table of Contents
* [✅ Features](#✅-features)
* [🛠️ Installation](#🛠️-installation)
* [🚀 Quick Start](#🚀-quick-start)
* [1. Configure settings](#1-configure-settings)
* [2. Use the template tag](#2-use-the-template-tag)
* [⚙️ Notes & Behavior](#⚙️-notes--behavior)
* [📄 License](#📄-license)
---
## ✅ Features
* 📦 Loads hashed/minified CSS from your CDN using a manifest
* 🚀 Simple `{% hopcdn_stylesheet 'name' %}` tag for easy integration
* ⚡ In-memory caching for performance (thread-safe)
* 🛠️ Fallback-safe if manifest is missing or incomplete
* 🧱 Easy to extend or override
---
## 🛠️ Installation
```bash
pip install django-hopcdn
```
Add to `INSTALLED_APPS`:
```python
INSTALLED_APPS = [
...,
'django_hopcdn',
]
```
---
## 🚀 Quick Start
### 1. Configure settings
Add the CDN base URL in your `settings.py`:
```python
CDN_URL = "https://cdn.hopsenn.com/"
```
Your CDN must expose a `manifest.json` at:
```
https://cdn.hopsenn.com/manifest.json
```
Example `manifest.json`:
```json
{
"main": "main.ab12cd.css",
"theme": "theme.f8d33e.css"
}
```
### 2. Use the template tag
In your template:
```django
{% load hopcdn_tags %}
{% hopcdn_stylesheet 'main' %}
{% hopcdn_stylesheet 'theme' %}
```
This will render:
```html
<link rel="stylesheet" href="https://cdn.hopsenn.com/styles/main/main.ab12cd.css">
<link rel="stylesheet" href="https://cdn.hopsenn.com/styles/theme/theme.f8d33e.css">
```
---
## ⚙️ Notes & Behavior
* The manifest is fetched only once per server process and cached in memory.
* If a key is missing or the CDN is unreachable, the tag will output a helpful comment like:
```html
<!-- hopcdn: 'main' not found in manifest -->
```
* The expected file structure on your CDN is:
```
/manifest.json
/styles/<name>/<filename-from-manifest>
```
---
## 📄 License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
---
## 🤝 Contributing
Contributions are welcome! Open issues or pull requests on [Git](https://git.hopsenn.com/hopsenn/django-hopcdn).