File size: 3,059 Bytes
3458c10
 
 
 
 
 
 
 
 
 
 
53416ba
 
 
 
 
 
 
3458c10
 
 
 
 
53416ba
 
3458c10
53416ba
 
 
 
 
3458c10
53416ba
 
 
 
 
 
 
 
 
 
3458c10
 
 
 
53416ba
3458c10
 
 
53416ba
 
 
 
 
 
 
 
 
 
 
3458c10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// AMC — published data files kept in the browser between visits.
//
// Hugging Face serves each file through an address signed anew on every
// visit, with no cache instructions, so the browser downloaded the same
// megabytes every time: a second visit to a company's page fetched 12.8 MB
// again, the explorer 5.5 MB (measured live, 2026-10-26). The manifest
// publishes each file's sha256, so a copy kept under that checksum is the
// published file until an edition changes it, and the next visit reads it
// from the disk. A new edition's checksum misses, downloads once, and the
// file's older copies are deleted.
//
// A FIRST VISIT DOES NOT WAIT FOR THE MANIFEST. Waiting for it to know the
// checksum before downloading started every download a second or two later,
// and a cold name search came about 2.5 s later on the live site (A/B,
// 2026-10-27). So when the browser holds no copy of a file, the download
// starts at once and the manifest only decides, afterwards, under which
// checksum the copy is kept.
//
// Only for files a page downloads whole. Where the browser keeps nothing (a
// private window, storage refused), the download goes ahead as before.
const STORE = "amc-data-v1";

const open = () => (typeof caches === "undefined" ? Promise.resolve(null) : caches.open(STORE).catch(() => null));
const prefixOf = (name) => `${location.origin}/__amc-data/${encodeURIComponent(name)}?`;
const keyOf = (name, sha) => `${prefixOf(name)}sha256=${sha}`;

// The file's bytes, from the browser's copy when its checksum is the
// manifest's. `meta` is { sha256, bytes } from the manifest, or a promise of it.
export async function fetchFile(url, { name, meta } = {}) {
  const store = name ? await open() : null;
  let held = null;
  if (store) {
    try { held = (await store.keys()).find((k) => k.url.startsWith(prefixOf(name))) || null; } catch {}
  }
  if (held) {
    const m = await Promise.resolve(meta).catch(() => null);
    if (m?.sha256 && held.url === keyOf(name, m.sha256)) {
      try {
        const buf = await (await store.match(held)).arrayBuffer();
        if (!m.bytes || buf.byteLength === m.bytes) return buf;
      } catch {}
    }
  }
  const r = await fetch(url);
  if (!r.ok) throw new Error(`${name || url.split("/").pop()}: HTTP ${r.status}`);
  const buf = await r.arrayBuffer();
  if (store) keep(store, name, buf, meta);
  return buf;
}

// Stored once the manifest says under which checksum, if the size is its
// size, and the edition before is cleared. The page already has its bytes.
function keep(store, name, buf, meta) {
  const copy = buf.slice(0);
  Promise.resolve(meta).then((m) => {
    if (!m?.sha256 || (m.bytes && copy.byteLength !== m.bytes)) return;
    const key = keyOf(name, m.sha256);
    return store.put(key, new Response(copy, { headers: { "Content-Type": "application/octet-stream" } }))
      .then(() => store.keys())
      .then((keys) => Promise.all(keys.filter((k) => k.url.startsWith(prefixOf(name)) && k.url !== key).map((k) => store.delete(k))));
  }).catch(() => {});
}