{"id":73405,"date":"2025-07-25T15:51:29","date_gmt":"2025-07-25T10:21:29","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=73405"},"modified":"2025-07-30T13:20:39","modified_gmt":"2025-07-30T07:50:39","slug":"understanding-service-worker-caching","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/understanding-service-worker-caching\/","title":{"rendered":"Understanding service worker caching"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>We often hear about service workers in the context of caching techniques, their importance in PWAs, and overall optimization. They enable us to create offline experiences within websites or applications, perform background sync activities, and create push notifications, among other features.<\/p>\n<div id=\"attachment_73393\" style=\"width: 522px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-73393\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-73393 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/07\/bg-sw-1.png\" alt=\"Understanding Service Worker Caching\" width=\"512\" height=\"341\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/07\/bg-sw-1.png 512w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/bg-sw-1-300x200.png 300w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><p id=\"caption-attachment-73393\" class=\"wp-caption-text\">Understanding Service Worker Caching<\/p><\/div>\n<p>Let us understand what service worker is and how to implement it in websites or applications.<\/p>\n<h3>What is a Service Worker?<\/h3>\n<p>According to the official definition, Service Worker is as a proxy server that sits between browser and the network layer. It intercepts network requests, can take action based on network availability, updating assets according to the requirement.<\/p>\n<p>A service worker is basically a JavaScript code that runs on a separate thread in the background, without disturbing the main thread. It captures network requests, can do some operation, and provide caching and offline functionality. It acts as a middleman between the browser and the server.<\/p>\n<h3>Key features<\/h3>\n<ul>\n<li>To prevent man-in-the-middle attacks, Service workers run only on HTTPS protocol and not HTTP.<\/li>\n<li>They are event-driven workers, based on an event-based architecture.<\/li>\n<li>A service worker does not have access to the DOM or Web APIs.<\/li>\n<li>It is registered against a specific origin and path.<\/li>\n<li>Not every browser supports service workers, so compatibility must be checked.<\/li>\n<\/ul>\n<h3>How Does Service Worker Caching Work?<\/h3>\n<ul>\n<li>The service worker intercepts requests made by the page.<\/li>\n<li>It checks the cache for the requested asset.<\/li>\n<li>If the asset is available, it is served to the page from the cache, without making a network call.<\/li>\n<li>If the asset is not available, a network call is made to fetch it.<\/li>\n<li>Once the asset is fetched, it is saved in the cache and served to the page.<\/li>\n<\/ul>\n<div id=\"attachment_73398\" style=\"width: 608px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-73398\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-73398\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/07\/sw-workflow.png\" alt=\"Service Worker Workflow\" width=\"598\" height=\"264\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/07\/sw-workflow.png 725w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/sw-workflow-300x133.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/sw-workflow-624x276.png 624w\" sizes=\"(max-width: 598px) 100vw, 598px\" \/><p id=\"caption-attachment-73398\" class=\"wp-caption-text\">Service Worker Workflow<\/p><\/div>\n<h3>How to implement a Service Worker?<\/h3>\n<p>To start with, we are fetching three resources from the network: a script file, css file, and an image file. These resources will be cached as a backup and can be used when there is a network failure or API response errors.<\/p>\n<p>At the time of registering the service worker, several events are triggered in a sequence. The first event is &#8220;install&#8221;, followed by &#8220;activate&#8221; and other events like fetch. These are used to implement caching .<\/p>\n<p><strong>Step 1: Register the sw.js file<\/strong><\/p>\n<p>First, create a one file for service worker logic. We will name this sw.js. Now, from script.js file, we will register the sw.js fle. In the sw.js file, we will listen to the key events to implement caching. We need to check whether service workers are supported in the browser. If supported, we register the sw.js file from our script.js file.<\/p>\n<div id=\"attachment_73394\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-73394\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-73394 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/07\/register-sw-1024x726.png\" alt=\"Register Service Worker\" width=\"625\" height=\"443\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/07\/register-sw-1024x726.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/register-sw-300x213.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/register-sw-768x545.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/register-sw-624x443.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/register-sw.png 1500w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-73394\" class=\"wp-caption-text\">Register Service Worker<\/p><\/div>\n<div id=\"attachment_73407\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-73407\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-73407\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/07\/sw-registered-1024x509.png\" alt=\"Service Worker Registered\" width=\"625\" height=\"311\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/07\/sw-registered-1024x509.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/sw-registered-300x149.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/sw-registered-768x382.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/sw-registered-1536x764.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/sw-registered-624x310.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/sw-registered.png 1926w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-73407\" class=\"wp-caption-text\">Service Worker Registered<\/p><\/div>\n<p><strong>Step 2: Include the files path to cache with a unique cache name<\/strong><\/p>\n<p>A unique name is given to the cache and the list of URLs we want to cache are added to the cache. During the install event, a new cache with unique name is opened and all the required URLs are added one by one.<\/p>\n<div id=\"attachment_73395\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-73395\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-73395\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/07\/install-sw-1024x700.png\" alt=\"Install Service Worker\" width=\"625\" height=\"427\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/07\/install-sw-1024x700.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/install-sw-300x205.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/install-sw-768x525.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/install-sw-624x427.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/install-sw.png 1500w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-73395\" class=\"wp-caption-text\">Install Service Worker<\/p><\/div>\n<p><strong>Step 3: Listen to the Fetch event<\/strong><\/p>\n<p>We add an event listener for the fetch event to listen to all network calls. There are two common approaches to handling the fetch event:<\/p>\n<ul>\n<li><strong>Network First, Cache Fallback:<\/strong> When a resource is requested, it is initially fetch from the network. After the resource is fetched, the cache is updated with the new resource, and the information is served to the page. The cache acts as a backup if the network fails during the request or there are some API response error from the back end.<\/li>\n<li><strong>Cache First, Network Fallback:<\/strong> In this approach, we first check if the cache has the resource for the request. If found, we respond using the cached version. If not, we make a network call and respond with the fetched resource.<\/li>\n<\/ul>\n<div id=\"attachment_73396\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-73396\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-73396\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/07\/fetch-sw-1024x778.png\" alt=\"Fetch Network Request\" width=\"625\" height=\"475\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/07\/fetch-sw-1024x778.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/fetch-sw-300x228.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/fetch-sw-768x584.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/fetch-sw-624x474.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/fetch-sw.png 1500w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-73396\" class=\"wp-caption-text\">Fetch Network Request<\/p><\/div>\n<p><strong>Step 4: Clean up old caches<\/strong><\/p>\n<p>In cache memory, it&#8217;s possible to store several versions of cached information. Clearing outdated caches that are unnecessary is essential. This cleanup usually occurs during the activate event.<\/p>\n<div id=\"attachment_73397\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-73397\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-73397\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/07\/activate-sw-1024x674.png\" alt=\"Activate Service Worker\" width=\"625\" height=\"411\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/07\/activate-sw-1024x674.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/activate-sw-300x198.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/activate-sw-768x506.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/activate-sw-624x411.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/activate-sw.png 1500w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-73397\" class=\"wp-caption-text\">Activate Service Worker<\/p><\/div>\n<h4>Network requests before Service Worker<\/h4>\n<div id=\"attachment_73403\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-73403\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-73403\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/07\/without-sw-1024x330.png\" alt=\"Network Request before Service Worker\" width=\"625\" height=\"201\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/07\/without-sw-1024x330.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/without-sw-300x97.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/without-sw-768x247.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/without-sw-1536x494.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/without-sw-624x201.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/without-sw.png 1917w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-73403\" class=\"wp-caption-text\">Network Request before Service Worker<\/p><\/div>\n<h4>Network requests after Service Worker<\/h4>\n<div id=\"attachment_73404\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-73404\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-73404\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/07\/with-sw-1-1024x348.png\" alt=\"Network Request after Service Worker\" width=\"625\" height=\"212\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/07\/with-sw-1-1024x348.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/with-sw-1-300x102.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/with-sw-1-768x261.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/with-sw-1-1536x522.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/with-sw-1-624x212.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/with-sw-1.png 1917w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-73404\" class=\"wp-caption-text\">Network Request after Service Worker<\/p><\/div>\n<h4>Cache stored in Cache Storage<\/h4>\n<div id=\"attachment_73402\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-73402\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-73402\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/07\/cache-storage-sw-1024x510.png\" alt=\"Cache stored in Cache Storage\" width=\"625\" height=\"311\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/07\/cache-storage-sw-1024x510.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/cache-storage-sw-300x150.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/cache-storage-sw-768x383.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/cache-storage-sw-1536x766.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/cache-storage-sw-624x311.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/cache-storage-sw.png 1926w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-73402\" class=\"wp-caption-text\">Cache stored in Cache Storage<\/p><\/div>\n<h3>Conclusion<\/h3>\n<p>Service worker is a great feature to improve web performance and providing superior offline user experiences. By using service worker caching correctly, we can optimize the load time and make our applications more fast and responsive.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction We often hear about service workers in the context of caching techniques, their importance in PWAs, and overall optimization. They enable us to create offline experiences within websites or applications, perform background sync activities, and create push notifications, among other features. Let us understand what service worker is and how to implement it in [&hellip;]<\/p>\n","protected":false},"author":1284,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":123},"categories":[5876],"tags":[118,3259,55,7636,477,2683,7635],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/73405"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1284"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=73405"}],"version-history":[{"count":8,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/73405\/revisions"}],"predecessor-version":[{"id":73744,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/73405\/revisions\/73744"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=73405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=73405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=73405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}