Skip to content

Commit

Permalink
update: service worker strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshd332 committed Jun 18, 2024
1 parent c787a45 commit 264f394
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
10 changes: 10 additions & 0 deletions public/_headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/index.html
cache-control: max-age=0
cache-control: no-cache
cache-control: no-store
cache-control: must-revalidate
/service-worker.js
cache-control: max-age=0
cache-control: no-cache
cache-control: no-store
cache-control: must-revalidate
41 changes: 13 additions & 28 deletions src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

import { clientsClaim } from "workbox-core";
import { ExpirationPlugin } from "workbox-expiration";
import { precacheAndRoute, createHandlerBoundToURL } from "workbox-precaching";
import { precacheAndRoute } from "workbox-precaching";
import { registerRoute } from "workbox-routing";
import { StaleWhileRevalidate } from "workbox-strategies";
import { NetworkFirst } from "workbox-strategies";

declare const self: ServiceWorkerGlobalScope;

Expand All @@ -22,35 +23,17 @@ clientsClaim();
// Their URLs are injected into the manifest variable below.
// This variable must be present somewhere in your service worker file,
// even if you decide not to use precaching. See https://cra.link/PWA
precacheAndRoute(self.__WB_MANIFEST);

// Set up App Shell-style routing, so that all navigation requests
// are fulfilled with your index.html shell. Learn more at
// https://developers.google.com/web/fundamentals/architecture/app-shell
const fileExtensionRegexp = new RegExp("/[^/?]+\\.[^/]+$");
registerRoute(
// Return false to exempt requests from being fulfilled by index.html.
({ request, url }: { request: Request; url: URL }) => {
// If this isn't a navigation, skip.
if (request.mode !== "navigate") {
return false;
}

// If this is a URL that starts with /_, skip.
if (url.pathname.startsWith("/_")) {
return false;
}
const toPrecache = self.__WB_MANIFEST.filter((file) =>
typeof file === "string"
? !file.includes("index.html")
: !file.url.includes("index.html")
);

// If this looks like a URL for a resource, because it contains
// a file extension, skip.
if (url.pathname.match(fileExtensionRegexp)) {
return false;
}
precacheAndRoute(toPrecache);

// Return true to signal that we want to use the handler.
return true;
},
createHandlerBoundToURL(process.env.PUBLIC_URL + "/index.html")
registerRoute(
({ url }) => url.pathname.includes("index.html"),
new NetworkFirst()
);

// An example runtime caching route for requests that aren't handled by the
Expand All @@ -60,6 +43,8 @@ registerRoute(
({ url }) =>
url.origin === self.location.origin &&
(url.pathname.endsWith(".png") ||
url.pathname.endsWith(".svg") ||
url.pathname.endsWith(".webp") ||
url.pathname.endsWith(".glb") ||
url.pathname.endsWith(".gltf") ||
url.pathname.endsWith(".bin") ||
Expand Down

0 comments on commit 264f394

Please sign in to comment.