Skip to content

Commit 3a193b7

Browse files
committed
docs: lazy loading & caching
1 parent 8b3cb74 commit 3a193b7

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#### 7.4. Lazy Loading and Caching Strategies
2+
3+
Lazy loading and caching are powerful techniques for optimizing the loading performance of your web applications. This submodule covers the concepts of lazy loading and caching and provides examples of how to implement them effectively.
4+
5+
##### Lazy Loading
6+
7+
Lazy loading involves deferring the loading of non-essential resources until they are actually needed. This can significantly improve the initial page load time and reduce the overall resource footprint.
8+
9+
**Example: Lazy Loading Images**
10+
11+
Consider a webpage with multiple images. Instead of loading all images at once, you can lazy load them as the user scrolls down the page.
12+
13+
```html
14+
<!-- Before lazy loading -->
15+
<img src="image1.jpg" alt="Image 1">
16+
<img src="image2.jpg" alt="Image 2">
17+
<img src="image3.jpg" alt="Image 3">
18+
<!-- ... -->
19+
20+
<!-- After lazy loading -->
21+
<img data-src="image1.jpg" alt="Image 1">
22+
<img data-src="image2.jpg" alt="Image 2">
23+
<img data-src="image3.jpg" alt="Image 3">
24+
<!-- ... -->
25+
26+
<script>
27+
document.addEventListener('DOMContentLoaded', function () {
28+
const lazyImages = document.querySelectorAll('img[data-src]');
29+
30+
lazyImages.forEach(function (lazyImage) {
31+
lazyImage.src = lazyImage.dataset.src;
32+
lazyImage.removeAttribute('data-src');
33+
});
34+
});
35+
</script>
36+
```
37+
38+
In this example, the actual `src` attribute is set only when the image is about to come into view, reducing the initial page load time.
39+
40+
##### Caching Strategies
41+
42+
Caching involves storing copies of resources to avoid redundant downloads. Proper caching strategies can significantly improve the speed and responsiveness of your web application.
43+
44+
**Example: Caching with Service Workers**
45+
46+
Service workers provide a powerful mechanism for caching assets and enabling offline capabilities. Here's a basic example:
47+
48+
```javascript
49+
// Service worker script (sw.js)
50+
const CACHE_NAME = 'my-cache-v1';
51+
52+
self.addEventListener('install', (event) => {
53+
event.waitUntil(
54+
caches.open(CACHE_NAME).then((cache) => {
55+
return cache.addAll([
56+
'/',
57+
'/index.html',
58+
'/styles.css',
59+
'/script.js',
60+
// ... other assets
61+
]);
62+
})
63+
);
64+
});
65+
66+
self.addEventListener('fetch', (event) => {
67+
event.respondWith(
68+
caches.match(event.request).then((response) => {
69+
return response || fetch(event.request);
70+
})
71+
);
72+
});
73+
```
74+
75+
In this example, the service worker caches essential assets during installation and intercepts fetch requests to serve cached content when available.
76+
77+
##### Combining Lazy Loading and Caching
78+
79+
Combining lazy loading and caching can provide a powerful optimization strategy. You can lazy load non-essential resources and cache essential assets for faster subsequent visits.
80+
81+
**Example: Combining Lazy Loading and Caching**
82+
83+
```javascript
84+
document.addEventListener('DOMContentLoaded', function () {
85+
// Lazy load images
86+
const lazyImages = document.querySelectorAll('img[data-src]');
87+
lazyImages.forEach(function (lazyImage) {
88+
lazyImage.src = lazyImage.dataset.src;
89+
lazyImage.removeAttribute('data-src');
90+
});
91+
92+
// Cache essential assets with a service worker
93+
if ('serviceWorker' in navigator) {
94+
navigator.serviceWorker.register('/sw.js')
95+
.then((registration) => {
96+
console.log('Service Worker registered with scope:', registration.scope);
97+
})
98+
.catch((error) => {
99+
console.error('Service Worker registration failed:', error);
100+
});
101+
}
102+
});
103+
```
104+
105+
In this combined example, lazy loading is applied to images, and a service worker is used for caching essential assets.
106+
107+
By implementing lazy loading and caching strategies, you can create web applications that load quickly, reduce bandwidth usage, and provide a seamless user experience.

0 commit comments

Comments
 (0)