Skip to content

Commit

Permalink
add note about jquery inclusion during action
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Jul 27, 2023
1 parent 00f7172 commit 4fa0f79
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
17 changes: 17 additions & 0 deletions code/DebugBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class DebugBar
*/
public static $bufferingEnabled = false;

/**
* @var bool
*/
public static $suppressJquery = false;

/**
* @var JavascriptRenderer
*/
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions code/Middleware/DebugBarMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, '</body>') !== false) {
if (DebugBar::$suppressJquery) {
// Move scripts after the latest script
$matches = [];
preg_match_all('/<script(.*) src="(.*)\/debugbar\/(.*)"><\/script>/', $body, $matches);
$body = preg_replace('/<script(.*) src="(.*)\/debugbar\/(.*)"><\/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('</body>', $script . '</body>', $body);
} else {
Expand Down

0 comments on commit 4fa0f79

Please sign in to comment.