Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
innovatixhub authored Nov 22, 2024
1 parent b6cec7e commit 0efb472
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions public/_redirects
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* /index.html 200
Binary file added public/android-chrome-192x192.webp
Binary file not shown.
Binary file added public/android-chrome-512x512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/android-chrome-512x512.webp
Binary file not shown.
Binary file added public/apple-touch-icon.webp
Binary file not shown.
Binary file added public/favicon-16x16.webp
Binary file not shown.
Binary file added public/favicon-32x32.webp
Binary file not shown.
Binary file added public/favicon.webp
Binary file not shown.
Binary file added public/install-pwa-image.webp
Binary file not shown.
Binary file added public/install-pwa-mobile.webp
Binary file not shown.
56 changes: 56 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"registerType": "prompt",
"name": "E-Commercew",
"short_name": "E-Commerce",
"description": "Effortless online shopping: curated collections, easy cart & wishlist, detailed descriptions, intuitive interface. Shop smarter today.",
"start_url": "/",
"scope": "/",
"display": "standalone",
"orientation": "any",
"theme_color": "#181818",
"background_color": "#181818",
"icons": [
{
"src": "/android-chrome-192x192.webp",
"sizes": "192x192",
"type": "image/webp"
},
{
"src": "/android-chrome-512x512.webp",
"sizes": "512x512",
"type": "image/webp"
},
{
"src": "/apple-touch-icon.webp",
"sizes": "180x180",
"type": "image/webp"
},
{
"src": "/android-chrome-512x512.webp",
"sizes": "512x512",
"type": "image/webp",
"purpose": "maskable"
},
{
"src": "/android-chrome-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"screenshots": [
{
"src": "/install-pwa-image.webp",
"sizes": "1897x861",
"type": "image/webp",
"form_factor": "wide",
"label": "Wonder Widgets"
},
{
"src": "/install-pwa-mobile.webp",
"sizes": "462x728",
"type": "image/webp",
"form_factor": "narrow",
"label": "Wonder Widgets"
}
]
}
11 changes: 11 additions & 0 deletions public/register-pwa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export async function register() {
if ("serviceWorker" in navigator) {
try {
const registration = await navigator.serviceWorker.register("./sw.js");

// console.log("serviceWorker registered", registration);
} catch (e) {
console.log("failed to register serviceWorker", e);
}
}
}
80 changes: 80 additions & 0 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"use strict";

// Variables
const CACHE_NAME = "e-commerce-v8";
const ASSETS = ["/", "/index.html"];

// Functions
async function cacheAssets() {
const cache = await caches.open(CACHE_NAME);
await cache.addAll(ASSETS);
}

async function handleFetchAndCache(request) {
const cacheResponse = await caches.match(request);

const networkResponse = fetch(request)
.then(async (networkRes) => {
const shouldSkipCache = networkRes.url.includes("chrome-extension");

if (shouldSkipCache) return networkRes;

if (networkRes.ok) {
const cache = await caches.open(CACHE_NAME);
await cache.put(request, networkRes.clone());
}

return networkRes;
})
.catch((err) => {
console.error("Fetch failed:", err);
});

return cacheResponse || networkResponse;
}

async function updateCachedAssets() {
const cache = await caches.open(CACHE_NAME);

try {
const responses = await Promise.all(
ASSETS.map(async (asset) => {
const fetchedResponse = await fetch(asset);

if (fetchedResponse.ok) {
await cache.put(asset, fetchedResponse.clone());
}

return fetchedResponse;
})
);

const cacheKeys = await caches.keys();
await Promise.all(
cacheKeys
.filter((key) => key !== CACHE_NAME)
.map((key) => caches.delete(key))
);

return responses;
} catch (error) {
console.error("Failed to update cache:", error);
}
}

function handleFetchEvent(event) {
const isGetMethod = event.request.method === "GET";

event.respondWith(handleFetchAndCache(event.request));

if (isGetMethod) {
event.waitUntil(updateCachedAssets());
}
}

// Events
self.addEventListener("install", (event) => event.waitUntil(cacheAssets()));
self.addEventListener("activate", (event) =>
event.waitUntil(updateCachedAssets())
);
self.addEventListener("fetch", handleFetchEvent);

0 comments on commit 0efb472

Please sign in to comment.