Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: force update service worker #72

Merged
merged 1 commit into from
Dec 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion apps/js-machine-admin/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.register();
serviceWorker.register({
onUpdate: registration => {
if (registration.waiting) {
// update service worker
// user have to reload page to use new version
registration.waiting.postMessage({ type: 'SKIP_WAITING' });
}
},
});
16 changes: 9 additions & 7 deletions apps/js-machine-admin/src/serviceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const isLocalhost = Boolean(
window.location.hostname === '[::1]' ||
// 127.0.0.1/8 is considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/,
),
);

type Config = {
Expand Down Expand Up @@ -50,7 +50,7 @@ export function register(config?: Config) {
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'
'worker. To learn more, visit https://bit.ly/CRA-PWA',
);
});
} else {
Expand All @@ -63,7 +63,7 @@ export function register(config?: Config) {

function registerValidSW(swUrl: string, config?: Config) {
navigator.serviceWorker
.register(swUrl)
.register(swUrl, { updateViaCache: 'none' })
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
Expand All @@ -78,7 +78,7 @@ function registerValidSW(swUrl: string, config?: Config) {
// 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.'
'tabs for this page are closed. See https://bit.ly/CRA-PWA.',
);

// Execute callback
Expand Down Expand Up @@ -107,7 +107,9 @@ function registerValidSW(swUrl: string, config?: Config) {

function checkValidServiceWorker(swUrl: string, config?: Config) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl)
fetch(swUrl, {
headers: { 'Service-Worker': 'script' },
})
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type');
Expand All @@ -128,7 +130,7 @@ function checkValidServiceWorker(swUrl: string, config?: Config) {
})
.catch(() => {
console.log(
'No internet connection found. App is running in offline mode.'
'No internet connection found. App is running in offline mode.',
);
});
}
Expand Down
10 changes: 9 additions & 1 deletion apps/js-machine-front/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.register();
serviceWorker.register({
onUpdate: registration => {
if (registration.waiting) {
// update service worker
// user have to reload page to use new version
registration.waiting.postMessage({ type: 'SKIP_WAITING' });
}
},
});
16 changes: 9 additions & 7 deletions apps/js-machine-front/src/serviceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const isLocalhost = Boolean(
window.location.hostname === '[::1]' ||
// 127.0.0.1/8 is considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/,
),
);

type Config = {
Expand Down Expand Up @@ -50,7 +50,7 @@ export function register(config?: Config) {
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'
'worker. To learn more, visit https://bit.ly/CRA-PWA',
);
});
} else {
Expand All @@ -63,7 +63,7 @@ export function register(config?: Config) {

function registerValidSW(swUrl: string, config?: Config) {
navigator.serviceWorker
.register(swUrl)
.register(swUrl, { updateViaCache: 'none' })
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
Expand All @@ -78,7 +78,7 @@ function registerValidSW(swUrl: string, config?: Config) {
// 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.'
'tabs for this page are closed. See https://bit.ly/CRA-PWA.',
);

// Execute callback
Expand Down Expand Up @@ -107,7 +107,9 @@ function registerValidSW(swUrl: string, config?: Config) {

function checkValidServiceWorker(swUrl: string, config?: Config) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl)
fetch(swUrl, {
headers: { 'Service-Worker': 'script' },
})
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type');
Expand All @@ -128,7 +130,7 @@ function checkValidServiceWorker(swUrl: string, config?: Config) {
})
.catch(() => {
console.log(
'No internet connection found. App is running in offline mode.'
'No internet connection found. App is running in offline mode.',
);
});
}
Expand Down