-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathNoJSDetector.tsx
56 lines (53 loc) · 1.93 KB
/
NoJSDetector.tsx
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
import React from "react"
/**
* For more details about these components, see `browser-support.md`.
*/
// Detect if JavaScript is enabled and set a class on the HTML element.
// Also detect if the browser doesn't support `<script type="module">`, or if there's a parsing error
// happening in old browsers because "relatively new" syntax is being used.
// Sets the `js-enabled` class if JavaScript is enabled, and `js-disabled` if not or if an error ocurred.
export const NoJSDetector = ({ baseUrl }: { baseUrl: string }) => (
<script
dangerouslySetInnerHTML={{
__html: `
function setJSEnabled(enabled) {
var elem = window.document.documentElement;
if (enabled) {
elem.classList.remove("js-disabled");
elem.classList.add("js-enabled");
} else {
elem.classList.remove("js-enabled");
elem.classList.add("js-disabled");
}
}
if ("noModule" in HTMLScriptElement.prototype) {
setJSEnabled(true);
} else {
setJSEnabled(false);
}
window.onerror = function (err, url) {
var isOurSyntaxError = typeof err === "string" && err.indexOf("SyntaxError") > -1 && url.indexOf("${baseUrl}") > -1;
if (isOurSyntaxError) {
console.error("Caught global syntax error", err, url);
setJSEnabled(false);
}
}`,
}}
></script>
)
// Attaches `onerror` event listeners to all scripts with a `data-attach-owid-error-handler` attribute.
// If they fail to load, it will set the `js-disabled` class on the HTML element.
export const ScriptLoadErrorDetector = () => (
<script
dangerouslySetInnerHTML={{
__html: `
document.querySelectorAll("script[data-attach-owid-error-handler]").forEach(script => {
script.onerror = () => {
console.log(new Error("Failed to load script: ", script.src));
document.documentElement.classList.add("js-disabled");
document.documentElement.classList.remove("js-enabled");
}
})`,
}}
/>
)