Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

1.4 KB
Raw Blame
/**
 * Detect whether the browser can create a WebGL context.
 *
 * Used before mounting react-three-fiber `<Canvas>` so CoS 3D avatars (and
 * similar) degrade cleanly instead of throwing unhandled
 * `THREE.WebGLRenderer: Error creating WebGL context` rejections on headless
 * Chrome, remote desktops, or GPUs with WebGL disabled.
 *
 * Result is memoized for the page lifetime — context creation is not free,
 * and availability does not change mid-session in practice.
 */

let cached = null;

export function isWebGLAvailable() {
  if (cached !== null) return cached;
  if (typeof document === 'undefined') {
    cached = false;
    return cached;
  }
  try {
    const canvas = document.createElement('canvas');
    const gl =
      canvas.getContext('webgl2', { failIfMajorPerformanceCaveat: false }) ||
      canvas.getContext('webgl', { failIfMajorPerformanceCaveat: false }) ||
      canvas.getContext('experimental-webgl', { failIfMajorPerformanceCaveat: false });
    cached = !!gl;
    // Drop the context immediately so we don't hold a GPU slot just for the probe.
    if (gl && typeof gl.getExtension === 'function') {
      const lose = gl.getExtension('WEBGL_lose_context');
      lose?.loseContext?.();
    }
  } catch {
    cached = false;
  }
  return cached;
}

/** Test-only: clear the memo so cases can flip availability. */
export function __resetWebGLAvailableCache() {
  cached = null;
}