kyber

Форк
0
/
service-worker.ts 
80 строк · 2.9 Кб
1
/// <reference lib="webworker" />
2
/* eslint-disable no-restricted-globals */
3

4
// This service worker can be customized!
5
// See https://developers.google.com/web/tools/workbox/modules
6
// for the list of available Workbox modules, or add any other
7
// code you'd like.
8
// You can also remove this file if you'd prefer not to use a
9
// service worker, and the Workbox build step will be skipped.
10

11
import { clientsClaim } from 'workbox-core'
12
import { ExpirationPlugin } from 'workbox-expiration'
13
import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching'
14
import { registerRoute } from 'workbox-routing'
15
import { StaleWhileRevalidate } from 'workbox-strategies'
16

17
declare const self: ServiceWorkerGlobalScope
18

19
clientsClaim()
20

21
// Precache all of the assets generated by your build process.
22
// Their URLs are injected into the manifest variable below.
23
// This variable must be present somewhere in your service worker file,
24
// even if you decide not to use precaching. See https://cra.link/PWA
25
precacheAndRoute(self.__WB_MANIFEST)
26

27
// Set up App Shell-style routing, so that all navigation requests
28
// are fulfilled with your index.html shell. Learn more at
29
// https://developers.google.com/web/fundamentals/architecture/app-shell
30
const fileExtensionRegexp = new RegExp('/[^/?]+\\.[^/]+$')
31
registerRoute(
32
  // Return false to exempt requests from being fulfilled by index.html.
33
  ({ request, url }: { request: Request; url: URL }) => {
34
    // If this isn't a navigation, skip.
35
    if (request.mode !== 'navigate') {
36
      return false
37
    }
38

39
    // If this is a URL that starts with /_, skip.
40
    if (url.pathname.startsWith('/_')) {
41
      return false
42
    }
43

44
    // If this looks like a URL for a resource, because it contains
45
    // a file extension, skip.
46
    if (url.pathname.match(fileExtensionRegexp)) {
47
      return false
48
    }
49

50
    // Return true to signal that we want to use the handler.
51
    return true
52
  },
53
  createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
54
)
55

56
// An example runtime caching route for requests that aren't handled by the
57
// precache, in this case same-origin .png requests like those from in public/
58
registerRoute(
59
  // Add in any other file extensions or routing criteria as needed.
60
  ({ url }) => url.origin === self.location.origin && url.pathname.endsWith('.png'),
61
  // Customize this strategy as needed, e.g., by changing to CacheFirst.
62
  new StaleWhileRevalidate({
63
    cacheName: 'images',
64
    plugins: [
65
      // Ensure that once this runtime cache reaches a maximum size the
66
      // least-recently used images are removed.
67
      new ExpirationPlugin({ maxEntries: 50 }),
68
    ],
69
  })
70
)
71

72
// This allows the web app to trigger skipWaiting via
73
// registration.waiting.postMessage({type: 'SKIP_WAITING'})
74
self.addEventListener('message', (event) => {
75
  if (event.data && event.data.type === 'SKIP_WAITING') {
76
    self.skipWaiting()
77
  }
78
})
79

80
// Any other custom service worker logic can go here.

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.