|
| 1 | +'use client'; |
| 2 | + |
| 3 | +// Report rendering-library warm-up for directory pages (landing, user |
| 4 | +// listing). Renders nothing; once the page has fully loaded AND the main |
| 5 | +// thread is idle, it warms the shared rendering libraries the first report |
| 6 | +// a visitor opens needs, so the report renders with them already in local |
| 7 | +// caches: |
| 8 | +// |
| 9 | +// - vizdom wasm (~4.8MB): the shared bridge module is injected exactly the |
| 10 | +// way the report page injects it, so the binary is fetched once and stored |
| 11 | +// in the bridge's versioned Cache Storage entry — the report page's own |
| 12 | +// bridge import then resolves it locally with zero network. |
| 13 | +// - Chart.js + svg-pan-zoom: fetched no-cors into the HTTP cache; the |
| 14 | +// report page's classic <script src> requests then hit the cache (the CDN |
| 15 | +// serves both immutable), matching the later request mode exactly. |
| 16 | +// - The bundled report template: a same-origin fetch into the HTTP cache. |
| 17 | +// |
| 18 | +// Report DATA is deliberately never warmed: listings can hold thousands of |
| 19 | +// repositories and the one a visitor opens is unknowable in advance — only |
| 20 | +// the shared rendering pipeline is warmed. Every step is fire-and-forget |
| 21 | +// and fails silently (a cold cache is just the status quo), and nothing |
| 22 | +// ever delays the hosting page's own rendering (load + idle scheduling). |
| 23 | +// Popup-only widgets (sigma, graphology, graphlib-dot, the 3D force graph) |
| 24 | +// also stay cold by design: they load lazily on the report page and are |
| 25 | +// needed only on demand. |
| 26 | + |
| 27 | +import { useEffect, useRef } from 'react'; |
| 28 | +import { withBasePath } from '../lib/base-path'; |
| 29 | +// The idle warm-up loads the vizdom bridge module on directory pages; this |
| 30 | +// module installs the widget-readiness bridge the bridge reports through, so |
| 31 | +// a mark fired while warming reaches the registry a later client-side-navigated |
| 32 | +// report reads from (the module map keeps the already-evaluated bridge module |
| 33 | +// from re-running on the report page). |
| 34 | +import '../lib/widget-loader'; |
| 35 | + |
| 36 | +const WIDGET_SCRIPTS = [ |
| 37 | + 'https://cdn.jsdelivr.net/npm/chart.js@4.4.7/dist/chart.umd.min.js', |
| 38 | + 'https://cdn.jsdelivr.net/npm/svg-pan-zoom@3.6.1/dist/svg-pan-zoom.min.js' |
| 39 | +]; |
| 40 | + |
| 41 | +/** Fire-and-forget fetch: warming is best-effort, failures are fine. */ |
| 42 | +function warmFetch(url, options) { |
| 43 | + fetch(url, options).catch(() => {}); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Warms the report rendering libraries until a directory page visitor |
| 48 | + * opens a report. |
| 49 | + * |
| 50 | + * @param {object} props - Warm-up configuration. |
| 51 | + * @param {number} [props.fallbackDelayMs=1500] - Test seam: delay before the |
| 52 | + * warm when requestIdleCallback is unavailable. |
| 53 | + * @returns {null} Nothing is rendered. |
| 54 | + */ |
| 55 | +export default function ReportWarmup({ fallbackDelayMs = 1500 }) { |
| 56 | + const warmedRef = useRef(false); |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + // The warm runs once per mount (warmedRef); re-renders are no-ops. |
| 60 | + if (warmedRef.current) return undefined; |
| 61 | + let cancelled = false; |
| 62 | + let cancelIdle = () => {}; |
| 63 | + let bridgeScript = null; |
| 64 | + |
| 65 | + /** Runs each warm step exactly once, best-effort. */ |
| 66 | + const warm = () => { |
| 67 | + if (cancelled || warmedRef.current) return; |
| 68 | + warmedRef.current = true; |
| 69 | + |
| 70 | + // 1. vizdom wasm through the shared bridge module: same file, same |
| 71 | + // versioned Cache Storage entry as the report page uses. |
| 72 | + const bridgeSrc = withBasePath('/widgets/vizdom-bridge.js'); |
| 73 | + const alreadyInjected = [...document.querySelectorAll('script[type="module"]')] |
| 74 | + .some(script => (script.getAttribute('src') || script.src || '') |
| 75 | + .endsWith('/widgets/vizdom-bridge.js')); |
| 76 | + if (!alreadyInjected) { |
| 77 | + const script = document.createElement('script'); |
| 78 | + script.type = 'module'; |
| 79 | + script.src = bridgeSrc; |
| 80 | + document.body.appendChild(script); |
| 81 | + bridgeScript = script; |
| 82 | + } |
| 83 | + |
| 84 | + // 2. Classic widget scripts into the HTTP cache (no-cors matches the |
| 85 | + // report page's classic <script src> requests). |
| 86 | + WIDGET_SCRIPTS.forEach(src => warmFetch(src, { mode: 'no-cors' })); |
| 87 | + |
| 88 | + // 3. The bundled report template. |
| 89 | + warmFetch(withBasePath('/assets/refactor-first-report.mustache')); |
| 90 | + }; |
| 91 | + |
| 92 | + /** |
| 93 | + * Schedules the warm at the first idle moment after the page loads. The |
| 94 | + * idle callback is registered without a forced timeout, so the browser |
| 95 | + * fires it strictly at an idle opportunity rather than on a timer while |
| 96 | + * the page may still be busy; engines without requestIdleCallback wait |
| 97 | + * out an idle window (the fallback delay) before the warm starts. |
| 98 | + */ |
| 99 | + const scheduleAtIdle = () => { |
| 100 | + if (cancelled) return; |
| 101 | + if (typeof window.requestIdleCallback === 'function') { |
| 102 | + const handle = window.requestIdleCallback(warm); |
| 103 | + cancelIdle = () => window.cancelIdleCallback?.(handle); |
| 104 | + } else { |
| 105 | + const timer = setTimeout(warm, fallbackDelayMs); |
| 106 | + cancelIdle = () => clearTimeout(timer); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + if (document.readyState === 'complete') { |
| 111 | + scheduleAtIdle(); |
| 112 | + } else { |
| 113 | + window.addEventListener('load', scheduleAtIdle, { once: true }); |
| 114 | + } |
| 115 | + return () => { |
| 116 | + cancelled = true; |
| 117 | + window.removeEventListener('load', scheduleAtIdle); |
| 118 | + cancelIdle(); |
| 119 | + // The bridge script is appended straight to <body> (outside React's |
| 120 | + // knowledge); drop it again on unmount so a navigating-away page leaves |
| 121 | + // no script behind. |
| 122 | + if (bridgeScript) bridgeScript.remove(); |
| 123 | + }; |
| 124 | + }, [fallbackDelayMs]); |
| 125 | + |
| 126 | + return null; |
| 127 | +} |
0 commit comments