1// Firebase configs by environment 2const FIREBASE_CONFIGS = { 3 production: { 4 apiKey: "AIzaSyC7VvXKRFVYfB_vGFUI3_Q2r4OPyTHwFHU", 5 authDomain: "texas-monthly-app.firebaseapp.com", 6 databaseURL: "https://texas-monthly-app-default-rtdb.firebaseio.com", 7 projectId: "texas-monthly-app", 8 storageBucket: "texas-monthly-app.appspot.com", 9 messagingSenderId: "548360159151", 10 appId: "1:548360159151:web:4e6fef6ebf3708eac2795a", 11 measurementId: "G-LGSQBXTG03", 12 }, 13 staging: { 14 apiKey: "AIzaSyC7VvXKRFVYfB_vGFUI3_Q2r4OPyTHwFHU", 15 authDomain: "texas-monthly-app.firebaseapp.com", 16 databaseURL: "https://texas-monthly-app-default-rtdb.firebaseio.com", 17 projectId: "texas-monthly-app", 18 storageBucket: "texas-monthly-app.appspot.com", 19 messagingSenderId: "548360159151", 20 appId: "1:548360159151:web:4e6fef6ebf3708eac2795a", 21 measurementId: "G-LGSQBXTG03", 22 }, 23 dev: { 24 apiKey: "AIzaSyB9glDh1AF5O0aZBRgp4I9yO9ucAB1lE60", 25 authDomain: "id.texasmonthly.dev", 26 projectId: "texas-monthly-dev-78681", 27 storageBucket: "texas-monthly-dev-78681.appspot.com", 28 messagingSenderId: "508876919510", 29 appId: "1:508876919510:web:7056614e741f2bcbe36f77", 30 measurementId: "G-9DKKCZ2B40", 31 }, 32}; 33 34/** 35 * Determine the environment from the service worker's URL search params. 36 * The main page passes ?env=<TM_ENV> when registering the service worker. 37 * Falls back to inferring from the hostname, then defaults to 'dev'. 38 */ 39function getEnvironment() { 40 const params = new URL(self.location.href).searchParams; 41 const envParam = params.get("env"); 42 43 if (envParam === "production" || envParam === "staging") { 44 return envParam; 45 } 46 47 if (envParam) { 48 return "dev"; 49 } 50 51 // Fallback: infer from hostname 52 if ( 53 self.location.hostname === "www.texasmonthly.com" || 54 self.location.hostname === "texasmonthly.com" 55 ) { 56 return "production"; 57 } 58 59 return "dev"; 60} 61 62const ENV = getEnvironment(); 63const firebaseConfig = FIREBASE_CONFIGS[ENV] || FIREBASE_CONFIGS.dev; 64 65console.debug(`Firebase service worker using "${ENV}" environment`); 66 67// Add an event listener for the install event 68self.addEventListener("install", function (event) { 69 console.debug("Firebase service worker installed"); 70 // The promise that skipWaiting() returns can be safely ignored. 71 self.skipWaiting(); 72}); 73 74self.addEventListener("activate", function (event) { 75 console.debug("Firebase service worker activated"); 76 // The promise that clients.claim() returns can be safely ignored. 77 event.waitUntil(self.clients.claim()); 78}); 79 80self.addEventListener("notificationclick", (event) => { 81 // Get the URL from the notification. 82 const destination_url = 83 event.notification.data.url || event.notification.data.FCM_MSG.data.url; 84 event.notification.close(); 85 86 if (destination_url) { 87 event.waitUntil( 88 clients 89 .matchAll({ 90 type: "window", 91 }) 92 .then((clientList) => { 93 // This looks to see if the current is already open and focuses if it is. 94 for (const client of clientList) { 95 if (client.url === destination_url && "focus" in client) 96 return client.focus(); 97 } 98 if (clients.openWindow && destination_url !== null) 99 return clients.openWindow(destination_url); 100 }), 101 ); 102 } 103}); 104 105// Scripts for firebase and firebase messaging 106importScripts( 107 "https://www.gstatic.com/firebasejs/10.8.0/firebase-app-compat.js", 108); 109importScripts( 110 "https://www.gstatic.com/firebasejs/10.8.0/firebase-messaging-compat.js", 111); 112 113// Initialize the Firebase app in the service worker with the environment-specific config 114firebase.initializeApp(firebaseConfig); 115 116// Retrieve firebase messaging 117const messaging = firebase.messaging(); 118 119/** 120 * Messages coming from Firebase Console use payload.notification. 121 * NOTE: If using the firebase console, theres no need to call showNotification. 122 * When we switch over to using the plugin, we can uncomment the code below. 123 * 124 * Messages coming from the API(or the Firebase plugin) use payload.data. 125 * We'll try to use payload.data, but if it's not there, we'll use payload.notification. 126 */
127messaging.onBackgroundMessage(function (payload) { 128 // const notificationTitle = payload.data.title ? payload.data.title : payload.notification.title; 129 // const notificationOptions = { 130 // body: payload.data.body || payload.notification.body, 131 // icon: payload.data.imageUrl || payload.notification.image, 132 // image: payload.data.imageUrl || payload.notification.image, 133 // data: payload.data.url ? { url: payload.data.url } : null, 134 // }; 135 // if (notificationTitle && payload.data) { 136 // self.registration.showNotification(notificationTitle, 137 // notificationOptions); 138 // } 139});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.