Skip to content

Commit 53cd69e

Browse files
authored
fix: add nonce (#183)
1 parent 4e34bf3 commit 53cd69e

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

packages/experiment-tag/src/messenger.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ export const asyncLoadScript = (url: string) => {
4949
scriptElement.type = 'text/javascript';
5050
scriptElement.async = true;
5151
scriptElement.src = url;
52+
// Set the script nonce if it exists
53+
// This is useful for CSP (Content Security Policy) to allow the script to be loaded
54+
const nonceElem = document.querySelector('[nonce]');
55+
if (nonceElem) {
56+
scriptElement.setAttribute(
57+
'nonce',
58+
nonceElem['nonce'] ||
59+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
60+
(nonceElem as any).nonce ||
61+
nonceElem.getAttribute('nonce'),
62+
);
63+
}
5264
scriptElement.addEventListener(
5365
'load',
5466
() => {

0 commit comments

Comments
 (0)