133 lines
4.4 KiB
JavaScript
133 lines
4.4 KiB
JavaScript
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
|
|
// عین google-services.json/pushToken.ts اندروید — این مقادیر عمومی و امن برای
|
|
// مرورگرند. سرویسورکرِ استاتیک نمیتونه process.env بخونه، پس مستقیم اینجا نوشته
|
|
// شدن (دقیقاً همون چیزی که NEXT_PUBLIC_FIREBASE_* توی باندلِ کلاینت هم inline میشه)
|
|
importScripts("https://www.gstatic.com/firebasejs/10.13.2/firebase-app-compat.js");
|
|
importScripts("https://www.gstatic.com/firebasejs/10.13.2/firebase-messaging-compat.js");
|
|
|
|
firebase.initializeApp({
|
|
apiKey: "AIzaSyDpUh8AlafwyM931o519oX3z7JYnXZJyBg",
|
|
authDomain: "modstagram-com.firebaseapp.com",
|
|
projectId: "modstagram-com",
|
|
storageBucket: "modstagram-com.firebasestorage.app",
|
|
messagingSenderId: "767614062479",
|
|
appId: "1:767614062479:web:ec2aea23cc8ce17dfe45fa",
|
|
});
|
|
|
|
// پیامِ پوش وقتی تب بسته/پسزمینهست — عین رفتار پیشفرضِ اندروید (نوتیفِ سیستمی).
|
|
// وقتی تب باز و فعاله، Firebase این رویداد رو صدا نمیزنه — اونجا onMessage
|
|
// (pushNotifications.ts سمتِ کلاینت) با toast جایگزینش میکنه
|
|
const messaging = firebase.messaging.isSupported() ? firebase.messaging() : null;
|
|
if (messaging) {
|
|
messaging.onBackgroundMessage((payload) => {
|
|
const title = payload.notification?.title || "modstagram";
|
|
self.registration.showNotification(title, {
|
|
body: payload.notification?.body,
|
|
icon: "/images/icons/192x192.png",
|
|
badge: "/images/icons/192x192.png",
|
|
data: payload.data || {},
|
|
});
|
|
});
|
|
}
|
|
|
|
self.addEventListener("notificationclick", (event) => {
|
|
event.notification.close();
|
|
const url = "/settings/notifications";
|
|
event.waitUntil(
|
|
self.clients.matchAll({ type: "window", includeUncontrolled: true }).then((clientsArr) => {
|
|
const existing = clientsArr.find((c) => c.url.includes(url));
|
|
if (existing) return existing.focus();
|
|
return self.clients.openWindow(url);
|
|
})
|
|
);
|
|
});
|
|
|
|
const CACHE_NAME = "pwa-cache-v11";
|
|
|
|
const PRECACHE_URLS = [
|
|
"/",
|
|
"/manifest.webmanifest",
|
|
"/manifest.json",
|
|
"/images/icons/192x192.png",
|
|
"/images/icons/256x256.png",
|
|
"/images/icons/512x512.png",
|
|
"/images/icons/apple-touch-icon.png",
|
|
"/images/icons/maskable-192.png",
|
|
"/images/icons/maskable-512.png",
|
|
"/favicon.png",
|
|
];
|
|
|
|
self.addEventListener("install", (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => cache.addAll(PRECACHE_URLS))
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(
|
|
caches
|
|
.keys()
|
|
.then((keys) =>
|
|
Promise.all(
|
|
keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key))
|
|
)
|
|
)
|
|
.then(() => self.clients.claim())
|
|
);
|
|
});
|
|
|
|
self.addEventListener("fetch", (event) => {
|
|
const url = new URL(event.request.url);
|
|
|
|
if (event.request.mode === "navigate") {
|
|
event.respondWith(fetch(event.request));
|
|
return;
|
|
}
|
|
|
|
if (
|
|
url.pathname.startsWith("/images/icons/") ||
|
|
url.pathname.endsWith("manifest.webmanifest") ||
|
|
url.pathname.endsWith("manifest.json") ||
|
|
url.pathname.endsWith("favicon.png")
|
|
) {
|
|
event.respondWith(
|
|
fetch(event.request)
|
|
.then((res) => {
|
|
if (res.ok) {
|
|
const clone = res.clone();
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
|
|
}
|
|
return res;
|
|
})
|
|
.catch(() => caches.match(event.request))
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (
|
|
url.pathname.startsWith("/images/") ||
|
|
url.pathname.endsWith(".png") ||
|
|
url.pathname.endsWith(".jpg") ||
|
|
url.pathname.endsWith(".jpeg") ||
|
|
url.pathname.endsWith(".gif") ||
|
|
url.pathname.endsWith(".webp")
|
|
) {
|
|
event.respondWith(
|
|
caches.match(event.request).then((cached) => {
|
|
const networkFetch = fetch(event.request).then((res) => {
|
|
if (res.ok) {
|
|
const clone = res.clone();
|
|
caches
|
|
.open(CACHE_NAME)
|
|
.then((cache) => cache.put(event.request, clone));
|
|
}
|
|
return res;
|
|
});
|
|
return cached || networkFetch;
|
|
})
|
|
);
|
|
}
|
|
});
|