-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yusef Almamari
committed
Aug 13, 2024
1 parent
10588a4
commit da2b27d
Showing
6 changed files
with
164 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
VITE_PUBLIC_URL=http://localhost:3000/citeease | ||
VITE_NODE_ENV=development |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
VITE_PUBLIC_URL=https://discontinuedlabs.github.io/citeease | ||
VITE_NODE_ENV=production |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* eslint-disable no-restricted-globals, no-underscore-dangle */ | ||
|
||
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(); | ||
|
||
precacheAndRoute([...self.__WB_MANIFEST]); | ||
|
||
const fileExtensionRegexp = /[^?/]+\.[^/]+$/; | ||
|
||
registerRoute( | ||
({ request, url }) => { | ||
if (request.mode !== "navigate") return false; | ||
|
||
if (url.pathname.startsWith("/_")) return false; | ||
|
||
if (url.pathname.match(fileExtensionRegexp)) return false; | ||
|
||
return true; | ||
}, | ||
createHandlerBoundToURL(`${import.meta.env.VITE_PUBLIC_URL}/index.html`) | ||
); | ||
|
||
registerRoute( | ||
({ url }) => { | ||
const path = url.pathname.split("/"); | ||
const lastSegment = path[path.length - 1]; | ||
return url.origin === self.location.origin && ["png", "jpg", "jpeg", "svg"].includes(lastSegment); | ||
}, | ||
new StaleWhileRevalidate({ | ||
cacheName: "images", | ||
plugins: [new ExpirationPlugin({ maxEntries: 50 })], | ||
}) | ||
); | ||
|
||
self.addEventListener("message", (event) => { | ||
if (event.data && event.data.type === "SKIP_WAITING") { | ||
self.skipWaiting(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const isLocalhost = Boolean( | ||
window.location.hostname === "localhost" || | ||
window.location.hostname === "[::1]" || | ||
window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/) | ||
); | ||
|
||
function registerValidSW(swUrl, config) { | ||
navigator.serviceWorker | ||
.register(swUrl) | ||
.then((registration) => { | ||
// eslint-disable-next-line no-param-reassign | ||
registration.onupdatefound = () => { | ||
const installingWorker = registration.installing; | ||
if (installingWorker == null) { | ||
return; | ||
} | ||
installingWorker.onstatechange = () => { | ||
if (installingWorker.state === "installed") { | ||
if (navigator.serviceWorker.controller) { | ||
console.log( | ||
"New content is available and will be used when all " + | ||
"tabs for this page are closed. See https://cra.link/PWA." | ||
); | ||
|
||
if (config && config.onUpdate) { | ||
config.onUpdate(registration); | ||
} | ||
} else { | ||
console.log("Content is cached for offline use."); | ||
|
||
if (config && config.onSuccess) { | ||
config.onSuccess(registration); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
}) | ||
.catch((error) => { | ||
console.error("Error during service worker registration:", error); | ||
}); | ||
} | ||
|
||
function checkValidServiceWorker(swUrl, config) { | ||
fetch(swUrl, { | ||
headers: { "Service-Worker": "script" }, | ||
}) | ||
.then((response) => { | ||
const contentType = response.headers.get("content-type"); | ||
if (response.status === 404 || (contentType != null && contentType.indexOf("javascript") === -1)) { | ||
navigator.serviceWorker.ready.then((registration) => { | ||
registration.unregister().then(() => { | ||
window.location.reload(); | ||
}); | ||
}); | ||
} else { | ||
registerValidSW(swUrl, config); | ||
} | ||
}) | ||
.catch(() => { | ||
console.log("No internet connection found. App is running in offline mode."); | ||
}); | ||
} | ||
|
||
export function register(config) { | ||
if (import.meta.env.VITE_NODE_ENV === "production" && "serviceWorker" in navigator) { | ||
const publicUrl = new URL(import.meta.env.VITE_PUBLIC_URL, window.location.href); | ||
if (publicUrl.origin !== window.location.origin) { | ||
return; | ||
} | ||
|
||
window.addEventListener("load", () => { | ||
const swUrl = `${import.meta.env.VITE_PUBLIC_URL}/service-worker.js`; | ||
|
||
if (isLocalhost) { | ||
checkValidServiceWorker(swUrl, config); | ||
|
||
navigator.serviceWorker.ready.then(() => { | ||
console.log( | ||
"This web app is being served cache-first by a service worker. To learn more, visit https://cra.link/PWA" | ||
); | ||
}); | ||
} else { | ||
registerValidSW(swUrl, config); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export function unregister() { | ||
if ("serviceWorker" in navigator) { | ||
navigator.serviceWorker.ready | ||
.then((registration) => { | ||
registration.unregister(); | ||
}) | ||
.catch((error) => { | ||
console.error(error.message); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters