-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
160 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,6 @@ | ||
{ | ||
"$schema": "./node_modules/@angular/service-worker/config/schema.json", | ||
"index": "/index.html", | ||
"assetGroups": [ | ||
{ | ||
"name": "app", | ||
"installMode": "prefetch", | ||
"resources": { | ||
"files": [ | ||
"/favicon.ico", | ||
"/index.html", | ||
"/manifest.webmanifest", | ||
"/*.css", | ||
"/*.js" | ||
] | ||
} | ||
}, | ||
{ | ||
"name": "assets", | ||
"installMode": "lazy", | ||
"updateMode": "prefetch", | ||
"resources": { | ||
"files": [ | ||
"/**/*.(svg|cur|jpg|jpeg|png|apng|webp|avif|gif|otf|ttf|woff|woff2)" | ||
] | ||
} | ||
} | ||
] | ||
"$schema": "./node_modules/@angular/service-worker/config/schema.json", | ||
"index": "/index.html", | ||
"assetGroups": [], | ||
"navigationRequestStrategy": "freshness" | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Injectable, NgZone } from '@angular/core'; | ||
import { SwUpdate } from '@angular/service-worker'; | ||
import { Subscription, interval } from 'rxjs'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class NewVersionCheckerService { | ||
isNewVersionAvailable: boolean = false; | ||
newVersionSubscription?: Subscription; | ||
intervalSource = interval(15 * 60 * 1000); // every 15 mins | ||
intervalSubscription?: Subscription; | ||
|
||
constructor( | ||
private swUpdate: SwUpdate, | ||
private zone: NgZone, | ||
) { | ||
this.checkForUpdateOnInterval(); | ||
this.checkForUpdateOnLoad(); | ||
} | ||
|
||
applyUpdate(): void { | ||
// Reload the page to update to the latest version after the new version is activated | ||
this.swUpdate | ||
.activateUpdate() | ||
.then(() => document.location.reload()) | ||
.catch((error) => console.error('Failed to apply updates:', error)); | ||
} | ||
|
||
checkForUpdateOnInterval(): void { | ||
this.intervalSubscription?.unsubscribe(); | ||
if (!this.swUpdate.isEnabled) { | ||
return; | ||
} | ||
|
||
this.zone.runOutsideAngular(() => { | ||
this.intervalSubscription = this.intervalSource.subscribe(async () => { | ||
if (!this.isNewVersionAvailable) { | ||
try { | ||
this.isNewVersionAvailable = await this.swUpdate.checkForUpdate(); | ||
console.log(this.isNewVersionAvailable ? 'A new version is available.' : 'Already on the latest version.'); | ||
} catch (error) { | ||
console.error('Failed to check for updates:', error); | ||
} | ||
} else { | ||
// Check for updates at interval, which will keep the | ||
// browser updating to latest version as long as it's being kept open. | ||
await this.swUpdate.checkForUpdate(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
checkForUpdateOnLoad(): void { | ||
this.newVersionSubscription?.unsubscribe(); | ||
if (!this.swUpdate.isEnabled) { | ||
console.log('Service worker updates are disabled for this app.'); | ||
return; | ||
} | ||
this.newVersionSubscription = this.swUpdate.versionUpdates.subscribe((evt) => { | ||
console.log('New version update event:'); | ||
console.log(evt); | ||
switch (evt.type) { | ||
case 'VERSION_DETECTED': | ||
console.log(`Downloading new app version: ${evt.version.hash}`); | ||
break; | ||
case 'VERSION_READY': | ||
console.log(`Current app version: ${evt.currentVersion.hash}`); | ||
console.log(`New app version ready for use: ${evt.latestVersion.hash}`); | ||
this.isNewVersionAvailable = true; | ||
break; | ||
case 'VERSION_INSTALLATION_FAILED': | ||
console.log(`Failed to install app version '${evt.version.hash}': ${evt.error}`); | ||
break; | ||
} | ||
}); | ||
|
||
console.log('Subscribed to new version updates.'); | ||
} | ||
} |
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'; | ||
|
||
@Pipe({ | ||
name: 'safeResourceUrl', | ||
standalone: true, | ||
}) | ||
export class SafeUrlPipe implements PipeTransform { | ||
constructor(private readonly sanitizer: DomSanitizer) {} | ||
|
||
public transform(url: string): SafeResourceUrl { | ||
return this.sanitizer.bypassSecurityTrustResourceUrl(url); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { SizePipe } from './size.pipe'; | ||
|
||
describe('SizePipe', () => { | ||
it('create an instance', () => { | ||
const pipe = new SizePipe(); | ||
expect(pipe).toBeTruthy(); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
@Pipe({ | ||
name: 'size', | ||
standalone: true, | ||
}) | ||
export class SizePipe implements PipeTransform { | ||
transform(value: any, args?: any): any { | ||
if (value == null) { | ||
return ''; | ||
} | ||
|
||
if (value === 0) { | ||
return '0 Bytes'; | ||
} | ||
|
||
const k = 1024; | ||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB']; | ||
const i = Math.floor(Math.log(value) / Math.log(k)); | ||
|
||
return parseFloat((value / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
@Pipe({ | ||
name: 'stripHtml', | ||
standalone: true, | ||
}) | ||
export class StripHtmlPipe implements PipeTransform { | ||
transform(value: string, limit: number = 200): string { | ||
if (!value) { | ||
return '...'; | ||
} | ||
|
||
// Strip HTML tags and replace them with a space | ||
let strippedText = value.replace(/<\/?[^>]+(>|$)/g, ' '); | ||
|
||
// Replace with a space | ||
strippedText = strippedText.replace(/ /g, ' '); | ||
|
||
// Remove extra spaces | ||
const normalizedText = strippedText.replace(/\s\s+/g, ' ').trim(); | ||
|
||
// Limit the text to the specified number of characters | ||
let result = normalizedText; | ||
if (normalizedText.length > limit) { | ||
result = normalizedText.substring(0, limit); | ||
} | ||
|
||
// Always add "..." at the end | ||
return result.trim() + '...'; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.