javascript - For ServiceWorker cache.addAll(), how do the URLs work? -


i see lot of example code this: (slightly shortened version of this mozilla doc)

this.addeventlistener('install', function(event) {   event.waituntil(     caches.open('v1').then(function(cache) {       return cache.addall([         '/sw-test/',         '/sw-test/index.html',         '/sw-test/style.css',         '/sw-test/gallery/',         '/sw-test/gallery/bountyhunters.jpg',       ]);     })   ); }); 

i don't understand why add both /sw-test/ and /sw-test/index.html. seems either first folder url should autoload underneath, or, if doesn't that, why there? same /sw-test/gallery/ , /sw-test/gallery/bountyhunters.jpg.

the docs "the addall() method of cache interface takes array of urls, retrieves them, , adds resulting response objects given cache". isn't helpful.

what want cache *.html files few folders , image files (in various formats) folder. listing them one-by-one fragile (will out-of-sync), prone typos, , plain silly.

added later

after further reading, doesn't seem wildcards exist, silly is. :-) whats point of adding folder /sw-test/?

yes, wildcard doesn't exist , that's reasonable when come think of it: web server doesn't expose files found in path (eg. /gallery/) how browser cache files doesn't know names for? web server can of course configured expose directory listings of path browsers don't have ability "take path x". directory/index listing bunch of html, not mapping of files or so.

caching both / , /index.html bit confusing service worker's standpoint different urls. web server of course configured serve same file (index.html) whichever url visit if ask sw, separate , should have separate entries in cache. can tested easily: deploy caches / not /index.html (even though web server serves index.html /) , try visit /index.html in offline mode. no luck.

i don't think there's reason @ cache /index.html if sure web app never makes requests it. if web app knows of / leaving /index.html out should fine. i've tested myself , works perfectly. if has more information on matter, please correct me!

something note useful: sw may configured treat / , /index.html same. caching / , serving whenever either 1 requested possible. guess of libraries has sort of functionality built-in.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -