Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/grid-ui/src/serviceWorker.ts
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
// This optional code is used to register a service worker.
19
// register() is not called by default.
20
21
// This lets the app load faster on subsequent visits in production, and gives
22
// it offline capabilities. However, it also means that developers (and users)
23
// will only see deployed updates on subsequent visits to a page, after all the
24
// existing tabs open on the page have been closed, since previously cached
25
// resources are updated in the background.
26
27
// To learn more about the benefits of this model and instructions on how to
28
// opt-in, read https://bit.ly/CRA-PWA
29
30
/* eslint-disable */
31
32
const isLocalhost = Boolean(
33
window.location.hostname === 'localhost' ||
34
// [::1] is the IPv6 localhost address.
35
window.location.hostname === '[::1]' ||
36
// 127.0.0.0/8 are considered localhost for IPv4.
37
window.location.hostname.match(
38
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
39
)
40
)
41
42
export function register (config) {
43
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
44
// The URL constructor is available in all browsers that support SW.
45
// @ts-ignore
46
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href)
47
if (publicUrl.origin !== window.location.origin) {
48
// Our service worker won't work if PUBLIC_URL is on a different origin
49
// from what our page is served on. This might happen if a CDN is used to
50
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
51
return
52
}
53
54
window.addEventListener('load', () => {
55
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`
56
57
if (isLocalhost) {
58
// This is running on localhost. Let's check if a service worker still exists or not.
59
checkValidServiceWorker(swUrl, config)
60
61
// Add some additional logging to localhost, pointing developers to the
62
// service worker/PWA documentation.
63
navigator.serviceWorker.ready.then(() => {
64
console.log(
65
'This web app is being served cache-first by a service ' +
66
'worker. To learn more, visit https://bit.ly/CRA-PWA'
67
)
68
})
69
} else {
70
// Is not localhost. Just register service worker
71
registerValidSW(swUrl, config)
72
}
73
})
74
}
75
}
76
77
function registerValidSW (swUrl, config) {
78
navigator.serviceWorker
79
.register(swUrl)
80
.then(registration => {
81
registration.onupdatefound = () => {
82
const installingWorker = registration.installing
83
if (installingWorker == null) {
84
return
85
}
86
installingWorker.onstatechange = () => {
87
if (installingWorker.state === 'installed') {
88
if (navigator.serviceWorker.controller != null) {
89
// At this point, the updated precached content has been fetched,
90
// but the previous service worker will still serve the older
91
// content until all client tabs are closed.
92
console.log(
93
'New content is available and will be used when all ' +
94
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
95
)
96
97
// Execute callback
98
if (config && config.onUpdate) {
99
config.onUpdate(registration)
100
}
101
} else {
102
// At this point, everything has been precached.
103
// It's the perfect time to display a
104
// "Content is cached for offline use." message.
105
console.log('Content is cached for offline use.')
106
107
// Execute callback
108
if (config && config.onSuccess) {
109
config.onSuccess(registration)
110
}
111
}
112
}
113
}
114
}
115
})
116
.catch(error => {
117
console.error('Error during service worker registration:', error)
118
})
119
}
120
121
function checkValidServiceWorker (swUrl, config) {
122
// Check if the service worker can be found. If it can't reload the page.
123
window.fetch(swUrl, {
124
headers: { 'Service-Worker': 'script' }
125
})
126
.then(response => {
127
// Ensure service worker exists, and that we really are getting a JS file.
128
const contentType = response.headers.get('content-type')
129
if (
130
response.status === 404 ||
131
(contentType != null && !contentType.includes('javascript'))
132
) {
133
// No service worker found. Probably a different app. Reload the page.
134
navigator.serviceWorker.ready.then(registration => {
135
registration.unregister().then(() => {
136
window.location.reload()
137
})
138
})
139
} else {
140
// Service worker found. Proceed as normal.
141
registerValidSW(swUrl, config)
142
}
143
})
144
.catch(() => {
145
console.log(
146
'No internet connection found. App is running in offline mode.'
147
)
148
})
149
}
150
151
export function unregister () {
152
if ('serviceWorker' in navigator) {
153
navigator.serviceWorker.ready
154
.then(registration => {
155
registration.unregister()
156
})
157
.catch(error => {
158
console.error(error.message)
159
})
160
}
161
}
162
163