-
-
Save mostafazs/d05ed7ccd74d6362df6eb2806177c177 to your computer and use it in GitHub Desktop.
Snippet to cache all angular.io content for offline access.
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
/* | |
* Run this snippet on https://angular.io or https://next.angular.io to let the ServiceWorker | |
* cache all content (guides, API docs, etc.) and make it available for offline access. | |
* | |
* DISCLAIMER: | |
* It's a hack. Not officially supported. Use at your own risk. | |
* Works for me, but may not work for you (or it may stop working in the future). | |
*/ | |
(async () => {'use strict'; | |
try { | |
const manifest = await fetch('ngsw.json').then(res => res.json()); | |
const lazyUrls = manifest.assetGroups.find(g => g.name === 'docs-lazy').urls; | |
const confirmed = confirm(`Download all ${lazyUrls.length} URLs?`); | |
if (confirmed) { | |
const start = performance.now(); | |
const responses = await Promise.all(lazyUrls.map(u => fetch(u))); | |
const failedCount = responses.filter(r => !r.ok).length; | |
const duration = Math.round((performance.now() - start) / 1000); | |
alert(`Downloaded ${lazyUrls.length} URLs in ${duration}s. (${failedCount} failed.)`); | |
} | |
} catch (err) { | |
console.error(err); | |
alert(`An error occurred:\n\n${err.stack}`); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment