Skip to content

Commit 72fd6d9

Browse files
committed
Catch errors when registering script
1 parent 6981ad3 commit 72fd6d9

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "putyourlightson/craft-datastar-module",
33
"description": "A template-driven, reactive hypermedia framework for Craft.",
4-
"version": "1.0.2",
4+
"version": "1.0.3",
55
"type": "yii-module",
66
"license": "mit",
77
"require": {

src/Datastar.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,20 @@ private function registerScript(): void
9898
return;
9999
}
100100

101-
$bundle = Craft::$app->getView()->registerAssetBundle(DatastarAssetBundle::class);
102-
103-
/**
104-
* Register the JS file explicitly so that it will be output when using template caching. We use the `EVENT_BEFORE_SEND` event so that the JS file is registered regardless of whether this is an error response or not.
105-
* https://github.com/putyourlightson/craft-datastar/issues/20
106-
*/
107-
Event::on(Response::class, Response::EVENT_BEFORE_SEND, function() use ($bundle) {
108-
$url = Craft::$app->getView()->getAssetManager()->getAssetUrl($bundle, $bundle->js[0]);
109-
Craft::$app->getView()->registerJsFile($url, $bundle->jsOptions);
110-
});
101+
try {
102+
$bundle = Craft::$app->getView()->registerAssetBundle(DatastarAssetBundle::class);
103+
104+
/**
105+
* Register the JS file explicitly so that it will be output when using template caching. We use the `EVENT_BEFORE_SEND` event so that the JS file is registered regardless of whether this is an error response or not.
106+
* https://github.com/putyourlightson/craft-datastar/issues/20
107+
*/
108+
Event::on(Response::class, Response::EVENT_BEFORE_SEND, function() use ($bundle) {
109+
$url = Craft::$app->getView()->getAssetManager()->getAssetUrl($bundle, $bundle->js[0]);
110+
Craft::$app->getView()->registerJsFile($url, $bundle->jsOptions);
111+
});
112+
} catch (\Throwable $error) {
113+
Craft::error('Failed to register Datastar script: ' . $error->getMessage(), __METHOD__);
114+
}
111115
}
112116

113117
private function registerSseDumper(): void

0 commit comments

Comments
 (0)