-
-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathreact-bootloader.tsx
More file actions
481 lines (423 loc) · 16.1 KB
/
react-bootloader.tsx
File metadata and controls
481 lines (423 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import React from 'react'
import { createRoot } from 'react-dom/client'
import * as Sentry from '@sentry/react'
import { ExercismTippy } from '../components/misc/ExercismTippy'
import { QueryClientProvider } from '@tanstack/react-query'
import {
isChunkLoadError,
safeReloadForChunkError,
} from './chunk-load-error-handler'
type ErrorBoundaryType = React.ComponentType<any>
type GeneratorFunc = (data: any, elem: HTMLElement) => JSX.Element
type Mappings = Record<string, GeneratorFunc>
type TurboFrameRenderDetail = {
fetchResponse: {
response: {
headers: {
get: (name: string) => string | null
}
}
}
}
if (process.env.SENTRY_DSN) {
Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.NODE_ENV,
enabled: process.env.NODE_ENV === 'production',
sendDefaultPii: false,
beforeSend: (event) => {
// Drop non-actionable dynamic import failures (network issues, stale chunks).
// Error messages vary by browser:
// Chrome/Edge: "Failed to fetch dynamically imported module: <url>"
// Firefox: "error loading dynamically imported module: <url>"
// Safari: "Importing a module script failed."
const isDynamicImportError = event.exception?.values?.some((ex) => {
const msg = ex.value?.toLowerCase() || ''
return (
msg.includes('failed to fetch dynamically imported module') ||
msg.includes('error loading dynamically imported module') ||
msg.includes('importing a module script failed')
)
})
if (isDynamicImportError) return null
// Drop non-actionable Cloudflare Turnstile widget errors (browser extensions, privacy settings, etc.)
const isTurnstileError = event.exception?.values?.some(
(ex) =>
ex.type?.includes('TurnstileError') ||
ex.value?.includes('TurnstileError') ||
ex.value?.includes('[Cloudflare Turnstile]')
)
if (isTurnstileError) return null
// Drop AbortErrors — expected when fetch requests are cancelled
// (e.g. user navigates away, component unmounts, React Query cancels stale requests)
const isAbortError = event.exception?.values?.some(
(ex) =>
ex.type === 'AbortError' ||
ex.value?.includes('AbortError') ||
ex.value?.includes('Fetch is aborted') ||
ex.value?.includes('The operation was aborted') ||
ex.value?.includes('signal is aborted without reason')
)
if (isAbortError) return null
// Drop non-actionable cross-origin postMessage errors (third-party iframes: Turnstile, YouTube, etc.)
const isInvalidOriginError = event.exception?.values?.some((ex) =>
ex.value?.includes('invalid origin')
)
if (isInvalidOriginError) return null
// Drop non-actionable iframe readiness errors (third-party scripts or browser internals
// accessing iframe.contentWindow before the frame is ready)
const isFrameNotReadyError = event.exception?.values?.some((ex) =>
ex.value?.includes('frame window is not ready')
)
if (isFrameNotReadyError) return null
// Drop non-actionable network errors (connectivity loss, DNS failures, browser extensions blocking requests).
// Messages vary by browser: Chrome="Failed to fetch", Firefox="NetworkError…", Safari="Load failed"
const isNetworkError = event.exception?.values?.some(
(ex) =>
ex.value?.includes(
'NetworkError when attempting to fetch resource'
) ||
ex.value?.includes('Failed to fetch') ||
ex.value?.includes('Load failed')
)
if (isNetworkError) return null
// Drop non-actionable DOM removeChild errors (third-party scripts like reCAPTCHA
// trying to clean up nodes that React has already unmounted)
const isRemoveChildError = event.exception?.values?.some((ex) =>
ex.value?.includes("Failed to execute 'removeChild' on 'Node'")
)
if (isRemoveChildError) return null
// Drop errors from obfuscated browser extensions or injected scripts.
// Obfuscated code uses hex variable names like _0x4761, _0x5e4966 etc.
// Safari reports "Can't find variable: _0x...", Chrome reports "_0x... is not defined".
// Also filter when stack frames reference webkit-masked-url (WebKit extension sandbox).
const isObfuscatedExtensionError = event.exception?.values?.some((ex) => {
const hasObfuscatedVar = /_0x[0-9a-f]+/.test(ex.value ?? '')
const hasMaskedUrl = ex.stacktrace?.frames?.some((frame) =>
frame.filename?.includes('webkit-masked-url')
)
return hasObfuscatedVar || hasMaskedUrl
})
if (isObfuscatedExtensionError) return null
// Drop non-actionable promise rejections with non-Error values
// (no stack trace, arbitrary strings from third-party code or browser extensions)
const isNonErrorRejection = event.exception?.values?.some((ex) =>
ex.value?.includes('Non-Error promise rejection captured with value:')
)
if (isNonErrorRejection) return null
// Drop errors from student code running in blob URLs (bootcamp JS
// exercises execute student solutions via dynamically created blob:
// modules — errors from those are expected, not application bugs)
const isStudentCodeError = event.exception?.values?.some((ex) =>
ex.stacktrace?.frames?.some((frame) =>
frame.filename?.startsWith('blob:')
)
)
if (isStudentCodeError) return null
// Drop non-actionable localStorage/sessionStorage SecurityErrors (Safari private
// browsing, restricted iframes, or strict cookie settings block storage access entirely)
const isStorageAccessError = event.exception?.values?.some(
(ex) =>
ex.value?.includes(
"read the 'localStorage' property from 'Window'"
) ||
ex.value?.includes("read the 'sessionStorage' property from 'Window'")
)
if (isStorageAccessError) return null
// Drop non-actionable "Maximum call stack size exceeded" errors.
// These come from browser extensions, third-party scripts, or users
// with cached bundles (the root cause in app code was fixed in #8408).
// Stack traces for these are truncated by browsers and not useful in
// minified production bundles.
const isStackOverflowError = event.exception?.values?.some(
(ex) =>
ex.type === 'RangeError' &&
ex.value?.includes('Maximum call stack size exceeded')
)
if (isStackOverflowError) return null
// Drop non-actionable Stripe.js loading failures (ad blockers,
// privacy extensions, or network issues preventing the script from loading).
// Users already see a friendly fallback message in the UI.
const isStripeLoadError = event.exception?.values?.some((ex) =>
ex.value?.includes('Failed to load Stripe.js')
)
if (isStripeLoadError) return null
const tag = document.querySelector<HTMLMetaElement>(
'meta[name="user-id"]'
)
if (tag) {
event.user = { id: tag.content }
return event
}
// Sample 1% of errors from logged-out users
return Math.random() < 0.01 ? event : null
},
})
}
let ErrorBoundary: ErrorBoundaryType = ({ children }) => <>{children}</>
if (process.env.SENTRY_DSN) {
ErrorBoundary = Sentry.ErrorBoundary
}
// Reload the page when a dynamic import fails due to stale chunks after deployment.
// This catches failures that occur outside React's error boundary tree.
window.addEventListener('unhandledrejection', (event) => {
if (isChunkLoadError(event.reason)) {
event.preventDefault()
safeReloadForChunkError()
}
})
const chunkErrorFallback: Sentry.FallbackRender = ({ error }) => {
if (isChunkLoadError(error)) {
safeReloadForChunkError()
}
return <></>
}
// Asynchronously appends a stylesheet to the head and resolves
// the promise when it's finished loading.
const loadStylesheet = function (url: string): Promise<void> {
return new Promise((resolve, reject) => {
const link = document.createElement('link')
link.type = 'text/css'
link.rel = 'stylesheet'
link.onload = () => resolve()
link.onerror = () => reject(new Error(`Failed to load stylesheet: ${url}`))
link.href = url
document.getElementsByTagName('head')[0].appendChild(link)
})
}
function initEventListeners() {
// As we have conditional stylesheets per page, we need to extract and
// render those when the frame changes. We want the CSS to load BEFORE
// then HTML renders, so we get any stylesheets downloaded and THEN render
// continnue processing the frame render.
document.addEventListener('turbo:before-frame-render', (e: Event) => {
if (!(e instanceof CustomEvent)) return
const hrefs = Array.from(
e.detail.newFrame.querySelectorAll('link[rel="stylesheet"]')
)
.map((link) => (link as HTMLLinkElement).getAttribute('href'))
.filter((href): href is string => href != null && href.length > 0)
// If we have no stylesheets, just continue
if (hrefs.length == 0) {
return
}
// Pause rendering until stylesheets are loaded
e.preventDefault()
// Load stylesheets in parallel asynchronously
const promises = hrefs.map((href) => loadStylesheet(href))
// When they're all loaded (or if any fail), resume rendering.
// Use allSettled so a failed stylesheet never blocks the Turbo frame.
Promise.allSettled(promises).then(() => e.detail.resume())
})
// This changes any extra things that need changing from the
// turbo frame, such as body class or page title
document.addEventListener('turbo:frame-render', (e) => {
const event = e as CustomEvent<TurboFrameRenderDetail>
const bodyClass = event.detail.fetchResponse.response.headers.get(
'exercism-body-class'
)
if (bodyClass) {
document.body.className = bodyClass
}
})
document.addEventListener('turbo:before-fetch-request', () => {
setTurboStyle('* { cursor:wait !important }')
})
document.addEventListener('turbo:before-render', () => {
window.scrollTo({
top: 0,
left: 0,
behavior: 'auto',
})
setTurboStyle('')
})
}
/********************************************************/
/** Add a loading cursor when a turbo-frame is loading **/
/********************************************************/
const styleElemId = 'turbo-style'
const setTurboStyle = (style: string) => {
let styleElem = document.getElementById(styleElemId)
if (!styleElem) {
styleElem = document.createElement('style')
styleElem.id = styleElemId
document.head.appendChild(styleElem)
}
styleElem.textContent = style
}
export function initReact(mappings: Mappings): void {
const renderThings = () => {
renderComponents(document.body, mappings)
renderTooltips(document.body, mappings)
}
// This adds rendering for all future turbo clicks
document.addEventListener('turbo:load', () => {
renderThings()
})
// This renders if turbo has already finished at the
// point at which this calls. See packs/core.tsx
if (window.turboLoaded) {
renderThings()
}
}
let googleTranslatePatched = false
function onGoogleTranslateDetected(): void {
if (googleTranslatePatched) return
googleTranslatePatched = true
// See: https://github.com/facebook/react/issues/11538#issuecomment-417504600
if (typeof Node === 'function' && Node.prototype) {
const originalRemoveChild: (child: Node) => Node =
Node.prototype.removeChild
Node.prototype.removeChild = function <T extends Node>(
this: Node,
child: T
): T {
if (child.parentNode !== this) {
if (console) {
console.error(
'Cannot remove a child from a different parent',
child,
this
)
}
return child
}
return originalRemoveChild.apply(this, [child]) as T
}
const originalInsertBefore: (
newNode: Node,
referenceNode: Node | null
) => Node = Node.prototype.insertBefore
Node.prototype.insertBefore = function <T extends Node>(
this: Node,
newNode: T,
referenceNode: Node | null
): T {
if (referenceNode && referenceNode.parentNode !== this) {
if (console) {
console.error(
'Cannot insert before a reference node from a different parent',
referenceNode,
this
)
}
return newNode
}
return originalInsertBefore.apply(this, [newNode, referenceNode]) as T
}
}
}
// Currently only Chrome produces this React bug, so let's not use this piece of code on other browsers
if (/Chrome/.test(navigator.userAgent)) {
// We need to use MutationObserver here because translating almost only happens as a DOM mutation
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (
mutation.type === 'attributes' &&
mutation.attributeName === 'class'
) {
const htmlElement = document.documentElement
const hasTranslationClasses =
htmlElement.classList.contains('translated-ltr') ||
htmlElement.classList.contains('translated-rtl')
if (hasTranslationClasses) {
onGoogleTranslateDetected()
}
}
})
})
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ['class'],
})
}
const roots = new WeakMap()
const eventListeners = new WeakSet()
const render = (elem: HTMLElement, component: React.ReactNode) => {
let root = roots.get(elem)
if (!root) {
root = createRoot(elem)
roots.set(elem, root)
}
root.render(
<React.StrictMode>
<QueryClientProvider client={window.queryClient}>
<ErrorBoundary fallback={chunkErrorFallback}>{component}</ErrorBoundary>
</QueryClientProvider>
</React.StrictMode>
)
// make sure we only add the event listener once per element
if (!eventListeners.has(elem)) {
eventListeners.add(elem)
document.addEventListener('turbo:before-frame-render', () => {
if (elem.dataset.persistent === 'true') return
const rootToCleanup = roots.get(elem)
if (rootToCleanup) {
rootToCleanup.unmount()
roots.delete(elem)
eventListeners.delete(elem)
}
})
}
}
export function renderComponents(
parentElement: HTMLElement,
mappings: Mappings
): void {
if (!parentElement) {
parentElement = document.body
}
// As getElementsByClassName returns a live collection, it is recommended to use Array.from
// when iterating through it, otherwise the number of elements may change mid-loop.
const elems = Array.from(
parentElement.getElementsByClassName('c-react-component')
)
for (const elem of elems) {
// dataset doesn't exist on type `Element`
if (!(elem instanceof HTMLElement)) continue
if (
elem.dataset.persistent === 'true' &&
elem.dataset.rendered === 'true'
) {
continue
}
const reactId = elem.dataset['reactId']
const reactData = elem.dataset.reactData
const generator = reactId ? mappings[reactId] : null
if (reactId && generator && reactData) {
const data = JSON.parse(reactData)
render(elem, generator(data, elem))
if (elem.dataset.persistent === 'true') {
elem.dataset.rendered = 'true'
}
}
}
}
function renderTooltips(parentElement: HTMLElement, mappings: Mappings) {
if (!parentElement) {
parentElement = document.body
}
parentElement
.querySelectorAll('[data-tooltip-type][data-endpoint]')
.forEach((elem) => renderTooltip(mappings, elem as HTMLElement))
}
function renderTooltip(mappings: Mappings, elem: HTMLElement) {
const name = elem.dataset['tooltipType'] + '-tooltip'
const generator = mappings[name]
if (!generator) {
return
}
const component = generator(elem.dataset, elem)
const tooltipElem = document.createElement('span')
elem.insertAdjacentElement('afterend', tooltipElem)
render(
tooltipElem,
<ExercismTippy
interactive={elem.dataset.interactive === 'true'}
renderReactComponents={elem.dataset.renderReactComponents === 'true'}
content={component}
reference={elem}
/>
)
}
initEventListeners()