Flask-Fingerprint
This project is a lightweight Flask extension for cache-busting static assets via file hash or query parameter.
Installation
It's available from PyPi using pip:
pip install Flask-Fingerprint
Getting started
Using this library is straightforward:
from flask_fingerprint import Fingerprint
# Configuration
config = {
'manifest': 'path/to/manifest.json',
'extensions': ['js', 'css', 'jpg'],
'hash_size': 12,
'query': False,
}
# Option 1
Fingerprint(app, config)
# Option 2
ext = Fingerprint(config=config)
ext.init_app(app)
After that, url_for takes care of cache-busting your static assets auto-magically:
<!-- When enabled, this .. -->
<script src="{{ url_for('static', filename='js/main.js') }}"></script>
<!-- .. becomes this .. -->
<script src="/<static-folder>/js/main.f1l3h4sh.js"></script>
Unless passing a manifest file (as created by third-party tools like gulp-rev) or using query mode (see section 'Configuration'), you need to implement server rules to make this work.
Apache
Place the following snippet inside your .htaccess, right after RewriteBase:
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.([0-9a-z]{8})\.(js|css)$ $1.$3 [L]
Note: The {8} part has to match your hash_size (see section 'Configuration').
NGINX
Place the following snippet anywhere inside your virtual host setup:
location ~ (.+)\.(?:\w+)\.(js|css)$ {
try_files $uri $1.$2;
}
Usage
__init__(app: Flask = None, config: dict = None)
Creates Fingerprint instance.
init_app(app: Flask, config: dict)
See __init__ above.
Note: flask_fingerprint also exposes a logger instance which might prove useful when debugging!
Configuration
When passing a config object (like in the example above), you may use the following options:
manifest: str:
Manifest file, mapping unbusted filenames & their busted revisions. Default: <static_folder>/manifest.json
extensions: list | str:
Allowed extensions. Default: ['css', 'js']
hash_size: int:
Length of file hash. Default: 8
query: bool:
Use query parameter instead of file hash. Default: False
Credits
This extension was inspired by other solutions for cache-busting assets in Flask applications.
Metadata
Release files for Flask-Fingerprint 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| Flask-Fingerprint-0.1.0.tar.gz | 5.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| Flask_Fingerprint-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.9 kB
Release files / Flask-Fingerprint-0.1.0.tar.gz
| Download URL | Flask-Fingerprint-0.1.0.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0acc9451b1c7202eded0374cc13eccd7d29e93aa6ca9e9c88a1f9a63ad296927
|
|
BLAKE2b-256 checksum How to use checksums |
6d056416638edf0144500be03875ad39eaba574b899cb33373b46ce5c6a668a3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.4.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.7
|
Release files / Flask_Fingerprint-0.1.0-py3-none-any.whl
| Download URL | Flask_Fingerprint-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8d86a44f4887d0511eb78d992e6724a172807df20f6aa1c63e242ed63c34f6df
|
|
BLAKE2b-256 checksum How to use checksums |
c40874e6fc6352c24af02defb792c6f4129d3d0540be53e03345e1ebcd0dfd0e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.4.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.7
|