Assume that a service-worker.js
is ready to offer offline browsing ability thanks to the following code,
self.addEventListener("fetch", event => {
event.respondWith( caches.match(event.request).then( cachedResponse => { return cachedResponse || fetch(event.request); } ) );
});
And let's say we have cached some assets where the files are listed relative to the root directory,
const cacheAssets = await caches.open('necessary-files-v1');
const listOfFiles = [
"/somefolder/somepage.html",
"/somefolder/somevisual.jpg",
"/somefolder/someaudio.mp3"
];
await cacheAssets.addAll(listOfFiles);
Now compare the offline case for the following two (obviously they both work fine when online),
Scenario 1: somepage.html
contains <img src="somevisual.jpg">
Scenario 2: somepage.html
contains <img src="/somefolder/somevisual.jpg">
Given the fact that the string to string comparison ("somevisual.jpg" == "/somefolder/somevisual.jpg")
will return false
should we always and always adopt Scenario 2 when building our html files with the hope of offline compatibility? Or is there a case or some method that offers us developers some nice-to-have flexibility?