All Projects → clubstudioltd → Craft Asset Rev

clubstudioltd / Craft Asset Rev

Licence: mit
A Craft CMS plugin to help with cache busting

Projects that are alternatives of or similar to Craft Asset Rev

Upper
Integrates Edge Caches like Fastly, KeyCDN, Cloudflare and Varnish with Craft.
Stars: ✭ 89 (-18.35%)
Mutual labels:  craftcms, craft-plugin
Craft.doxter
Markdown editor and parser for Craft CMS
Stars: ✭ 80 (-26.61%)
Mutual labels:  craftcms, craft-plugin
Craft Quick Field
Create fields on the fly while designing field layouts
Stars: ✭ 104 (-4.59%)
Mutual labels:  craftcms, craft-plugin
Craft.patrol
Patrol simplifies SSL and maintenance routing for sites built with Craft
Stars: ✭ 91 (-16.51%)
Mutual labels:  craftcms, craft-plugin
Guest Entries
Accept anonymous entry submissions with Craft.
Stars: ✭ 100 (-8.26%)
Mutual labels:  craftcms, craft-plugin
Instantanalytics
DEPRECATED Instant Analytics brings full Google Analytics support to your Twig templates and automatic Craft Commerce integration with Google Enhanced Ecommerce.
Stars: ✭ 103 (-5.5%)
Mutual labels:  craftcms, craft-plugin
Craft User Manual
📚 Help Section Plugin for Craft CMS.
Stars: ✭ 83 (-23.85%)
Mutual labels:  craftcms, craft-plugin
Commercemailer
ARCHIVED: For Craft Commerce. Makes it easy to create forms for sending carts, products and orders by email.
Stars: ✭ 8 (-92.66%)
Mutual labels:  craftcms, craft-plugin
Craft Brief
Quick, easy, and customizable user-group notifications for Craft CMS.
Stars: ✭ 47 (-56.88%)
Mutual labels:  craftcms, craft-plugin
Oauth
(Deprecated) Helps developers consume OAuth-based web services in Craft plugins.
Stars: ✭ 38 (-65.14%)
Mutual labels:  craftcms, craft-plugin
Craft Coordinates
A twig filter for Craft CMS that gets the latitude and longitude from an address
Stars: ✭ 27 (-75.23%)
Mutual labels:  craftcms, craft-plugin
Buttonbox
A collection of utility field types for Craft
Stars: ✭ 94 (-13.76%)
Mutual labels:  craftcms, craft-plugin
Vanillaforums
DEPRECATED Single Sign On plugin for VanillaForums/jsConnect and CraftCMS
Stars: ✭ 14 (-87.16%)
Mutual labels:  craftcms, craft-plugin
Craft Helpers
Helpers plugin for Craft CMS
Stars: ✭ 58 (-46.79%)
Mutual labels:  craftcms, craft-plugin
Falcon
Enables Xkey / Hash-Two / Surrogate Key caching with Craft 2.x. Very experimental.
Stars: ✭ 14 (-87.16%)
Mutual labels:  craftcms, craft-plugin
Aws S3
Amazon S3 volume type for Craft CMS
Stars: ✭ 57 (-47.71%)
Mutual labels:  craftcms, craft-plugin
Plugin Installer
Composer installer for Craft CMS plugins
Stars: ✭ 22 (-79.82%)
Mutual labels:  craftcms, craft-plugin
Craft Emptycoalesce
Empty Coalesce adds the ??? operator to Twig that will return the first thing that is defined, not null, and not empty
Stars: ✭ 24 (-77.98%)
Mutual labels:  craftcms, craft-plugin
Commerceregisteroncheckout
ARCHIVED: SEE README Allow user registration during Craft Commerce V1 checkouts.
Stars: ✭ 34 (-68.81%)
Mutual labels:  craftcms, craft-plugin
Craft Preparse Field
Field type that parses twig when an element is saved.
Stars: ✭ 103 (-5.5%)
Mutual labels:  craftcms, craft-plugin

CraftCMS Asset Rev / Cache Busting (Craft 3.x)

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

Looking for Craft 2 Support? Asset Rev for Craft 2

A Twig extension for CraftCMS that helps you cache-bust your assets using configurable strategies.

Why?

In order to speed up the load time of your pages, you can set a far-future expires header on your images, stylesheets and scripts. However, when you update those assets you'll need to update their file names to force the browser to download the updated version.

Using a manifest file is the recommended approach - you can read up on why using query strings isn't ideal here.

Strategies

This plugin allows you to configure multiple cache-busting strategies for your asset filenames. The plugin comes with three strategies out of the box:

Manifest File

css/main.css will be replaced with the corresponding hashed filename as defined within your assets manifest .json file.

If the contents of your manifest file are...

{
    "css/main.css": "css/main.a9961d38.css",
    "js/main.js": "js/main.786087f5.js"
}

then rev('css/main.css') will expand to css/main.a9961d38.css.

Please note: This plugin does not create manifest files; instead they should be generated during your build process using Gulp Rev, Laravel Mix or another comparable tool.

Query String

Append a query string to your file, based on the time it was last modified. For example: rev('css/main.css') will expand to something like css/main.css?1473534554.

Passthrough

Returns the original filename, without modification. This is useful if all other cache-busting strategies fail.

Strategy Pipeline

Pipelines allow you to attempt multiple cache-busting strategies in sequence. If one strategy fails, the plugin can proceed to try and cache-bust the asset filename using the next strategy in the pipeline.

The default pipeline is manifest|querystring|passthrough and will:

  1. Attempt to use the ManifestFileStrategy. If it can’t, because the manifest file doesn’t exist, it will throw a ContinueException that defers cache-busting to the next strategy in the pipeline…
  2. Attempt to use the QueryStringStrategy. If it can’t, because it can’t find the asset file, it will throw another ContinueException that defers cache-busting to the final default strategy…
  3. Returns the original filename using the closure-based pass-through strategy.

Need to provide your own cache-busting logic? Simply create your own implementation of the Strategy class or define a Closure in the configuration file.

Installation

Install via the Plugin Store within your Craft 3 installation or using Composer: composer require clubstudioltd/craft-asset-rev

Configuration

The plugin comes with a config.php file that defines some sensible defaults.

If you want to set your own values you should create a assetrev.php file in your Craft config directory. The contents of this file will get merged with the plugin defaults, so you only need to specify values for the settings you want to override.

Strategies

strategies is where you define the strategies you'd like to try to rev your asset filename. You can provide the name of a class that implements StrategyContact or a custom closure. The defaults should cater to most requirements.

Pipeline

pipeline allows you to set the order of the configured strategies you'd like to try when revving your asset file names. The default of: manifest|querystring|passthrough should be adequate for most use-cases.

Manifest Path

manifestPath is where Craft should look for your manifest file. Non-absolute paths will be relative to the base path of your Craft installation (whatever CRAFT_BASE_PATH is set to).

Assets Base Path

assetsBasePath is the the base path to your assets. Again, this is relative to your craft base directory, unless you supply an absolute directory path.

Asset Url Prefix

assetUrlPrefix will be prepended to the output of rev().

Note: You can use Yii aliases in your configuration values.

An Example Config File

<?php
return array(
    '*' => array(
        'strategies' => [
            'manifest' => \club\assetrev\utilities\strategies\ManifestFileStrategy::class,
            'querystring' => \club\assetrev\utilities\strategies\QueryStringStrategy::class,
            'passthrough' => function ($filename, $config) {
                return $filename;
            },
        ],
        'pipeline' => 'manifest|querystring|passthrough',
        'manifestPath' => 'resources/assets/assets.json',
        'assetsBasePath' => '../public/build/',
        'assetUrlPrefix' => '@web/assets',
    ),
);

Usage

Once activated and configured you can use the rev() function in your templates.

<link rel="stylesheet" href="{{ rev('css/main.css') }}">

Custom Strategies

Need to provide your own cache-busting logic? Create your own Strategy class or simply use a Closure.

Example Strategy Class

<?php

namespace your\namespace;

use club\assetrev\utilities\Strategy;
use club\assetrev\exceptions\ContinueException;

class QueryStringStrategy extends Strategy
{
    public function rev($filename)
    {
        // add your logic to manipulate $filename here...
        return $filename;
    }
}

Example Closure

Your method will have access to the asset filename and the plugin configuration array.

function ($filename, $config) {
    // add your logic to manipulate $filename here...
    return $filename;
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].