<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Divyansh Gupta</title>
    <description>The latest articles on Forem by Divyansh Gupta (@divyansh7924).</description>
    <link>https://forem.com/divyansh7924</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F336411%2Fe3721527-ea2f-4811-a0e7-b5b6c78d895c.jpeg</url>
      <title>Forem: Divyansh Gupta</title>
      <link>https://forem.com/divyansh7924</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/divyansh7924"/>
    <language>en</language>
    <item>
      <title>Configuring Workbox with Create React App</title>
      <dc:creator>Divyansh Gupta</dc:creator>
      <pubDate>Sun, 16 Feb 2020 09:54:32 +0000</pubDate>
      <link>https://forem.com/divyansh7924/customizing-workbox-with-create-react-app-1ada</link>
      <guid>https://forem.com/divyansh7924/customizing-workbox-with-create-react-app-1ada</guid>
      <description>&lt;p&gt;There are many ways to integrate Workbox in CRA, you can use a library like &lt;strong&gt;react-app-rewired&lt;/strong&gt; or eject react-scripts for taking full control over your configuration for your web app. But we are going to use a small dev dependency called &lt;strong&gt;workbox-build&lt;/strong&gt; for this.&lt;/p&gt;

&lt;h2&gt;
  
  
  1 - Add these 2 files to your src folder
&lt;/h2&gt;

&lt;h3&gt;
  
  
  sw-build.js
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const workboxBuild = require('workbox-build');
// NOTE: This should be run *AFTER* all your assets are built
const buildSW = () =&amp;gt; {
  // This will return a Promise
  workboxBuild
    .injectManifest({
      swSrc: 'src/sw-template.js', // this is your sw template file
      swDest: 'build/service-worker.js', // this will be created in the build step
      globDirectory: 'build',
      globPatterns: ['**/*.{jpg}'], // precaching jpg files
    })
    .then(({ count, size, warnings }) =&amp;gt; {
      // Optionally, log any warnings and details.
      warnings.forEach(console.warn);
      console.log(`${count} files will be precached, totaling ${size} bytes.`);
    })
    .catch(console.error);
};
buildSW();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file will run on every build and will inject your custom service worker from sw-template.js in place.&lt;/p&gt;

&lt;h3&gt;
  
  
  sw-template.js
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (typeof importScripts === 'function') {
  importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.0.0/workbox-sw.js');
  /* global workbox */
  if (workbox) {
    console.log('Workbox is loaded');
    workbox.core.skipWaiting();

    /* injection point for manifest files.  */
    workbox.precaching.precacheAndRoute(self.__WB_MANIFEST);

    /* custom cache rules */
     workbox.routing.registerRoute(
      new workbox.routing.NavigationRoute(
        new workbox.strategies.NetworkFirst({
          cacheName: 'PRODUCTION',
        })
      )
    );
  } else {
    // console.log('Workbox could not be loaded. No Offline support');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file is our custom service worker and uses Network first strategy. Read more about workbox caching strategies &lt;a href="https://developers.google.com/web/tools/workbox/modules/workbox-strategies"&gt;here&lt;/a&gt;.&lt;br&gt;
 You can try out caching fonts also using CacheFirst strategy.&lt;/p&gt;

&lt;h2&gt;
  
  
  2 - Install workbox-build
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev workbox-build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3 - Add the following to your scripts in package.json
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"build-sw": "node ./src/sw-build.js",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4 - Add npm run build-sw to your build script in package.json like this
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"build": "react-scripts build &amp;amp;&amp;amp; npm run build-sw",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally in your index.js replace the serviceWorker.unregister() to serviceWorker.register()&lt;/p&gt;

&lt;h2&gt;
  
  
  And there we have it
&lt;/h2&gt;

&lt;p&gt;We have successfully integrated workbox in our React APP. You can find the Github repo &lt;a href="https://github.com/divyansh7924/workbox-example-with-cra"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>pwa</category>
      <category>workbox</category>
      <category>caching</category>
    </item>
  </channel>
</rss>
