Paulescu Claude Sonnet 4.6 commited on
Commit
d85d217
·
1 Parent(s): 934bcfd

Fix infinite reload loop in coi-serviceworker on HuggingFace

Browse files

In HF Space iframe embeds, the parent page (huggingface.co) does not send
COOP headers, so window.crossOriginIsolated never becomes true even after
the SW is active and injecting COOP/COEP. The previous code reloaded on
every page load, causing Chrome to throttle the loop and leave the page
in a broken state with JS not executing (empty model dropdown).

Fix: guard the reload with a sessionStorage key so it only fires once per
session. If crossOriginIsolated still can't be achieved after one reload,
the page loads normally without isolation rather than looping.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. public/coi-serviceworker.js +7 -2
public/coi-serviceworker.js CHANGED
@@ -54,9 +54,14 @@ if (typeof window === "undefined") {
54
  navigator.serviceWorker
55
  .register(document.currentScript.src)
56
  .then((reg) => {
57
- // SW already active but page loaded without isolation: reload to activate headers
 
 
58
  if (reg.active && !window.crossOriginIsolated) {
59
- window.location.reload();
 
 
 
60
  }
61
  })
62
  .catch((err) =>
 
54
  navigator.serviceWorker
55
  .register(document.currentScript.src)
56
  .then((reg) => {
57
+ // SW already active but page loaded without isolation: reload once to activate headers.
58
+ // Guard with sessionStorage to avoid an infinite loop in contexts where
59
+ // crossOriginIsolated can never become true (e.g. HuggingFace iframe embeds).
60
  if (reg.active && !window.crossOriginIsolated) {
61
+ if (!sessionStorage.getItem("coi-reloaded")) {
62
+ sessionStorage.setItem("coi-reloaded", "1");
63
+ window.location.reload();
64
+ }
65
  }
66
  })
67
  .catch((err) =>