I have a number of Javascript and CSS files that I use in my app, but in limited situations. Most users never need those files to be loaded. So I want to load them dynamically as needed. The idea is to reduce the time for the initial app load.
The main question is where those files need to live so they do not get downloaded automatically when the app loads.
Anything referenced in the Native Libraries section is clearly automatically downloaded with the rest of the app.
What about files in the Assets section? Are they automatically downloaded on the initial app load as well, or on demand as they’re referenced (e.g. from the Native Libraries section, image load, etc).
Since you’re suggesting using a third-party spot to host those files, does that mean you are saying files in the Assets section are automatically downloaded when the app is loaded? Can we get confirmation on that from some of the Anvil folks?
I don’t know about Javascript files but any image you store in assets definitely has an impact on the loading time. So I’ll say that it best to host your file somewhere else
Is there any reason the file could not be stored in a datatable as a bytes like object in a media column and then loaded as needed?
I really don’t know much about dynamically loaded JavaScript or if the browser would have a fit loading a file information from memory instead of a linked source.
Would this be possible?
Anvil Extras has some helper methods for this which we use on the client side components to ensure that various bits of html, javascript and css are only loaded when that particular component is being used.
I do like those helper methods, and am using an older version copied over from Anvil Extras. The most recent version is even better, though, and is on my todo list to use.
The main question wasn’t about how to load those files, but if they could be stored in the Assets section and achieve the desired effect (e.g. be downloaded only when needed, and not automatically when the app loads, or if the files need to be stored outside of Anvil to get that effect.
@divyeshlakhotia’s observation about images in Assets impacting load time suggests that Assets isn’t a good place for these files.
That’s a good idea. I’d been working through the issues of having a dev/prod split and managing two versions of these files without having Anvil do it automatically. Data tables are a nice approach.
When running the app assets are lazily loaded
the exception to this is html files and theme.css, both of which are eagerly loaded.
(note this is different to the ide where all asset files are loaded)
The anvil-extras loading functions are a good way to go.
Another way you might take advantage of assets being lazy loaded is to include script tags in native libraries with the src attribute and either the async or defer keyword.
The async keyword loads the src in parallel and won’t block the loading of the page
The defer keyword waits until the page has loaded before loading the script
Some more info available at MDN