GithubHelp home page GithubHelp logo

escapework / laravel-asset-versioning Goto Github PK

View Code? Open in Web Editor NEW
43.0 10.0 4.0 64 KB

Package to add cache busting on your assets. Made for Laravel.

License: MIT License

PHP 100.00%
laravel-5-package laravel versioning

laravel-asset-versioning's Introduction

Laravel - Assets Versioning

Latest Stable Version Downloads Build Status License MIT

Disclaimer: This package is not really maintained anymore, we recommend you use the Laravel Mix versioning instead.

Have you ever had a problem with cache in your assets? This package may help you.

Version Compatibility

Laravel Laravel Assets Versioning
6.x/7.x/8.x 0.8.x
5.5+ 0.7.x
5.4.x 0.6.x
5.3.x 0.5.x
5.2.x 0.4.x
5.1.x 0.3.x
5.0.x 0.2.x
4.2.x 0.1.x

Installation

Via Composer:

$ composer require escapework/laravel-asset-versioning:"0.7.*"

And publish the configurations running the following command:

$ php artisan vendor:publish --provider="EscapeWork\Assets\AssetsServiceProvider"

Usage

Instead of using the asset helper, you will need to use the Asset:v method.

Imagine that your layout template has the following lines:

<link rel="stylesheet" href="{{ Asset::v('assets/stylesheets/css/main.css') }}">
<script src="{{ Asset::v('assets/javascripts/js/main.js') }}"></script>

In your local environment, nothing changes. But in production, you just need to run the following command every time you need to update your assets' version:

$ php artisan asset:dist

And your layout will be rendered as this:

<link rel="stylesheet" href="/assets/stylesheets/dist/1392745827/main.css" />
<script src="/assets/javascripts/dist/1392745827/main.js"></script>

The version is the timestamp when you performed the asset:dist command.

This package knows which folder you need by the file extension, which is the array key in the config file.

You also can get only the path for some extension:

{{ Asset::path('css') }} <!-- /assets/stylesheets/dist/1392745827 -->

HTTP2 Server Push

You can also enable the HTTP2 Server Push header for all assets used with this package.

For that, you need to add the HTTP2ServerPush to the middlewares of your application.

protected $middleware = [
    \EscapeWork\Assets\Middleware\HTTP2ServerPush::class,
];

And that's it, your response will come with the Link HTTP header.

If you want to add some assets that are not versioned, you can use this method:

Asset::addHTTP2Link('/assets/fonts/robotto.woff', 'font');
Asset::addHTTP2Link('/assets/css/home.css', 'css');
Asset::addHTTP2Link('/assets/js/home.js', 'js');

Configurations

Of course you can configure the folders you need. Just edit the config/assets.php file, in the types array.

'types' => [
    'css' => [
        'origin_dir' => 'your-custom-css-dir/css',
        'dist_dir'   => 'your-custom-css-dir/dist',
    ],

    'js' => [
        'origin_dir' => 'your-custom-js-dir/js',
        'dist_dir'   => 'your-custom-js-dir/dist',
    ],

    'jpg' => [
        'origin_dir' => 'assets/images',
        'dist_dir'   => 'assets/images/dist',
    ],
],

You also can add more folders by adding more items into the array.

Also, you can configure in which environments the assets are gonna be versioned.

'environments' => ['production'],

Changelog

See Changelog.

Unit tests

Just run vendor/bin/phpunit.

License

See the License file.

laravel-asset-versioning's People

Contributors

arseto avatar ehkasper avatar luisdalmolin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-asset-versioning's Issues

Add config option to enable/disable versioning

Instead of checking if the environment is local, just add a config option to enable or disable the versioning. Then it will be more flexible and not assume that everybody uses the local environment. Users can then use Laravel's config system to configure which environments should use versioning.

L5, cache:clear triggers event that generate error "Class artisan does not exist"

The following code generates an error:

File: src/AssetsServiceProvider.php

$this->app['events']->listen('cache:cleared', function() use($app) {
    $app['artisan']->call('asset:dist');
});

To reproduce this bug, call the command: php artisan cache:clear with enabled AssetsServiceProvider

This fix solves the issue:

$this->app['events']->listen('cache:cleared', function() use($app) {
    \Artisan::call('asset:dist');
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.