Developed with love by KnpLabs Hire us for your project!
0

hashed-asset-bundle

by Incenteev

Apply an asset version based on a hash of the asset for symfony/asset

HashedAssetBundle

The HashedAssetBundle provides an asset version strategy which uses a hash
of the file content as asset version. This allows bumping the asset version
separately for each asset (automatically).

Build Status Total Downloads Latest Stable Version

Installation

Use Composer to install the bundle:

$ composer require incenteev/hashed-asset-bundle

Usage

Register the bundle in the kernel:

// app/AppKernel.php

// ...

class AppKernel extends Kernel {
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Incenteev\HashedAssetBundle\IncenteevHashedAssetBundle(),
        );
    }
}

Then configure FrameworkBundle to use the new version strategy:

framework:
    assets:
        version_strategy: incenteev_hashed_asset.strategy

If you are using Symfony <3.1, this configuration setting is not available.
Here is the workaround:

# app/config/config.yml
framework:
    assets:
        version: dummy # set a dummy version so that the package does not use the empty version

services:
    assets._version__default:
        alias: incenteev_hashed_asset.strategy
# If you use additional packages, you may need to create additional aliases for other packages than `_default`

Advanced configuration

The default configuration should fit common needs, but the bundle exposes
a few configuration settings in case you need them:

incenteev_hashed_asset:
    # Absolute path to the folder in which assets can be found
    # Note: in case you apply a base_path in your asset package, it is not
    # yet applied to the string received by the bundle
    web_root: '%kernel.root_dir%/../web'
    # Format used to apply the version. This is equivalent to the
    # `framework > assets > version_format` of the static version strategy
    # of FrameworkBundle.
    version_format: '%%s?%%s'

License

This bundle is under the MIT license.

Alternative projects

If you want to apply cache busting by renaming files in your asset pipeline
(for instance with the gulp-rev plugin), have a look at
https://github.com/irozgar/gulp-rev-versions-bundle which processes a
manifest file.

Reporting an issue or a feature request

Issues and feature requests are tracked in the Github issue tracker.

Copyright (c) 2017 Incenteev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.