It depends on what you mean by loading a form.
When the app starts, all the code is loaded, forms are code, so all the forms are loaded when the app is loaded. If you have 100 forms and 5000 lines of code, when the app starts you load 100 forms and 5000 lines of code. You can’t do anything about this, that’s how Anvil works. This is the overhead of starting up the app.
Forms are also imported, and you may call that import loading a form. The client side only allows a few packages, and they are all fast to load. So avoiding the import of a form would save you very little time.
Each form does something when it starts up, you may consider that loading a form. This can be very slow, for example if you call dozens of server functions, or even one single server function that runs something very slow on the server before returning the control to the form. You have full control over this, and you can do it as lazy as you like, but it’s up to you, it’s not something that Anvil “supports”.
Lazily loading a form may mean loading the data required by each component only when that component needs it. This could be beneficial in some cases, but in most cases this will make your form slower, much slower. It’s often faster to load everything you need in one shot, even if it means loading more than you actually need, than postponing the loading and triggering many delayed server calls.
Each form is an instance of its form class. The routing of Anvil Extras caches (optionally) the form instances, which means that if you go again to the same url, the module will show the form as it was the last time it was shown, without executing or loading or initializing anything. Well, it will execute the form_show
event, where you can decide whether to do nothing, reload everything, or do select checks for stale data and update only what you need to update. That’s how you make your forms lazy.
To use the routing module you need to change how forms are loaded. The routing module will load the forms for you and pass the url arguments to the form. Then the form decides what to do with those arguments, usually in the form_show
event.
All my apps use the routing module, and all my forms make zero or one server call in the form_show
event, so they are as fast as that server call.