Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,47 @@ function testIndexedDB(output) {
}
}

/**
* @param {HTMLElement} output
*/
async function testCacheStorage(output) {
output.appendChild(h("h3", {}, "CacheStorage"));

const appendErr = (err) => {
output.append(h("strong", {class: "red"}, "WARNING: "), "CacheStorage is not supported." + err);
}
if (!globalThis.caches) {
appendErr(`globalThis.caches is ${globalThis.caches}`);
return;
}

try {
const cache = await caches.open("test-cache");
const key = 'test-url';
const res1 = await cache.match(key);
const initialValue = parseInt(
res1 != undefined ? await res1.text() : "0"
);
await cache.put(key, new Response(initialValue + 1));

const res2 = await cache.match(key);
if (res2 == undefined) {
appendErr('Failed to fetch the value that we just stored');
}
const retreived2 = await res2.text()

output.append("Counter: ", h("span", {}, retreived2));
} catch (err) {
appendErr(err);
return;
}
}

window.addEventListener("load", () => {
let div = h("div", {class: "container"});
testStorage(div, "localStorage");
testStorage(div, "sessionStorage");
testIndexedDB(div);
testCacheStorage(div);
document.getElementById("storage-output").append(createHeader("Storage"), div);
});