Lazy Loading of blade [css/js] files #1340
Replies: 3 comments
-
A) Is it true we cannot do the above (includeIf, controller variables, @stack/@Push) in Backpack? No, it is not true that you cannot use these Blade features in Backpack. Backpack is built on top of Laravel and supports standard Blade features like @includeIf, passing variables from controllers, and using @stack/@Push for scripts and styles. In fact, Backpack encourages the use of @stack and @Push for managing CSS and JS assets, and even extends this with its own widgets and asset management tools for more flexibility and performance. B) What is the recommended way to only load the modals (css,js) when needed, or at minimum, lazy load on the specific page? The recommended way in Backpack 6.x is to use the Widget system or the new Basset asset loader to conditionally load CSS and JS only on the pages where they are needed: 1. Using Widgets for CSS/JS: // In your CrudController, inside the operation method (e.g., setupListOperation)
Widget::add()->type('script')->content('assets/js/custom-modal.js');
Widget::add()->type('style')->content('assets/css/custom-modal.css'); This ensures the assets are only loaded when that operation/page is rendered. You can also use integrity and crossorigin if loading from a CDN. For more details, see the Backpack Widgets documentation. 2. Using Basset for Asset Loading: @basset('https://example.com/link/to/asset.js')
@basset('public_path("assets/js/custom-modal.js")') This is recommended for both performance and privacy, as Basset caches and serves assets efficiently. See the Basset documentation for more info. 3. Limiting Asset Scope:
4. About @stack and @Push: Summary: If your modals are complex and interactive, this approach will ensure their assets are only loaded when required, improving your page load performance. ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️ Relevant sources: Github Issue (closed): [5.0] The same CSS and JS assets can be loaded multiple times |
Beta Was this translation helpful? Give feedback.
-
Hey @qontori Following two guides may help you here: |
Beta Was this translation helpful? Give feedback.
-
This is great. thanks very much |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
My app has grown quite "large" and I am trying to make the loading of the course pages fast. Then, only when I need to use the css/js for custom models load them.
I have a "@include(backpack_view('custom_modals'))" inside blank.blade.php which loads up my blade file [which is big].
I do not want to split the code up and then just have multiple @includes because this only makes it easier to maintain the code but does nothing about the page load performance.
I read that in laravel we can do things like:
But subsequently told these do not work in backpage.
A) Is it true we cannot do the above?
B) What is the recommended way to only load the modals (css,js) when I need it or at minimum using a lazy load on the specific page? Note: Note the modals are more than just a simple OK, Cancel, they have full interactivity with the DB and are jquery based
Thanks in advance for your assistance.
Beta Was this translation helpful? Give feedback.
All reactions