diff --git a/README.md b/README.md index d43a07e..4fbb5ac 100644 --- a/README.md +++ b/README.md @@ -294,6 +294,20 @@ protected function init() } ``` +If you include jQuery during an action, you need to call `DebugBar::suppressJquery();`. +This will move all our scripts after your own, which should include Jquery first. + +```php +public function my_action() +{ + DebugBar::suppressJquery(); + Requirements::javascript("//code.jquery.com/jquery-3.3.1.min.js"); + Requirements::javascript("my.plugin.js"); + + return $this; +} +``` + #### FulltextSearchable support It has been reported that the `FulltextSearchable` extension conflicts with the `config_collector`. diff --git a/code/DebugBar.php b/code/DebugBar.php index 0ec0593..12b1758 100644 --- a/code/DebugBar.php +++ b/code/DebugBar.php @@ -62,6 +62,11 @@ class DebugBar */ public static $bufferingEnabled = false; + /** + * @var bool + */ + public static $suppressJquery = false; + /** * @var JavascriptRenderer */ @@ -295,6 +300,18 @@ public static function moduleResource($path) return ModuleLoader::getModule('lekoala/silverstripe-debugbar')->getResource($path); } + public static function suppressJquery($flag = true) + { + $file = "debugbar/assets/vendor/jquery/dist/jquery.min.js"; + if ($flag) { + Requirements::block($file); + } else { + Requirements::unblock($file); + } + + self::$suppressJquery = $flag; + } + /** * Include DebugBar assets using Requirements API * This needs to be called before the template is rendered otherwise the calls to the Requirements API are ignored diff --git a/code/Middleware/DebugBarMiddleware.php b/code/Middleware/DebugBarMiddleware.php index dfc981c..73ea6da 100644 --- a/code/Middleware/DebugBarMiddleware.php +++ b/code/Middleware/DebugBarMiddleware.php @@ -7,6 +7,7 @@ use SilverStripe\Control\Director; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; +use SilverStripe\Dev\Debug; use SilverStripe\View\Requirements; class DebugBarMiddleware implements HTTPMiddleware @@ -102,6 +103,18 @@ protected function afterRequest(HTTPRequest $request, HTTPResponse $response) // Inject init script into the HTML response $body = (string)$response->getBody(); if (strpos($body, '') !== false) { + if (DebugBar::$suppressJquery) { + // Move scripts after the latest script + $matches = []; + preg_match_all('/<\/script>/', $body, $matches); + $body = preg_replace('/<\/script>\n/', '', $body); + + $ourRequirements = array_reverse($matches[0]); + foreach ($ourRequirements as $ourRequirement) { + $script = $ourRequirement . "\n" . $script; + } + } + if (Requirements::get_write_js_to_body()) { $body = str_replace('', $script . '', $body); } else {