Created
March 5, 2024 09:42
-
-
Save Burgov/ab8c1e0ed4fb373291be98432cad30d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const t = new Date(); | |
if (isPlatformBrowser(this.platformId)) { | |
const timeoutDelay = 60_000; | |
const events = ['keypress', 'click', 'wheel', 'mousemove', 'ontouchstart']; | |
const $onInactive = from(events.map(e => fromEvent(this.document, e))).pipe( | |
mergeAll(), | |
map(() => false), | |
timeout({ each: timeoutDelay, with: () => of(true) }), | |
repeat(), | |
shareReplay(1), | |
); | |
if (this.swUpdate.isEnabled) { | |
const newVersion$ = this.swUpdate.versionUpdates.pipe( | |
filter((event): event is VersionReadyEvent => event.type === 'VERSION_READY'), | |
take(1), | |
shareReplay(1), | |
); | |
newVersion$.subscribe(async event => { | |
type T = { version: string }; | |
const oldVersion = (event.currentVersion.appData as T)?.version; | |
const newVersion = (event.latestVersion.appData as T)?.version; | |
// eslint-disable-next-line no-console | |
console.log(`New version available: ${oldVersion} -> ${newVersion}`); | |
if (new Date().getTime() - t.getTime() < 5000) { | |
// new version detected within 5 seconds after page init. Refresh right away. | |
window.location.reload(); | |
return; | |
} | |
$onInactive.subscribe(() => { | |
window.location.reload(); | |
}); | |
this.router.events | |
.pipe( | |
filter((event): event is NavigationStart => event instanceof NavigationStart), | |
untilDestroyed(this), | |
) | |
.subscribe(event => { | |
window.location.href = event.url; | |
}); | |
}); | |
defer(() => from(this.swUpdate.checkForUpdate())) | |
.pipe( | |
catchError(() => of(false)), | |
repeatWhen(o => o.pipe(delay(timeoutDelay))), | |
takeUntil(newVersion$), | |
) | |
.subscribe(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment