When it comes to web development, caching and lazy loading are two distinct concepts.
The Concept of Caching
Essentially, caching a file means that there is no need to fetch it from the server every time. Instead, the file is retrieved from the browser's cache, saving time on HTTP requests.
One way to optimize caching is by inline templates within the HTML, reducing the need to load them from external files. While this may increase the initial loading time slightly due to the template size in bytes, it is generally beneficial to cache templates and scripts.
Understanding Lazy Loading
Lazily loading a file means that it is only fetched (no HTTP request is made) when the module is actually required. The script is then evaluated and executed.
It is also possible to combine lazy loading with caching. This can be achieved through using service workers for caching or relying on request headers and the browser's cache.