├── composer.json ├── LICENSE ├── README.md └── Classes └── Aspects └── CacheBusterAspect.php /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "flowpack/cachebuster", 3 | "type": "neos-package", 4 | "description": "Cache buster for static resources in Neos", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Sebastian Helzle", 9 | "email": "sebastian@helzle.it" 10 | } 11 | ], 12 | "require": { 13 | "neos/flow": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0" 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "Flowpack\\CacheBuster\\": "Classes" 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Flowpack 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://poser.pugx.org/flowpack/cachebuster/v/stable)](https://packagist.org/packages/flowpack/cachebuster) 2 | [![Total Downloads](https://poser.pugx.org/flowpack/cachebuster/downloads)](https://packagist.org/packages/flowpack/cachebuster) 3 | [![License](https://poser.pugx.org/flowpack/cachebuster/license)](LICENSE) 4 | 5 | # Flowpack.CacheBuster 6 | 7 | Adds automatic cache busting for static resources in the frontend. 8 | 9 | The output for those resources is modified by appending a shortened sha1 of the file like this 10 | 11 | /_Resources/Static/Packages/Neos.NeosIo/Styles/Main.css?bust=3e9a4e48 12 | 13 | ## Installation 14 | 15 | composer require flowpack/cachebuster 16 | 17 | ## Usage 18 | 19 | This package provides an aspect which is automatically active 20 | and enabled cache busting without any further modification. 21 | 22 | ### Compatibility with mod_pagespeed 23 | 24 | Add the following to your webserver configuration to allow pagespeed 25 | to optimize resources with the cache bust query parameter. 26 | 27 | #### nginx 28 | 29 | pagespeed Allow "*"; 30 | 31 | #### Apache 32 | 33 | ModPagespeedAllow "*" 34 | 35 | Or be more specific by just allowing the `*?bust=*`. 36 | -------------------------------------------------------------------------------- /Classes/Aspects/CacheBusterAspect.php: -------------------------------------------------------------------------------- 1 | getPublicPackageResourceUri())") 24 | * 25 | * @param JoinPointInterface $joinPoint The current joinpoint 26 | * 27 | * @return string The modified public resource uri with appended sha1 hash as parameter 28 | */ 29 | public function addCacheHash(JoinPointInterface $joinPoint) 30 | { 31 | $result = $joinPoint->getAdviceChain()->proceed($joinPoint); 32 | 33 | $packageKey = $joinPoint->getMethodArgument('packageKey'); 34 | $relativePathAndFilename = $joinPoint->getMethodArgument('relativePathAndFilename'); 35 | 36 | return $this->getModifiedResourceUri($result, $relativePathAndFilename, $packageKey); 37 | } 38 | 39 | /** 40 | * @param string $uri 41 | * @param string $path 42 | * @param string $package 43 | * @return string 44 | */ 45 | protected function getModifiedResourceUri($uri, $path, $package) 46 | { 47 | if (strpos($path, 'resource://') === 0) { 48 | $resourcePath = $path; 49 | } elseif ($package !== null) { 50 | $resourcePath = 'resource://' . $package . '/Public/' . $path; 51 | } else { 52 | return $uri; 53 | } 54 | 55 | if (!is_dir($resourcePath) && file_exists($resourcePath) && strpos($uri, 'bust') === false) { 56 | try { 57 | $hash = 'bust=' . substr(sha1_file($resourcePath), 0, 8); 58 | 59 | if (strpos($uri, '?') === false) { 60 | return $uri . '?' . $hash; 61 | } else { 62 | return $uri . '&' . $hash; 63 | } 64 | } catch (\Exception $e) { 65 | } 66 | } 67 | return $uri; 68 | } 69 | } 70 | --------------------------------------------------------------------------------