Skip to content

Commit

Permalink
lint serviceworker
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Jablonski authored Apr 1, 2022
1 parent 14d9326 commit bc54aad
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/serviceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// opt-in, read https://bit.ly/CRA-PWA

const isLocalhost = Boolean(
window.location.hostname === 'localhost'
|| window.location.hostname === '[::1]'
|| window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/,
),
window.location.hostname === 'localhost' ||
window.location.hostname === '[::1]' ||
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/,
),
);

type Config = {
Expand All @@ -27,7 +27,7 @@ type Config = {
function registerValidSW(swUrl: string, config?: Config) {
navigator.serviceWorker
.register(swUrl)
.then((registration) => {
.then(registration => {
// eslint-disable-next-line no-param-reassign
registration.onupdatefound = () => {
const installingWorker = registration.installing;
Expand All @@ -41,8 +41,8 @@ function registerValidSW(swUrl: string, config?: Config) {
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all '
+ 'tabs for this page are closed. See https://bit.ly/CRA-PWA.',
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://bit.ly/CRA-PWA.',
);

// Execute callback
Expand All @@ -64,7 +64,7 @@ function registerValidSW(swUrl: string, config?: Config) {
};
};
})
.catch((error) => {
.catch(error => {
console.error('Error during service worker registration:', error);
});
}
Expand All @@ -74,15 +74,15 @@ function checkValidServiceWorker(swUrl: string, config?: Config) {
fetch(swUrl, {
headers: { 'Service-Worker': 'script' },
})
.then((response) => {
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type');
if (
response.status === 404
|| (contentType != null && contentType.indexOf('javascript') === -1)
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then((registration) => {
navigator.serviceWorker.ready.then(registration => {
registration.unregister().then(() => {
window.location.reload();
});
Expand All @@ -102,7 +102,10 @@ function checkValidServiceWorker(swUrl: string, config?: Config) {
export function register(config?: Config) {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL ?? '', window.location.href);
const publicUrl = new URL(
process.env.PUBLIC_URL ?? '',
window.location.href,
);
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
Expand All @@ -121,8 +124,8 @@ export function register(config?: Config) {
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service '
+ 'worker. To learn more, visit https://bit.ly/CRA-PWA',
'This web app is being served cache-first by a service ' +
'worker. To learn more, visit https://bit.ly/CRA-PWA',
);
});
} else {
Expand All @@ -136,10 +139,10 @@ export function register(config?: Config) {
export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready
.then((registration) => {
.then(registration => {
registration.unregister();
})
.catch((error) => {
.catch(error => {
console.error(error.message);
});
}
Expand Down

0 comments on commit bc54aad

Please sign in to comment.