PHPackages                             spekkionu/assetcachebuster - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. spekkionu/assetcachebuster

ActiveLibrary[Caching](/categories/caching)

spekkionu/assetcachebuster
==========================

Prefixes asset urls with a unique hash which will allow invalidation of asset files cached by the browser.

v3.4.0(5y ago)3243.2k↓100%7[2 issues](https://github.com/spekkionu/laravel-assetcachebuster/issues)MITPHPPHP &gt;=7.0

Since Dec 6Pushed 5y ago5 watchersCompare

[ Source](https://github.com/spekkionu/laravel-assetcachebuster)[ Packagist](https://packagist.org/packages/spekkionu/assetcachebuster)[ RSS](/packages/spekkionu-assetcachebuster/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (18)Used By (0)

[![Build Status](https://camo.githubusercontent.com/116005bf03b64468c9ff74eec5d5f1da7bbebfc14b246bb4c9f56957360d52ec/68747470733a2f2f7472617669732d63692e6f72672f7370656b6b696f6e752f6c61726176656c2d617373657463616368656275737465722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/spekkionu/laravel-assetcachebuster)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/4b29e7ee03bc7a9a39e61294756eb5bed225a488147c86962c2337936d2aa9dd/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7370656b6b696f6e752f6c61726176656c2d617373657463616368656275737465722f6261646765732f7175616c6974792d73636f72652e706e673f733d33663865363865633561393033636265363933346665366163666334306330613735316563336636)](https://scrutinizer-ci.com/g/spekkionu/laravel-assetcachebuster/)[![Code Coverage](https://camo.githubusercontent.com/59bd2bc2e9c75d87f6de9552b799d98653b54a59bb99b22128b8ea810eb8be2f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7370656b6b696f6e752f6c61726176656c2d617373657463616368656275737465722f6261646765732f636f7665726167652e706e673f733d36343636646461346431366630343235363432333434353633343735383161636137636165396463)](https://scrutinizer-ci.com/g/spekkionu/laravel-assetcachebuster/)

Asset Cache Buster
==================

[](#asset-cache-buster)

This package prefixes asset urls with md5 hashes so that changing the hash results in the url for all asset files to change. This forces browsers to download new versions of the asset files before the browser cache is expired.

This is especially useful when using a CDN that sets far future expires headers on files.

It works with any static files like stylesheets, javascript files, and images.

Installation
============

[](#installation)

The 2.x branch is only compatible with Laravel 5.x. If you need Laravel 4.x compatibility use the 1.x branch.

Add `spekkionu\assetcachebuster` as a requirement with composer:

```
composer require spekkionu\assetcachebuster
```

If you have disabled automatic package discovery you will need to register the service provider with the application. Open up `config/app.php` and find the `providers` key.

```
'providers' => array(
    Spekkionu\Assetcachebuster\AssetcachebusterServiceProvider::class,
)
```

If you have disabled automatic package discovery you will need to register the facade in order to generate an asset url. You can register the facade via the `aliases` key of your `config/app.php` file.

```
'aliases' => array(
    'Asset' => Spekkionu\Assetcachebuster\Facades\Cachebuster::class
)
```

There are other packages that want to register the Asset facade. If this is the case you can change the Asset key to be something else to prevent a collision.

For the asset urls to function the following will need to be added to the apache .htaccess file. Add the following to your .htaccess file **before** the Laravel rewrite rule:

```
# ------------------------------------------------------------------------------
# | Filename-based cache busting                                               |
# ------------------------------------------------------------------------------

# Rewrite assets/hash/file.js to assets/file.js

    RewriteEngine On

    # Remove prefix from asset files
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^[a-f0-9]{32}/(.*)$ $1 [L]

```

For Nginx, add the following to your virtual host file

```
# ------------------------------------------------------------------------------
# | Filename-based cache busting                                               |
# ------------------------------------------------------------------------------
# Rewrite assets/hash/file.js to assets/file.js
location ~* "^\/[a-f0-9]{32}(\/*\.*(.*))$" {
    try_files $uri $1;
}
```

Configuration
=============

[](#configuration)

In order to generate new hashes to invalidate the cache you must publish the package configuration by running the following artisan command.

```
php artisan vendor:publish --provider="Spekkionu\Assetcachebuster\AssetcachebusterServiceProvider" --tag=config

```

This will create a config file at `config/assetcachebuster.php`Use this file to configure the package.

Set the `enabled` flag in the config to `true` in order for asset urls to be prefixed.

Usage
=====

[](#usage)

For any asset urls you want to be able to cache you must use the `Asset::url($url)` facade rather than directly outputting the url. For example if you wanted to link to a stylesheet located at `/css/stylesheet.css` you would add the following to your view

```
