 {"id":305,"date":"2019-08-26T11:49:43","date_gmt":"2019-08-26T18:49:43","guid":{"rendered":"https:\/\/www.mavice.com\/blog\/?p=305"},"modified":"2020-12-17T17:16:59","modified_gmt":"2020-12-18T01:16:59","slug":"aem-caching-with-service-workers-and-googles-workbox-library","status":"publish","type":"post","link":"https:\/\/www.mavice.com\/blog\/aem-caching-with-service-workers-and-googles-workbox-library\/","title":{"rendered":"AEM: Caching with Service Workers and Google\u2019s Workbox Library"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"465\" height=\"108\" src=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/google-workbox.png\" alt=\"google workbox logo\" class=\"wp-image-308\" srcset=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/google-workbox.png 465w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/google-workbox-300x70.png 300w\" sizes=\"(max-width: 465px) 100vw, 465px\" \/><\/figure><\/div>\n\n\n\n<p>Caching\n has become a norm in pretty much all modern web applications. Caching \nstatic assets improves the load time of web applications by reducing the\n calls that it needs to make to retrieve resources, and by also reducing\n network congestion. If you have static assets that are rarely updated, \nwhy not cache them? \ud83d\ude42<\/p>\n\n\n\n<p>There are many ways you can implement caching into your web application, in this article we will be using Service Workers. <a rel=\"noreferrer noopener\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Service_Worker_API\" target=\"_blank\">Service Workers<\/a>  run in the background within its own script, separate from a web page.  Including caching, there are many features that are implemented with  Service Workers, such as: Push Notifications, Background Sync, and  storing data in the browsers IndexedDB.<\/p>\n\n\n\n<p><strong>Google\u2019s Workbox<br><\/strong><a rel=\"noreferrer noopener\" href=\"https:\/\/developers.google.com\/web\/tools\/workbox\/\" target=\"_blank\">WorkBox<\/a>  is a JavaScript Library that simplifies and optimizes the code needed  to implement caching within a Service Worker. A highly recommend anyone  who isn\u2019t familiar with Caching or Service Workers to check the <a rel=\"noreferrer noopener\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Service_Worker_API\/Using_Service_Workers\" target=\"_blank\">Docs<\/a> first before moving forward with WorkBox.<\/p>\n\n\n\n<p>This\n article is based off the assumption that you have an AEM 6.4 project \nsetup. If you don\u2019t have a project setup, check out this <a href=\"https:\/\/docs.adobe.com\/content\/help\/en\/experience-manager-learn\/getting-started-wknd-tutorial-develop\/overview.html\" target=\"_blank\" rel=\"noreferrer noopener\">guide<\/a>.<\/p>\n\n\n\n<p>After setting up your project, your folder structure should look similar to this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>testapp\/\n| \u2014 core\/\n| \u2014 it.launcher\/\n| \u2014 it.tests\/\n| \u2014 pom.xml\n| \u2014 gulpfile.js\n| \u2014 README.md\n| \u2014 ui.apps\/\n| \u2014 ui.content\/<\/code><\/pre>\n\n\n\n<p>Within\n a custom AEM component, we are going to add the required code to run \nour service worker. If you haven\u2019t created a custom component, check out\n this <a href=\"https:\/\/docs.adobe.com\/content\/help\/en\/experience-manager-learn\/getting-started-wknd-tutorial-develop\/custom-component.html\" target=\"_blank\" rel=\"noreferrer noopener\">guide<\/a>.<\/p>\n\n\n\n<p>Within the ui.apps folder, find the projects clientlib-site folder, it should be in a similar location like the example below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"367\" height=\"175\" src=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-clientlibs-folder.png\" alt=\"aem clientlib-site folder structure\" class=\"wp-image-309\" srcset=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-clientlibs-folder.png 367w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-clientlibs-folder-300x143.png 300w\" sizes=\"(max-width: 367px) 100vw, 367px\" \/><\/figure><\/div>\n\n\n\n<p>In clientlib-site, the folder structure should look similar to this:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"471\" height=\"191\" src=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-clientlib-structure.png\" alt=\"aem clientlib-site folder structure\" class=\"wp-image-310\" srcset=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-clientlib-structure.png 471w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-clientlib-structure-300x122.png 300w\" sizes=\"(max-width: 471px) 100vw, 471px\" \/><\/figure><\/div>\n\n\n\n<p>In the JS Folder, let\u2019s add a javascript file that will load our Service Worker whenever the site is loaded.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"245\" height=\"68\" src=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-js-folder.png\" alt=\"aem js folder app.js\" class=\"wp-image-311\"\/><\/figure><\/div>\n\n\n\n<p>Be sure to add the JavaScript file to the ClientLibs js.txt file so that it is compiled. Here is an example:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"268\" height=\"141\" src=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-js-txt.png\" alt=\"aem ClientLibs js.txt file\" class=\"wp-image-312\"\/><\/figure><\/div>\n\n\n\n<p>Next, let\u2019s add a Service Worker JS file to our project. Our service-worker.js file will be located inside of the content folder within ui-apps. Here is an example of what the folder structure might look like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ui-apps\/\n| \u2014 {project-name}\/\n| \u2014 \u2014 src\/\n| \u2014 \u2014 \u2014 main\/\n| \u2014 \u2014 \u2014 \u2014 content\/\n| \u2014 \u2014 \u2014 \u2014 \u2014 jcr_root\/\n| \u2014 \u2014 \u2014 \u2014 \u2014 \u2014 service-worker.js<\/code><\/pre>\n\n\n\n<p>Great! Now that we have our files in place, lets add the code for our  Service Worker. For the purpose of this article, I will provide you with  the necessary code for the Service Worker. You can find the Getting  Started information for Workbox <a rel=\"noreferrer noopener\" href=\"https:\/\/developers.google.com\/web\/tools\/workbox\/guides\/get-started\" target=\"_blank\">here<\/a>. In your app.js file, add this block of code:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"788\" height=\"205\" src=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-service-worker.png\" alt=\"aem app.js file adding ServiceWorker\" class=\"wp-image-313\" srcset=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-service-worker.png 788w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-service-worker-300x78.png 300w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-service-worker-768x200.png 768w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-service-worker-640x166.png 640w\" sizes=\"(max-width: 788px) 100vw, 788px\" \/><\/figure><\/div>\n\n\n\n<p>This block of code registers are Service Worker at the root of our project.<\/p>\n\n\n\n<p>In our service-worker.js file, add this block of code:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"733\" height=\"867\" src=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-serviceworker-caching.png\" alt=\"aem service-worker.js add caching\" class=\"wp-image-314\" srcset=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-serviceworker-caching.png 733w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-serviceworker-caching-254x300.png 254w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-serviceworker-caching-640x757.png 640w\" sizes=\"(max-width: 733px) 100vw, 733px\" \/><\/figure><\/div>\n\n\n\n<p>Lastly,\n since the Service Worker file needs to be included at the root of our \nproject to properly function with WorkBox, we will need to add it in \nfilter.xml so it can be packaged correctly.<\/p>\n\n\n\n<p>Find your filter.xml file at \/ui.apps\/{project_name}\/src\/main\/content\/META-INF\/vault\/filter.xml, and add the following :<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"334\" height=\"158\" src=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-filter-xml.png\" alt=\"aem filter.xml file\" class=\"wp-image-315\" srcset=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-filter-xml.png 334w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-filter-xml-300x142.png 300w\" sizes=\"(max-width: 334px) 100vw, 334px\" \/><\/figure><\/div>\n\n\n\n<p>For more information on customizing your Maven Builds, check these <a href=\"https:\/\/helpx.adobe.com\/experience-manager\/6-2\/sites\/developing\/using\/ht-projects-maven.html\" target=\"_blank\" rel=\"noreferrer noopener\">Docs<\/a>.<\/p>\n\n\n\n<p>Build your project with the following command (adjust to your local setup):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mvn clean install -PautoInstallPackage -Daem.host=localhost -Daem.port=4502 -Dsling.user=admin -Dsling.password=admin -D skipTests=true<\/code><\/pre>\n\n\n\n<p>Now, whenever you have a component that loads the categories that is defined in your content.xml for clientlib-site, it will load the service worker and begin caching JS, CSS, and Images.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"1024\" height=\"369\" src=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-logger-1024x369.png\" alt=\"AEM logger showing service worker caching js, css and images\" class=\"wp-image-316\" srcset=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-logger-1024x369.png 1024w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-logger-300x108.png 300w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-logger-768x277.png 768w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-logger-1040x375.png 1040w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-logger-640x231.png 640w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2019\/11\/aem-logger.png 1688w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Caching  has become a norm in pretty much all modern web applications. Caching  static assets improves the load time of web applications by reducing the  calls that it needs to make to retrieve resources, and by also reducing  network congestion.<\/p>\n","protected":false},"author":2,"featured_media":366,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[15,10,21,24,22,20,23],"_links":{"self":[{"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/posts\/305"}],"collection":[{"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":4,"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":394,"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/posts\/305\/revisions\/394"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/media\/366"}],"wp:attachment":[{"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}