Created
April 25, 2023 12:18
-
-
Save nwellis/614125b7ac8581d2b4cbbe6929e8d970 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
import { CleanupResult, InstallResult, PrecacheController } from "workbox-precaching" | |
export class CustomPrecacheController extends PrecacheController { | |
private _debugPrint = false | |
private _isPrecaching = false | |
get isPrecaching() { return this._isPrecaching } | |
private _installResult: InstallResult = undefined | |
get installResult() { return this._installResult } | |
private _cleanupResult: CleanupResult = undefined | |
get cleanupResult() { return this._cleanupResult } | |
constructor( | |
readonly appCacheKey: string, | |
...options: ConstructorParameters<typeof PrecacheController> | |
) { | |
super(...options) | |
} | |
enableDebugPrint(enable = true) { | |
this._debugPrint = enable | |
return this | |
} | |
override async install(event: ExtendableEvent) { | |
this._isPrecaching = true | |
this._installResult = await super.install(event) | |
this._isPrecaching = false | |
if (this._debugPrint) { | |
this.printInstallResult() | |
} | |
return this._installResult | |
} | |
override async activate(event: ExtendableEvent) { | |
this._cleanupResult = await super.activate(event) | |
if (this._debugPrint) { | |
this.printCleanupResult() | |
} | |
return this._cleanupResult | |
} | |
printInstallResult() { | |
const header = `===== SW ${this.appCacheKey} (Install) =====` | |
const footer = "".padEnd(header.length, "=") | |
const lines = [ | |
header, | |
...( | |
this._installResult?.updatedURLs?.length > 0 | |
? this._installResult.updatedURLs.map(req => `>> ${req}`) | |
: [`>> NO INSTALL RESULT <<`] | |
), | |
footer, | |
] | |
console.debug(lines.join("\n"), this) | |
} | |
printCleanupResult() { | |
const header = `===== SW ${this.appCacheKey} (Cleanup) =====` | |
const footer = "".padEnd(header.length, "=") | |
const lines = [ | |
header, | |
...( | |
this._cleanupResult?.deletedCacheRequests?.length > 0 | |
? this._cleanupResult.deletedCacheRequests.map(req => `>> ${req}`) | |
: [`>> NO CLEANUP RESULT <<`] | |
), | |
footer, | |
] | |
console.debug(lines.join("\n"), this) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment