Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: properly generate service-worker

thecodrr83a8c06e8f22
diff --git a/apps/web/.gitignore b/apps/web/.gitignore
index 3804dc722..7a31922fb 100644
--- a/apps/web/.gitignore
+++ b/apps/web/.gitignore
@@ -26,4 +26,5 @@ yarn-error.log*
__diff_output__
-dist
\ No newline at end of file
+dist
+public/workbox
\ No newline at end of file
diff --git a/apps/web/public/index.html b/apps/web/public/index.html
index 56770dab1..5511a40d4 100644
--- a/apps/web/public/index.html
+++ b/apps/web/public/index.html
@@ -29,8 +29,6 @@
<meta name="theme-color" content="#ffffff" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
-
- <link rel="apple-touch-icon" href="logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
diff --git a/apps/web/src/index.js b/apps/web/src/index.js
index ef49a01c0..f4e2963a0 100644
--- a/apps/web/src/index.js
+++ b/apps/web/src/index.js
@@ -1,7 +1,7 @@
import React from "react";
import { initializeDatabase } from "./common/db";
import "./index.css";
-import * as serviceWorker from "./serviceWorker";
+import * as serviceWorker from "./serviceWorkerRegistration";
import { init, showReportDialog } from "@sentry/react";
import { Integrations } from "@sentry/tracing";
import { getAppVersion } from "./utils/useVersion";
diff --git a/apps/web/src/serviceWorker.js b/apps/web/src/service-worker-registration.js
similarity index 90%
rename from apps/web/src/serviceWorker.js
rename to apps/web/src/service-worker-registration.js
index b719ab46c..fd0e8c5a6 100644
--- a/apps/web/src/serviceWorker.js
+++ b/apps/web/src/service-worker-registration.js
@@ -8,13 +8,13 @@
// resources are updated in the background.
// To learn more about the benefits of this model and instructions on how to
-// opt-in, read https://bit.ly/CRA-PWA
+// opt-in, read https://cra.link/PWA
const isLocalhost = Boolean(
window.location.hostname === "localhost" ||
// [::1] is the IPv6 localhost address.
window.location.hostname === "[::1]" ||
- // 127.0.0.1/8 is considered localhost for IPv4.
+ // 127.0.0.0/8 are considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
@@ -43,7 +43,7 @@ export function register(config) {
navigator.serviceWorker.ready.then(() => {
console.log(
"This web app is being served cache-first by a service " +
- "worker. To learn more, visit https://bit.ly/CRA-PWA"
+ "worker. To learn more, visit https://cra.link/PWA"
);
});
} else {
@@ -71,7 +71,7 @@ function registerValidSW(swUrl, config) {
// content until all client tabs are closed.
console.log(
"New content is available and will be used when all " +
- "tabs for this page are closed. See https://bit.ly/CRA-PWA."
+ "tabs for this page are closed. See https://cra.link/PWA."
);
// Execute callback
@@ -100,7 +100,9 @@ function registerValidSW(swUrl, config) {
function checkValidServiceWorker(swUrl, config) {
// Check if the service worker can be found. If it can't reload the page.
- fetch(swUrl)
+ fetch(swUrl, {
+ headers: { "Service-Worker": "script" },
+ })
.then((response) => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get("content-type");
@@ -128,8 +130,12 @@ function checkValidServiceWorker(swUrl, config) {
export function unregister() {
if ("serviceWorker" in navigator) {
- navigator.serviceWorker.ready.then((registration) => {
- registration.unregister();
- });
+ navigator.serviceWorker.ready
+ .then((registration) => {
+ registration.unregister();
+ })
+ .catch((error) => {
+ console.error(error.message);
+ });
}
}
diff --git a/apps/web/src/service-worker.js b/apps/web/src/service-worker.js
new file mode 100644
index 000000000..a0d2c5e6f
--- /dev/null
+++ b/apps/web/src/service-worker.js
@@ -0,0 +1,73 @@
+/* eslint-disable no-restricted-globals */
+
+// This service worker can be customized!
+// See https://developers.google.com/web/tools/workbox/modules
+// for the list of available Workbox modules, or add any other
+// code you'd like.
+// You can also remove this file if you'd prefer not to use a
+// service worker, and the Workbox build step will be skipped.
+
+import { clientsClaim } from "workbox-core";
+import { ExpirationPlugin } from "workbox-expiration";
+import { precacheAndRoute, createHandlerBoundToURL } from "workbox-precaching";
+import { registerRoute } from "workbox-routing";
+import { StaleWhileRevalidate } from "workbox-strategies";
+
+clientsClaim();
+
+// Precache all of the assets generated by your build process.
+// 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 }) => {
+ // 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;
+ } // If this looks like a URL for a resource, because it contains // a file extension, skip.
+
+ if (url.pathname.match(fileExtensionRegexp)) {
+ return false;
+ } // Return true to signal that we want to use the handler.
+
+ return true;
+ },
+ createHandlerBoundToURL(process.env.PUBLIC_URL + "/index.html")
+);
+
+// An example runtime caching route for requests that aren't handled by the
+// precache, in this case same-origin .png requests like those from in public/
+registerRoute(
+ // Add in any other file extensions or routing criteria as needed.
+ ({ url }) =>
+ url.origin === self.location.origin && url.pathname.endsWith(".png"), // Customize this strategy as needed, e.g., by changing to CacheFirst.
+ new StaleWhileRevalidate({
+ cacheName: "images",
+ plugins: [
+ // Ensure that once this runtime cache reaches a maximum size the
+ // least-recently used images are removed.
+ new ExpirationPlugin({ maxEntries: 50 }),
+ ],
+ })
+);
+
+// This allows the web app to trigger skipWaiting via
+// registration.waiting.postMessage({type: 'SKIP_WAITING'})
+self.addEventListener("message", (event) => {
+ if (event.data && event.data.type === "SKIP_WAITING") {
+ self.skipWaiting();
+ }
+});
+
+// Any other custom service worker logic can go here.