Exclude specific pages from JS asset registration

Anyone know how to exclude a couple of pages when registering a JS asset? I need this script running in the background on 99% of pages so I'm not sure that lazy loading is the right approach...
Solution:
As an example: ```php if (! function_exists('is_panel_auth_route')) { function is_panel_auth_route(): bool...
Jump to solution
7 Replies
binaryfire
binaryfireOP10mo ago
Bump
awcodes
awcodes10mo ago
you can either inline it with a renderhook and scope or with the asset manager https://filamentphp.com/docs/3.x/support/assets#lazy-loading-javascript
binaryfire
binaryfireOP10mo ago
Problem is this is something I need on every page except login and register. I couldn't see a simple way of doing that with render hooks. I guess I could use an empty div to lazy load it and use @if to exclude the script from being include on the login and register routes?
Solution
awcodes
awcodes10mo ago
As an example:
if (! function_exists('is_panel_auth_route')) {
function is_panel_auth_route(): bool
{
$authRoutes = [
'/login',
'/password-reset',
'/register',
'/email-verification',
];

return Str::of(Request::path())->contains($authRoutes);
}
}

if (! is_panel_auth_route()) {
FilamentView::registerRenderHook(
name: 'panels::body.end',
fn (): string => Blade::render('@vite("resources/js/my-script.js")')
);
}
if (! function_exists('is_panel_auth_route')) {
function is_panel_auth_route(): bool
{
$authRoutes = [
'/login',
'/password-reset',
'/register',
'/email-verification',
];

return Str::of(Request::path())->contains($authRoutes);
}
}

if (! is_panel_auth_route()) {
FilamentView::registerRenderHook(
name: 'panels::body.end',
fn (): string => Blade::render('@vite("resources/js/my-script.js")')
);
}
binaryfire
binaryfireOP10mo ago
Awesome, thanks!
binaryfire
binaryfireOP10mo ago
Request::path() in boot() was always returning / (probably because of Octane) so ended up doing it middleware.
Want results from more Discord servers?
Add your server