Flask extension to serve md5 hashed assets.
Project description
Flask-Cache-Manifest
Flask-cache-manifest is a Flask extension to help you serve your md5 hashed assets. Having file hashes in filenames is a popular feature of modern asset bundlers. It's a good strategy for browser cache busting. However, Flask does not provide an easy way to reference those complicated and ever-changing filenames. Flask-cache-manifest lets you reference those assets by leveraging cache_manifest.json
files.
Full Documentation: https://maxdup.github.io/flask-cache-manifest/
Turns:
<link type="text/css" rel="stylesheet"
href="{{ hashed_url_for('static', filename='css/app.css') }}">
into:
<link type="text/css" rel="stylesheet"
href="/static/css/app-d41d8cd98f00b204e9800998ecf8427e.css">
Installation
pip install flask-cache-manifest
Initializing
The extension needs to be loaded alongside your Flask application.
Here's how it's done:
from flask import Flask, Blueprint
from flask_cache_manifest import FlaskCacheManifest
flaskCacheManifest = FlaskCacheManifest()
app = Flask('my-app',
static_folder='dist/static',
static_url_path='/static')
bp = Blueprint('my-blueprint',
__name__,
static_folder='blueprints/static',
static_url_path='/bp/static')
app.register_blueprint(bp)
flaskCacheManifest.init_app(app)
app.run()
NOTE:
Ideally, flaskCacheManifest.init_app
needs to be called after you've registered your blueprints.
Static folders registered after init_app
will not be loaded.
Usage
Flask-cache-manifest adds the hashed_url_for
function for use in your templates.
It is analogous to Flask's url_for. Given the above example and its blueprints,
here's how you would be able to reference your static files in your Jinja templates.
<!-- from the app's static folder -->
<link type="text/css" rel="stylesheet"
href="{{ hashed_url_for('static', filename='css/app.css') }}">
<!-- from the blueprint's static folder -->
<link type="text/css" rel="stylesheet"
href="{{ hashed_url_for('my-blueprint.static', filename='css/app.css') }}">
<!-- from the static folder relative to what is currently being rendered -->
<link type="text/css" rel="stylesheet"
href="{{ hashed_url_for('.static', filename='css/app.css') }}">
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for flask-cache-manifest-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f1995774f152f80dbc7f718f1d6a766a3e0cfeb762a7ea222cf43a2588f29ed |
|
MD5 | 2f3eb34142c7db1ac52dd7fed7a60c34 |
|
BLAKE2b-256 | 400b4bf6d02de7097ddc7c7738bc10d77146064d14b79a37eb8d2a37d723d3c8 |