F
Filament15mo ago
Arjen

Best way to include script

Hi, I've registered a custom script like this:
FilamentAsset::register([
Js::make('google-maps')->html(new HtmlString('
<script>
(g => {
var h, a, k, p = "The Google Maps JavaScript API",
c = "google",
l = "importLibrary",
q = "__ib__",
m = document,
b = window;
b = b[c] || (b[c] = {});
var d = b.maps || (b.maps = {}),
r = new Set,
e = new URLSearchParams,
u = () => h || (h = new Promise(async (f, n) => {
await (a = m.createElement("script"));
e.set("libraries", [...r] + "");
for (k in g) e.set(k.replace(/[A-Z]/g, t => "_" + t[0].toLowerCase()), g[k]);
e.set("callback", c + ".maps." + q);
a.src = `https://maps.${c}apis.com/maps/api/js?` + e;
d[q] = f;
a.onerror = () => h = n(Error(p + " could not load."));
a.nonce = m.querySelector("script[nonce]")?.nonce || "";
m.head.append(a)
}));
d[l] ? console.warn(p + " only loads once. Ignoring:", g) : d[l] = (f, ...n) => r.add(f) && u().then(() => d[l](f, ...n))
})({
key: "",
v: "weekly",
});
</script>
')),
]);
FilamentAsset::register([
Js::make('google-maps')->html(new HtmlString('
<script>
(g => {
var h, a, k, p = "The Google Maps JavaScript API",
c = "google",
l = "importLibrary",
q = "__ib__",
m = document,
b = window;
b = b[c] || (b[c] = {});
var d = b.maps || (b.maps = {}),
r = new Set,
e = new URLSearchParams,
u = () => h || (h = new Promise(async (f, n) => {
await (a = m.createElement("script"));
e.set("libraries", [...r] + "");
for (k in g) e.set(k.replace(/[A-Z]/g, t => "_" + t[0].toLowerCase()), g[k]);
e.set("callback", c + ".maps." + q);
a.src = `https://maps.${c}apis.com/maps/api/js?` + e;
d[q] = f;
a.onerror = () => h = n(Error(p + " could not load."));
a.nonce = m.querySelector("script[nonce]")?.nonce || "";
m.head.append(a)
}));
d[l] ? console.warn(p + " only loads once. Ignoring:", g) : d[l] = (f, ...n) => r.add(f) && u().then(() => d[l](f, ...n))
})({
key: "",
v: "weekly",
});
</script>
')),
]);
This works, but running php artisan filament:assets produces the following type error Filament\Support\Assets\Asset::getPath(): Return value must be of type string, null returned...
Solution:
I did it this way for tracking analytics: ```php FilamentView::registerRenderHook( 'panels::body.end', static fn (): View => view('analytics'),...
Jump to solution
3 Replies
Arjen
ArjenOP15mo ago
... so I'm not sure if this is the correct way to include scripts that I want to have loaded on all pages
Solution
DrByte
DrByte15mo ago
I did it this way for tracking analytics:
FilamentView::registerRenderHook(
'panels::body.end',
static fn (): View => view('analytics'),
);
FilamentView::registerRenderHook(
'panels::body.end',
static fn (): View => view('analytics'),
);
and then created view file analytics.blade.php to contain my js
Arjen
ArjenOP14mo ago
Ooh nice one, thanks!
Want results from more Discord servers?
Add your server