-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent double-registering of asset bundles and JS files in the CP
- Loading branch information
1 parent
9f25a19
commit 6b446e8
Showing
8 changed files
with
115 additions
and
8 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
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
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 |
---|---|---|
|
@@ -39,6 +39,8 @@ | |
* @property-read Environment $twig the Twig environment | ||
* @property-read string $bodyHtml the content to be inserted at the end of the body section | ||
* @property-read string $headHtml the content to be inserted in the head section | ||
* @property-write string[] $registeredAssetBundles the asset bundle names that should be marked as already registered | ||
* @property-write string[] $registeredJsFiles the JS files that should be marked as already registered | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 3.0 | ||
*/ | ||
|
@@ -196,6 +198,20 @@ class View extends \yii\web\View | |
*/ | ||
private $_isRenderingPageTemplate = false; | ||
|
||
/** | ||
* @var string[] | ||
* @see registerAssetFiles() | ||
* @see setRegisteredAssetBundles() | ||
*/ | ||
private $_registeredAssetBundles = []; | ||
|
||
/** | ||
* @var string[] | ||
* @see registerJsFile() | ||
* @see setRegisteredJsfiles() | ||
*/ | ||
private $_registeredJsFiles = []; | ||
|
||
// Public Methods | ||
// ========================================================================= | ||
|
||
|
@@ -791,6 +807,19 @@ public function clearJsBuffer(bool $scriptTag = true) | |
return $js; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function registerJsFile($url, $options = [], $key = null) | ||
{ | ||
$key = $key ?: $url; | ||
if (isset($this->_registeredJsFiles[$key])) { | ||
return; | ||
} | ||
$this->_registeredJsFiles[$key] = true; | ||
parent::registerJsFile($url, $options, $key); | ||
} | ||
|
||
/** | ||
* Registers a generic `<script>` code block. | ||
* | ||
|
@@ -1206,6 +1235,26 @@ public function invokeHook(string $hook, array &$context): string | |
return $return; | ||
} | ||
|
||
/** | ||
* Sets the JS files that should be marked as already registered. | ||
* | ||
* @param string[] $keys | ||
*/ | ||
public function setRegisteredJsFiles(array $keys) | ||
{ | ||
$this->_registeredJsFiles = array_flip($keys); | ||
} | ||
|
||
/** | ||
* Sets the asset bundle names that should be marked as already registered. | ||
* | ||
* @param string[] $names Asset bundle names | ||
*/ | ||
public function setRegisteredAssetBundles(array $names) | ||
{ | ||
$this->_registeredAssetBundles = array_flip($names); | ||
} | ||
|
||
// Events | ||
// ------------------------------------------------------------------------- | ||
|
||
|
@@ -1335,6 +1384,28 @@ protected function renderBodyEndHtml($ajaxMode) | |
$lines[] = implode("\n", $this->_scripts[self::POS_END]); | ||
} | ||
|
||
if (Craft::$app->getRequest()->getIsCpRequest()) { | ||
if (!empty($this->_registeredJsFiles)) { | ||
$json = Json::encode($this->_registeredJsFiles); | ||
$js = <<<JS | ||
if (typeof Craft !== 'undefined') { | ||
jQuery.extend(Craft.registeredJsFiles, {$json}); | ||
} | ||
JS; | ||
$this->registerJs($js, self::POS_END); | ||
} | ||
|
||
if (!empty($this->_registeredAssetBundles)) { | ||
$json = Json::encode($this->_registeredAssetBundles); | ||
$js = <<<JS | ||
if (typeof Craft !== 'undefined') { | ||
jQuery.extend(Craft.registeredAssetBundles, {$json}); | ||
} | ||
JS; | ||
$this->registerJs($js, self::POS_END); | ||
} | ||
} | ||
|
||
$html = parent::renderBodyEndHtml($ajaxMode); | ||
|
||
return empty($lines) ? $html : implode("\n", $lines).$html; | ||
|
@@ -1376,6 +1447,19 @@ protected function registerAllAssetFiles() | |
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function registerAssetFiles($name) | ||
{ | ||
// Don't re-register bundles | ||
if (isset($this->_registeredAssetBundles[$name])) { | ||
return; | ||
} | ||
$this->_registeredAssetBundles[$name] = true; | ||
parent::registerAssetFiles($name); | ||
} | ||
|
||
// Private Methods | ||
// ========================================================================= | ||
|
||
|
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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