|
| 1 | +import { LDObserve } from '@launchdarkly/observability' |
| 2 | +import { useEffect, useState } from 'react' |
| 3 | + |
| 4 | +// Manual-test scaffolding for the "flush OTEL spans on page unload" fix. |
| 5 | +// Open DevTools → Network and filter for `/v1/traces`. With the fix in place, |
| 6 | +// every button below should produce a POST that shows up as `Type: fetch` |
| 7 | +// with the request staying in flight (keepalive) through the navigation / |
| 8 | +// visibility change. Before the fix, the batched spans sat in memory for up |
| 9 | +// to 30s and the XHR transport was cancelled by the browser on unload, so the |
| 10 | +// request never landed server-side. |
| 11 | +const UNLOAD_LOG_KEY = 'flush-on-unload.events' |
| 12 | + |
| 13 | +type UnloadEvent = { ts: number; name: string; detail?: string } |
| 14 | + |
| 15 | +function readUnloadLog(): UnloadEvent[] { |
| 16 | + try { |
| 17 | + return JSON.parse(sessionStorage.getItem(UNLOAD_LOG_KEY) ?? '[]') |
| 18 | + } catch { |
| 19 | + return [] |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function recordUnloadEvent(name: string, detail?: string) { |
| 24 | + const log = readUnloadLog() |
| 25 | + log.push({ ts: Date.now(), name, detail }) |
| 26 | + try { |
| 27 | + sessionStorage.setItem(UNLOAD_LOG_KEY, JSON.stringify(log)) |
| 28 | + } catch { |
| 29 | + // ignore |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +export default function FlushOnUnload() { |
| 34 | + const [lastSpan, setLastSpan] = useState<string>('') |
| 35 | + const [unloadLog, setUnloadLog] = useState<UnloadEvent[]>(() => |
| 36 | + readUnloadLog(), |
| 37 | + ) |
| 38 | + |
| 39 | + // Persist unload events to sessionStorage so we can observe them after |
| 40 | + // navigating back from example.com — console.log from pagehide/unload is |
| 41 | + // unreliable across a cross-document nav. |
| 42 | + useEffect(() => { |
| 43 | + const onPageHide = (e: PageTransitionEvent) => |
| 44 | + recordUnloadEvent('pagehide', `persisted=${e.persisted}`) |
| 45 | + const onVisibility = () => |
| 46 | + recordUnloadEvent('visibilitychange', document.visibilityState) |
| 47 | + const onBeforeUnload = () => recordUnloadEvent('beforeunload') |
| 48 | + const onUnload = () => recordUnloadEvent('unload') |
| 49 | + const onFreeze = () => recordUnloadEvent('freeze') |
| 50 | + window.addEventListener('pagehide', onPageHide) |
| 51 | + document.addEventListener('visibilitychange', onVisibility) |
| 52 | + window.addEventListener('beforeunload', onBeforeUnload) |
| 53 | + window.addEventListener('unload', onUnload) |
| 54 | + document.addEventListener('freeze', onFreeze) |
| 55 | + return () => { |
| 56 | + window.removeEventListener('pagehide', onPageHide) |
| 57 | + document.removeEventListener('visibilitychange', onVisibility) |
| 58 | + window.removeEventListener('beforeunload', onBeforeUnload) |
| 59 | + window.removeEventListener('unload', onUnload) |
| 60 | + document.removeEventListener('freeze', onFreeze) |
| 61 | + } |
| 62 | + }, []) |
| 63 | + |
| 64 | + const emitSpan = (name: string) => { |
| 65 | + const suffix = Math.random().toString(36).slice(2, 8) |
| 66 | + const spanName = `${name}-${suffix}` |
| 67 | + LDObserve.startSpan(spanName, (span) => { |
| 68 | + span?.setAttribute('flush-on-unload.test', true) |
| 69 | + span?.setAttribute('flush-on-unload.scenario', name) |
| 70 | + }) |
| 71 | + setLastSpan(spanName) |
| 72 | + return spanName |
| 73 | + } |
| 74 | + |
| 75 | + return ( |
| 76 | + <div style={{ padding: 16, maxWidth: 720 }}> |
| 77 | + <h2>Flush on unload</h2> |
| 78 | + <p> |
| 79 | + Each button ends a span and then immediately triggers the unload |
| 80 | + path the fix targets. Watch DevTools → Network, filter{' '} |
| 81 | + <code>v1/traces</code>, and verify the POST goes out before the |
| 82 | + navigation completes. |
| 83 | + </p> |
| 84 | + <p> |
| 85 | + Last span emitted: <code>{lastSpan || '(none)'}</code> |
| 86 | + </p> |
| 87 | + |
| 88 | + <details open style={{ marginBottom: 16 }}> |
| 89 | + <summary> |
| 90 | + <strong>Unload events from previous navigation</strong> ( |
| 91 | + {unloadLog.length}) |
| 92 | + <button |
| 93 | + onClick={() => { |
| 94 | + sessionStorage.removeItem(UNLOAD_LOG_KEY) |
| 95 | + setUnloadLog([]) |
| 96 | + }} |
| 97 | + style={{ marginLeft: 12 }} |
| 98 | + > |
| 99 | + Clear |
| 100 | + </button> |
| 101 | + </summary> |
| 102 | + <pre |
| 103 | + style={{ |
| 104 | + background: '#f6f6f6', |
| 105 | + padding: 8, |
| 106 | + fontSize: 12, |
| 107 | + maxHeight: 200, |
| 108 | + overflow: 'auto', |
| 109 | + }} |
| 110 | + > |
| 111 | + {unloadLog.length === 0 |
| 112 | + ? '(none — navigate away and come back to see events)' |
| 113 | + : unloadLog |
| 114 | + .map( |
| 115 | + (e) => |
| 116 | + `+${e.ts - unloadLog[0].ts}ms ${e.name}${e.detail ? ` (${e.detail})` : ''}`, |
| 117 | + ) |
| 118 | + .join('\n')} |
| 119 | + </pre> |
| 120 | + </details> |
| 121 | + |
| 122 | + <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}> |
| 123 | + <button |
| 124 | + onClick={() => { |
| 125 | + emitSpan('hard-nav') |
| 126 | + // Full-page navigation — fires pagehide and cancels XHR. |
| 127 | + window.location.href = '/welcome' |
| 128 | + }} |
| 129 | + > |
| 130 | + Span + hard navigate to /welcome |
| 131 | + </button> |
| 132 | + |
| 133 | + <button |
| 134 | + onClick={() => { |
| 135 | + emitSpan('hard-nav-external') |
| 136 | + window.location.href = 'https://example.com/' |
| 137 | + }} |
| 138 | + > |
| 139 | + Span + navigate to example.com (cross-origin unload) |
| 140 | + </button> |
| 141 | + |
| 142 | + <button |
| 143 | + onClick={() => { |
| 144 | + emitSpan('spa-nav') |
| 145 | + // SPA nav — does NOT fire pagehide, but the 5s batch |
| 146 | + // delay should still catch it. Use "Span (no nav)" to |
| 147 | + // compare the before/after scheduledDelayMillis change. |
| 148 | + window.history.pushState({}, '', '/welcome') |
| 149 | + window.dispatchEvent(new PopStateEvent('popstate')) |
| 150 | + }} |
| 151 | + > |
| 152 | + Span + SPA navigate (pushState, no unload) |
| 153 | + </button> |
| 154 | + |
| 155 | + <button |
| 156 | + onClick={() => { |
| 157 | + emitSpan('visibility-hidden') |
| 158 | + // Simulate tab hidden without actually navigating. |
| 159 | + // Flush path is triggered by the visibilitychange |
| 160 | + // listener registered in registerFlushOnUnload(). |
| 161 | + Object.defineProperty(document, 'visibilityState', { |
| 162 | + configurable: true, |
| 163 | + get: () => 'hidden', |
| 164 | + }) |
| 165 | + document.dispatchEvent(new Event('visibilitychange')) |
| 166 | + // Flip it back so the page remains usable. |
| 167 | + setTimeout(() => { |
| 168 | + Object.defineProperty(document, 'visibilityState', { |
| 169 | + configurable: true, |
| 170 | + get: () => 'visible', |
| 171 | + }) |
| 172 | + document.dispatchEvent( |
| 173 | + new Event('visibilitychange'), |
| 174 | + ) |
| 175 | + }, 500) |
| 176 | + }} |
| 177 | + > |
| 178 | + Span + dispatch visibilitychange=hidden |
| 179 | + </button> |
| 180 | + |
| 181 | + <button |
| 182 | + onClick={() => { |
| 183 | + emitSpan('pagehide') |
| 184 | + window.dispatchEvent( |
| 185 | + new PageTransitionEvent('pagehide'), |
| 186 | + ) |
| 187 | + }} |
| 188 | + > |
| 189 | + Span + dispatch pagehide (no nav) |
| 190 | + </button> |
| 191 | + |
| 192 | + <button |
| 193 | + onClick={() => { |
| 194 | + emitSpan('reload') |
| 195 | + window.location.reload() |
| 196 | + }} |
| 197 | + > |
| 198 | + Span + reload() |
| 199 | + </button> |
| 200 | + |
| 201 | + <button |
| 202 | + onClick={() => { |
| 203 | + emitSpan('batch') |
| 204 | + }} |
| 205 | + > |
| 206 | + Span (no nav — should flush on 5s batch timer) |
| 207 | + </button> |
| 208 | + |
| 209 | + <button |
| 210 | + onClick={() => { |
| 211 | + for (let i = 0; i < 50; i++) { |
| 212 | + emitSpan('burst') |
| 213 | + } |
| 214 | + window.location.href = '/welcome' |
| 215 | + }} |
| 216 | + > |
| 217 | + 50 spans + hard navigate (batch size sanity check) |
| 218 | + </button> |
| 219 | + </div> |
| 220 | + </div> |
| 221 | + ) |
| 222 | +} |
0 commit comments