vomebook commited on
Commit
bd0a37d
·
1 Parent(s): d07332f

fix(reader): cache shell scripts when SW control begins after warming

Browse files
static/app.js CHANGED
@@ -2651,25 +2651,35 @@ function warmReaderIntent(rawUrl) {
2651
  } catch (_) { return; }
2652
  const shellAssets = VoiceOfMLReaderResources.shellAssets("/static/");
2653
  const engineAssets = VoiceOfMLReaderResources.engineAssets(extension, "/static/");
 
 
 
 
 
 
 
 
 
 
 
2654
  for (const href of shellAssets.concat(engineAssets)) {
2655
  if (warmedReaderAssets.has(href)) continue;
2656
  warmedReaderAssets.add(href);
2657
- if (href.endsWith("/reader.html")) {
 
 
 
 
 
2658
  fetch(href, { priority: "low", signal: AbortSignal.timeout(8000) })
2659
  .then(response => {
2660
  if (!response.ok) throw new Error("Reader shell HTTP " + response.status);
2661
  return response.arrayBuffer();
2662
- }).catch(() => warmedReaderAssets.delete(href));
 
2663
  continue;
2664
  }
2665
- const link = document.createElement("link");
2666
- const module = href.endsWith(".mjs") || href.includes("/foliate-reader/view.js") || (href.endsWith(".js") && !engineAssets.includes(href));
2667
- link.rel = module ? "modulepreload" : "preload";
2668
- if (!module) link.as = href.endsWith(".css") ? "style" : "script";
2669
- link.fetchPriority = "low";
2670
- link.href = href;
2671
- link.addEventListener("error", () => { warmedReaderAssets.delete(href); link.remove(); }, { once: true });
2672
- document.head.appendChild(link);
2673
  }
2674
  startReaderSourceWarmup("", readerId, sourceUrl);
2675
  warmConnection();
 
2651
  } catch (_) { return; }
2652
  const shellAssets = VoiceOfMLReaderResources.shellAssets("/static/");
2653
  const engineAssets = VoiceOfMLReaderResources.engineAssets(extension, "/static/");
2654
+ const controlled = !!navigator.serviceWorker?.controller;
2655
+ const preload = (href) => {
2656
+ const link = document.createElement("link");
2657
+ const module = href.endsWith(".mjs") || href.includes("/foliate-reader/view.js") || (href.endsWith(".js") && !engineAssets.includes(href));
2658
+ link.rel = module ? "modulepreload" : "preload";
2659
+ if (!module) link.as = href.endsWith(".css") ? "style" : "script";
2660
+ link.fetchPriority = "low";
2661
+ link.href = href;
2662
+ link.addEventListener("error", () => { warmedReaderAssets.delete(href); link.remove(); }, { once: true });
2663
+ document.head.appendChild(link);
2664
+ };
2665
  for (const href of shellAssets.concat(engineAssets)) {
2666
  if (warmedReaderAssets.has(href)) continue;
2667
  warmedReaderAssets.add(href);
2668
+ const shellFile = href.endsWith("/reader.html");
2669
+ // Under SW control, store shell files in its cache first. A repeated link
2670
+ // preload is deduplicated after a pre-control warm, leaving classic shell
2671
+ // scripts uncached and on the Reader's critical path. The link then reads
2672
+ // the cached copy without a second download.
2673
+ if (shellFile || (controlled && !engineAssets.includes(href))) {
2674
  fetch(href, { priority: "low", signal: AbortSignal.timeout(8000) })
2675
  .then(response => {
2676
  if (!response.ok) throw new Error("Reader shell HTTP " + response.status);
2677
  return response.arrayBuffer();
2678
+ }).then(() => { if (!shellFile) preload(href); })
2679
+ .catch(() => warmedReaderAssets.delete(href));
2680
  continue;
2681
  }
2682
+ preload(href);
 
 
 
 
 
 
 
2683
  }
2684
  startReaderSourceWarmup("", readerId, sourceUrl);
2685
  warmConnection();
tests/test_reader_navigation_host.py CHANGED
@@ -152,7 +152,8 @@ process.stdout.write(stripBundledScripts(readFileSync('static/index.html','utf8'
152
 
153
  def test_shell_is_warmed_without_pointer_intent_or_reader_execution(self):
154
  self.page.wait_for_function('warmedReaderAssets.has("/static/reader.html")')
155
- self.assertTrue(self.page.locator('link[rel="modulepreload"][href="/static/reader.js"]').count())
 
156
  self.assertEqual(self.page.locator('.reader-overlay').count(), 0)
157
  self.assertFalse(self.page.evaluate('!!window.VoiceOfMLReaderPdfDiagnostics'))
158
 
 
152
 
153
  def test_shell_is_warmed_without_pointer_intent_or_reader_execution(self):
154
  self.page.wait_for_function('warmedReaderAssets.has("/static/reader.html")')
155
+ # Under SW control the link follows the cached shell fetch.
156
+ self.page.locator('link[rel="modulepreload"][href="/static/reader.js"]').wait_for(state='attached')
157
  self.assertEqual(self.page.locator('.reader-overlay').count(), 0)
158
  self.assertFalse(self.page.evaluate('!!window.VoiceOfMLReaderPdfDiagnostics'))
159