Skip to main content

Flask extension to serve md5 hashed assets via manifest file.

Project description

License: MIT PyPI pyversions Platforms PyPI version fury.io GitHub Workflow Status (with event) Coverage

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-cache-manifest-1.0.3.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

flask_cache_manifest-1.0.3-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file flask-cache-manifest-1.0.3.tar.gz.

File metadata

  • Download URL: flask-cache-manifest-1.0.3.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for flask-cache-manifest-1.0.3.tar.gz
Algorithm Hash digest
SHA256 4f8b3c1db7826e2555614c722be901809c7e4f897bf25009bb63baedc74a99c8
MD5 06d03c6854e412aaf125bd13edb3522c
BLAKE2b-256 e0fd588027955545732c8eb98f0aed77ef02cbce74df1e77fb2600eea1d7b1e2

See more details on using hashes here.

File details

Details for the file flask_cache_manifest-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_cache_manifest-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b5bb78c923588243b40fef6b28f5d725f4a3e393717cd1da6f2e5cef3b1e656d
MD5 37d5dcdb36447f3cab284e650d19ed3d
BLAKE2b-256 6c94143f1e673aedc246a8454593fed28da2829e40504d2a6ff2c99edfbc51ed

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page