Added support for 'cache' attribute to addJs() to disable CF RocketLoader (#4092)

Credit to @itpcc 

Due to CloudFlare Rocket Loader, CF will automatically try to optimize page loading speed by changing script type attribute. This breaks the script execution order and makes the user unable to upload the file(s) in the backend using "FileUpload" widget.

However, [CloudFlare allows adding "data-cfasync"](https://support.cloudflare.com/hc/en-us/articles/200169436-How-can-I-have-Rocket-Loader-ignore-specific-JavaScripts-) to prevent this. And it was used in the [commit #3841](https://github.com/octobercms/october/pull/3841/files).
This commit is contained in:
Rachasak Ragkamnerd 2019-04-17 09:27:13 +07:00 committed by Luke Towers
parent 38070b0111
commit 70cd444f8a
3 changed files with 15 additions and 2 deletions

View File

@ -345,7 +345,10 @@ class FileUpload extends FormWidgetBase
protected function loadAssets()
{
$this->addCss('css/fileupload.css', 'core');
$this->addJs('js/fileupload.js', 'core');
$this->addJs('js/fileupload.js', [
'build' => 'core',
'cache' => 'false'
]);
}
/**

View File

@ -159,7 +159,10 @@ class Form extends WidgetBase
*/
protected function loadAssets()
{
$this->addJs('js/october.form.js', 'core');
$this->addJs('js/october.form.js', [
'build' => 'core',
'cache' => 'false'
]);
}
/**

View File

@ -128,6 +128,13 @@ trait AssetMaker
$jsPath = $this->getAssetScheme($jsPath);
// Prevent CloudFlare's Rocket Loader from breaking stuff
// @see octobercms/october#4092, octobercms/october#3841, octobercms/october#3839
if (isset($attributes['cache']) && $attributes['cache'] == 'false') {
$attributes['data-cfasync'] = 'false';
unset($attributes['cache']);
}
if (!in_array($jsPath, $this->assets['js'])) {
$this->assets['js'][] = ['path' => $jsPath, 'attributes' => $attributes];
}