Skip to content

Commit

Permalink
Add LocalValetDriver.php for static asset filename-based cache busting
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Welch <[email protected]>
  • Loading branch information
khalwat committed Jan 6, 2018
1 parent 69df569 commit 25e4836
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions LocalValetDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Implements a static asset filename-based cache busting driver for Craft CMS,
* based on the built-in CraftValetDriver. Place this file in your project root,
* and Valet will pick it up automatically as per the "Local Drivers" section here:
*
* https://laravel.com/docs/5.5/valet
*
* Static asset filename-based cache busting for Craft CMS is discussed here:
*
* https://nystudio107.com/blog/simple-static-asset-versioning
*
* Based on https://gist.github.com/stidges/3d0c0317bf0d36073dd045bbcc742852
*
* @author nystudio107
* @copyright Copyright (c) 2017 nystudio107
* @link https://nystudio107.com/
* @package nystudio107/craft
* @since 1.0.0
* @license MIT
*/

class LocalValetDriver extends CraftValetDriver
{
/**
* @inheritdoc
*/
public function isStaticFile($sitePath, $siteName, $uri)
{
// Try the parent first
$result = parent::isStaticFile($sitePath, $siteName, $uri);
if ($result !== false) {
return $result;
}

// Determine if this is a type we use filename-based cache busting with
if (preg_match('/(.+)\.(?:\d+)\.(js|css|png|jpg|jpeg|gif|webp)$/i', $uri, $matches)) {
// Rewrite cache busted URIs to their original filename (e.g. jquery.1476809927.js to jquery.js)
return $sitePath.'/'.$this->frontControllerDirectory($sitePath).$matches[1].'.'.$matches[2];
}

return false;
}
}

0 comments on commit 25e4836

Please sign in to comment.