Created
February 28, 2023 21:18
-
-
Save nwellis/d36613af0c2049d6e804cb766f69e52f to your computer and use it in GitHub Desktop.
Workbox warm caching spine files and their associated image files
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
/** | |
* A spine export produces 3 file types- one JSON file and one atlas.txt that maps | |
* any number of PNGs. We know the JSON and atlas paths ahead of time, but not the PNGs. | |
* This method will return a promise that completes when the files are loaded. If the | |
* path is an atlas.txt file, it will wait for the response and read the file contents | |
* in order to start loading the associated PNGs as well. | |
*/ | |
async function loadSpineFiles(event: ExtendableEvent, strategy: Strategy, path: string) { | |
const pending = Array.of<Promise<void>>() | |
const [asyncResponse, finished] = strategy.handleAll({ event, request: new Request(path) }) | |
pending.push(finished) | |
if (path.toLowerCase().endsWith("atlas.txt")) { | |
const pathFolder = path.split("/").slice(0, -1).join("/") | |
const response = await asyncResponse | |
const content = await response.text() | |
const associatedImagePaths = content.split("\n").filter(line => line.toLowerCase().endsWith(".webp")) | |
.map(imageFilename => `${pathFolder}/${imageFilename}`) | |
pending.push(...associatedImagePaths.map(imagePath => strategy.handleAll({ event, request: new Request(imagePath) })[1])) | |
} | |
return Promise.all(pending) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment