147 lines
5.7 KiB
JavaScript
147 lines
5.7 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";
|
|
// عکسِ فرستنده (یا بنرِ دستیِ ادمین برای admin_broadcast) — قبلاً همیشه لوگوی ثابتِ
|
|
// اپ بود، صرفنظر از اینکه نوتیف مربوط به کدوم کاربره؛ notification.image رو
|
|
// utils/pushNotification.js بکاند از قبل میفرسته (فقط اینجا نادیده گرفته میشد)
|
|
const avatarOrBanner = payload.notification?.image;
|
|
self.registration.showNotification(title, {
|
|
body: payload.notification?.body,
|
|
icon: avatarOrBanner || "/images/icons/192x192.png",
|
|
badge: "/images/icons/192x192.png",
|
|
// بنرِ بزرگِ زیرِ متن (برای admin_broadcast که واقعاً قراره «بنر» باشه)، غیر از
|
|
// آیکونِ کوچیکِ گردِ کنارِ عنوان — مرورگرهایی که image رو ساپورت نکنن سرشون این رو
|
|
// نادیده میگیرن، پس بیخطره
|
|
image: avatarOrBanner || undefined,
|
|
data: payload.data || {},
|
|
});
|
|
});
|
|
}
|
|
|
|
self.addEventListener("notificationclick", (event) => {
|
|
event.notification.close();
|
|
// بکاند وقتی خودش دقیقاً میدونه مقصد کجاست (پیام چت، تگِ پست و...) یه مسیرِ نسبیِ
|
|
// آماده میفرسته (نگاه کن به utils/notifyUser.js/chatHelpers.js: data.link) — وگرنه
|
|
// مثلِ قبل میریم صفحهی عمومیِ اعلانها
|
|
const url = event.notification.data?.link || "/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-v12";
|
|
|
|
const PRECACHE_URLS = [
|
|
"/",
|
|
"/offline.html",
|
|
"/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).catch(() => caches.match("/offline.html"))
|
|
);
|
|
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;
|
|
})
|
|
);
|
|
}
|
|
});
|