Advertisement
Guest User

Untitled

a guest
May 29th, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// <reference no-default-lib="true"/>
  2. /// <reference lib="esnext" />
  3. /// <reference lib="webworker" />
  4. /**@type {ServiceWorkerGlobalScope} self */
  5.  
  6. async function fastget({req}) {
  7.     cache_res = await self.caches.match(req)
  8.     if (cache_res) {
  9.         if (req.url === cache_res.url) {
  10.             return cache_res;
  11.         }
  12.     }
  13.  
  14.     try {
  15.         network_res = await fetch(req)
  16.         if (network_res.status == 502) {
  17.             if (req.headers.get('Accept').includes('application/json')) {
  18.                 return new Response('{"error": "SERVICE_DOWN", "http_code": 502}', {
  19.                     status: 502,
  20.                     headers: {
  21.                         "Content-Type": "application/json"
  22.                     }
  23.                 })
  24.             } else {
  25.                 return new Response(`
  26.                     <html>
  27.                         <head>
  28.                             <title>Down | Twilight</title>
  29.                             <style>
  30.                                 * {
  31.                                     color: white;
  32.                                     font-family: sans-serif;
  33.                                 }
  34.                                 body {
  35.                                     background-color: #343434;
  36.                                 }
  37.                             </style>
  38.                         </head>
  39.                         <body>
  40.                             <center>
  41.                                 <h1>Down</h1>
  42.                                 <p>It seems Twilight is down. Please wait for it to come up.</p>
  43.                             </center>
  44.                         </body>
  45.                     </html>`, {
  46.                     status: 502,
  47.                     headers: {
  48.                         "Content-Type": "text/html"
  49.                     }
  50.                 })
  51.             }
  52.         }
  53.         if (new URL(req.url).pathname.startsWith('/cdn/') || new URL(req.url).pathname.startsWith('/static/') && new URL(req.url).pathname != "/static/cache.js") {
  54.             cache = await self.caches.open('cdn_cache')
  55.             cache.put(req, network_res.clone())
  56.             console.log(`Cached from CDN (${req.url}).`)
  57.         } else if (new URL(req.url).hostname == 'fonts.gstatic.com' || new URL(req.url).hostname == 'fonts.googleapis.com') {
  58.             cache = await self.caches.open('cdn_cache')
  59.             cache.put(req, network_res.clone())
  60.             console.log(`Cached from font-store (${req.url}).`)
  61.         } else {
  62.             console.log(`Refusing to cache, not from CDN (${req.url}).`)
  63.         }
  64.         return network_res;
  65.     } catch (err) {
  66.         console.error(err)
  67.         if (req.headers.get('Accept').includes('application/json')) {
  68.             return new Response('{"error": "NO_NETWORK", "http_code": 408}', {
  69.                 status: 408,
  70.                 headers: {
  71.                     "Content-Type": "application/json"
  72.                 }
  73.             })
  74.         } else {
  75.             return new Response(`
  76.                 <html>
  77.                     <head>
  78.                         <title>Offline | Twilight</title>
  79.                         <style>
  80.                             * {
  81.                                 color: white;
  82.                                 font-family: sans-serif;
  83.                             }
  84.                             body {
  85.                                 background-color: #343434;
  86.                             }
  87.                         </style>
  88.                     </head>
  89.                     <body>
  90.                         <center>
  91.                             <h1>Offline</h1>
  92.                             <p>It seems you are offline. Please reconnect to the internet to continue.</p>
  93.                         </center>
  94.                     </body>
  95.                 </html>`, {
  96.                 status: 408,
  97.                 headers: {
  98.                     "Content-Type": "text/html"
  99.                 }
  100.             })
  101.         }
  102.     }
  103. }
  104.  
  105. self.addEventListener('install', ev => {
  106.     ev.waitUntil(self.caches.open('basecache').then(cache => {
  107.         cache.addAll([
  108.             '/favicon.ico',
  109.             '/',
  110.             '/cdn/__default_user__.webp',
  111.             '/cdn/__default_server__.webp',
  112.             '/static/ctxmenu.js'
  113.         ]).then(() => {console.log('Installed!')})
  114.     }))
  115.     ev.waitUntil(self.skipWaiting())
  116. })
  117.  
  118. self.addEventListener('activate', ev => {
  119.     ev.waitUntil(self.clients.claim())
  120. })
  121.  
  122. self.addEventListener('fetch', ev => {
  123.     ev.respondWith(fastget({
  124.         req: ev.request
  125.     }))
  126. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement