-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LocalValetDriver.php for static asset filename-based cache busting
Signed-off-by: Andrew Welch <[email protected]>
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |