|
| 1 | +#### Storage |
| 2 | + |
| 3 | +##### cookie,localStorage,sessionStorage,indexDB |
| 4 | + |
| 5 | +| features | cookie | localStorage | sessionStorage | indexDB | |
| 6 | +| :----------: | :----------------------------------------: | :----------------------: | :------------: | :----------------------: | |
| 7 | +| life cycle of data | generally generated by the server, but you can set the expiration time | unless cleared manually, it always exists | once the browser tab is closed, it will be cleaned up immediately | unless cleared manually, it always exists | |
| 8 | +| Storage size of data | 4K | 5M | 5M | unlimited | |
| 9 | +| Communicate with server | it is carried in the header everytime, and has a performance impact on the request | doesn't participate | doesn't participate | doesn't participate | |
| 10 | + |
| 11 | + |
| 12 | +As we can see from the above table, cookies are no longer recommended for storage. We can use localStorage and sessionStorage if we don't have much data to storage. Use localStorage to storage the data that doesn't change much, otherwise sessionStorage can be used. |
| 13 | + |
| 14 | +##### Service Worker |
| 15 | + |
| 16 | +> Service workers essentially act as proxy servers that sit between web applications, the browser, and the network (when available). They are intended, among other things, to enable the creation of effective offline experiences, intercept network requests and take appropriate action based on whether the network is available, and update assets residing on the server. They will also allow access to push notifications and background sync APIs. |
| 17 | +
|
| 18 | +At present, this technology is usually used to cache files, increase the render speed of the first screen, wo can try to implement this function. |
| 19 | + |
| 20 | +```js |
| 21 | +// index.js |
| 22 | +if (navigator.serviceWorker) { |
| 23 | + navigator.serviceWorker |
| 24 | + .register("sw.js") |
| 25 | + .then(function(registration) { |
| 26 | + console.log("service worker 注册成功"); |
| 27 | + }) |
| 28 | + .catch(function(err) { |
| 29 | + console.log("servcie worker 注册失败"); |
| 30 | + }); |
| 31 | +} |
| 32 | +// sw.js |
| 33 | +// Listen for the `install` event, and cache the required files in the callback |
| 34 | +self.addEventListener("install", e => { |
| 35 | + e.waitUntil( |
| 36 | + caches.open("my-cache").then(function(cache) { |
| 37 | + return cache.addAll(["./index.html", "./index.js"]); |
| 38 | + }) |
| 39 | + ); |
| 40 | +}); |
| 41 | + |
| 42 | +// intercept all the request events |
| 43 | +// use the cache directly if the requested data already existed in the cache; otherwise, send requests for data |
| 44 | +self.addEventListener("fetch", e => { |
| 45 | + e.respondWith( |
| 46 | + caches.match(e.request).then(function(response) { |
| 47 | + if (response) { |
| 48 | + return response; |
| 49 | + } |
| 50 | + console.log("fetch source"); |
| 51 | + }) |
| 52 | + ); |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +Start the page, we can see that the Service Worker has started in the `Application` of the devTools |
| 57 | + |
| 58 | +In the Cache, we can also find that the files we need have been cached |
| 59 | + |
| 60 | +Refreshing the page, we can see that our cached data is read from the Service Worker |
0 commit comments