Make possible to use hashed static assets generated by tools like Gulp or Webpack
Project description
===========================
RevAssets
===========================
.. image:: https://travis-ci.org/jpscaletti/rev-assets.svg?branch=master
:target: https://travis-ci.org/jpscaletti/rev-assets
:alt: Build Status
You care about the performance of your site, so you've configured the web server to cache all your assets for a long time. The most used way to bypass that cache when deploying a new version, is to add a hash of the assets to their names.
::
'scripts/home.js' --> 'scripts/home.1a23b.js'
'styles/home.css' --> 'styles/home.aef45.css'
The problem is, now your Python web app can't find the file unless you manually –and painstakingly— update all the URLs in the templates.
.. code:: html+jinja
<script src="{{ url_for('static', filename='scripts/home.js') }}></script>
<link rel="stylesheet" href="{{ url_for('static', filename='styles/home.css') }}</script>
Whit this library, there is no need for that. Just change your templates to:
.. code:: html+jinja
<script src="{{ 'scripts/home.js' | asset_url }}></script>
<link rel="stylesheet" href="{{ 'styles/home.css' | asset_url }}</script>
and use this code:
.. code:: python
# app.py
from flask import Flask, render_template
from rev_assets import RevAssets
app = flask.Flask(__name__)
rev = RevAssets(reload=app.debug)
app.jinja_env.filters['asset_url'] = rev.asset_url
@app.route('/')
def index():
return render_template('index.html')
and it will work for every version of the assets that you build.
This works by reading the ``manifest.json`` generated by the revision tool (so don't forget to configure your task runner to make one).
You can continue to use the old method to link un-versioned assets, like ``favicon.ico`` and others like it.
Note that *this is not a Flask extension*, but a Python library. You can use it with any other framework. You can also have many instances of ``RevAssets`` linked to differents manifests.
Run the tests
======================
We use some external dependencies, listed in ``requirements_tests.txt``::
$ pip install -r requirements-tests.txt
$ python setup.py develop
To run the tests in your current Python version do::
$ make test
To run them in every supported Python version do::
$ tox
Our test suite `runs continuously on Travis CI <https://travis-ci.org/jpscaletti/rev-assets>`_ with every update.
Contributing
======================
#. Check for `open issues <https://github.com/jpscaletti/rev-assets/issues>`_ or open
a fresh issue to start a discussion around a feature idea or a bug.
#. Fork the `RevAssets repository on Github <https://github.com/jpscaletti/rev-assets>`_
to start making your changes.
#. Write a test which shows that the bug was fixed or that the feature works
as expected.
#. Send a pull request and bug the maintainer until it gets merged and published.
:) Make sure to add yourself to ``AUTHORS``.
______
:copyright: `Juan-Pablo Scaletti <http://jpscaletti.com/>`_.
:license: BSD-3-Clause, see LICENSE.
RevAssets
===========================
.. image:: https://travis-ci.org/jpscaletti/rev-assets.svg?branch=master
:target: https://travis-ci.org/jpscaletti/rev-assets
:alt: Build Status
You care about the performance of your site, so you've configured the web server to cache all your assets for a long time. The most used way to bypass that cache when deploying a new version, is to add a hash of the assets to their names.
::
'scripts/home.js' --> 'scripts/home.1a23b.js'
'styles/home.css' --> 'styles/home.aef45.css'
The problem is, now your Python web app can't find the file unless you manually –and painstakingly— update all the URLs in the templates.
.. code:: html+jinja
<script src="{{ url_for('static', filename='scripts/home.js') }}></script>
<link rel="stylesheet" href="{{ url_for('static', filename='styles/home.css') }}</script>
Whit this library, there is no need for that. Just change your templates to:
.. code:: html+jinja
<script src="{{ 'scripts/home.js' | asset_url }}></script>
<link rel="stylesheet" href="{{ 'styles/home.css' | asset_url }}</script>
and use this code:
.. code:: python
# app.py
from flask import Flask, render_template
from rev_assets import RevAssets
app = flask.Flask(__name__)
rev = RevAssets(reload=app.debug)
app.jinja_env.filters['asset_url'] = rev.asset_url
@app.route('/')
def index():
return render_template('index.html')
and it will work for every version of the assets that you build.
This works by reading the ``manifest.json`` generated by the revision tool (so don't forget to configure your task runner to make one).
You can continue to use the old method to link un-versioned assets, like ``favicon.ico`` and others like it.
Note that *this is not a Flask extension*, but a Python library. You can use it with any other framework. You can also have many instances of ``RevAssets`` linked to differents manifests.
Run the tests
======================
We use some external dependencies, listed in ``requirements_tests.txt``::
$ pip install -r requirements-tests.txt
$ python setup.py develop
To run the tests in your current Python version do::
$ make test
To run them in every supported Python version do::
$ tox
Our test suite `runs continuously on Travis CI <https://travis-ci.org/jpscaletti/rev-assets>`_ with every update.
Contributing
======================
#. Check for `open issues <https://github.com/jpscaletti/rev-assets/issues>`_ or open
a fresh issue to start a discussion around a feature idea or a bug.
#. Fork the `RevAssets repository on Github <https://github.com/jpscaletti/rev-assets>`_
to start making your changes.
#. Write a test which shows that the bug was fixed or that the feature works
as expected.
#. Send a pull request and bug the maintainer until it gets merged and published.
:) Make sure to add yourself to ``AUTHORS``.
______
:copyright: `Juan-Pablo Scaletti <http://jpscaletti.com/>`_.
:license: BSD-3-Clause, see LICENSE.
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
rev-assets-1.0.3.tar.gz
(5.0 kB
view details)
Built Distribution
File details
Details for the file rev-assets-1.0.3.tar.gz
.
File metadata
- Download URL: rev-assets-1.0.3.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57945e3d13968a704bb11d622703a001c5fd762dc4af2cb3cca293f5ffea801f |
|
MD5 | a4af13273612041b8e460175cc2be910 |
|
BLAKE2b-256 | 45624ca6a3300f454865da840f3b01766d60b9dc38117c4d942755aebfd033ab |
File details
Details for the file rev_assets-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: rev_assets-1.0.3-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d5e0f292d740973ca5d335118268ee4d79e97257123bf5d17ecede153d4669c |
|
MD5 | d2a7e5b10a7c6f064fbe8b40afa779b1 |
|
BLAKE2b-256 | cfff2bde4a6a7172b7411c727d9845732013fa54875bf6560daf6656584b4ae1 |