Skip to content

Instantly share code, notes, and snippets.

@khalwat
Created February 26, 2018 17:25
Show Gist options
  • Save khalwat/a0687a8a8f0bf3ef5db7c371d88d3e04 to your computer and use it in GitHub Desktop.
Save khalwat/a0687a8a8f0bf3ef5db7c371d88d3e04 to your computer and use it in GitHub Desktop.
Keep hashed directories consistent in a load balanced server environment with Craft CMS 3
<?php
/**
* Yii Application Config
*
* Edit this file at your own risk!
*
* The array returned by this file will get merged with
* vendor/craftcms/cms/src/config/app/main.php and [web|console].php, when
* Craft's bootstrap script is defining the configuration for the entire
* application.
*
* You can define custom modules and system components, and even override the
* built-in system components.
*/
/**
* AdminCP resources in Craft CMS 3 are published in the public @webroot/cpresources
* with a directory name that's a hashed from the directory path and the timestamp
* of when the resource was published.
*
* On load balanced environments, the addition of a timestamp can cause 404s as
* the timestamp will vary from server to server. You can work around this problem
* by providing your own `hashCallback` function to generate a hash of the directory
* path without a timestamp.
*
* The `hashCallback` can actually be any PHP callable, but here it's presented
* as an anonymous function. c.f.:
*
* vendor/craftcms/cms/src/config/app.web.php
* vendor/craftcms/cms/src/web/AssetManager.php
*
* @author nystudio107
* @package n/a
* @since 1.0.0
*/
return [
// All environments
'*' => [
],
// Live (production) environment
'live' => [
'components' => [
'assetManager' => function() {
$generalConfig = Craft::$app->getConfig()->getGeneral();
$config = [
'class' => craft\web\AssetManager::class,
'basePath' => $generalConfig->resourceBasePath,
'baseUrl' => $generalConfig->resourceBaseUrl,
'fileMode' => $generalConfig->defaultFileMode,
'dirMode' => $generalConfig->defaultDirMode,
'appendTimestamp' => true,
'hashCallback' => function ($path) {
return hash('md4', $path);
}
];
return Craft::createObject($config);
},
],
],
// Staging (pre-production) environment
'staging' => [
],
// Local (development) environment
'local' => [
],
];
@timkelty
Copy link

timkelty commented Apr 9, 2018

Posted issue here: craftcms/cms#2712

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment