Skip to content

Commit 4cfe0f8

Browse files
authored
Merge pull request #784 from Pruxis/main
Add support for Content-Security-Policy
2 parents 8eb058d + 35bccb0 commit 4cfe0f8

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

.changeset/five-terms-smell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-use-intercom': patch
3+
---
4+
5+
Add support for Content-Security-Policy nonce

packages/react-use-intercom/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@
9494
"tsup": "^8.3.6",
9595
"typescript": "^5.7.3"
9696
}
97-
}
97+
}

packages/react-use-intercom/src/initialize.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
*
88
* @param appId - Intercom app id
99
* @param [timeout=0] - Amount of milliseconds that the initialization should be delayed, defaults to 0
10+
* @param [cspNonce=undefined] - Content-Security-Policy nonce to use for the Intercom <script> tag during initializing
1011
*
1112
* @see {@link https://developers.intercom.com/installing-intercom/docs/basic-javascript}
1213
*/
13-
const initialize = (appId: string, timeout = 0) => {
14+
const initialize = (appId: string, timeout: number = 0, cspNonce?: string) => {
1415
var w = window;
1516
var ic = w.Intercom;
1617
if (typeof ic === 'function') {
@@ -31,6 +32,7 @@ const initialize = (appId: string, timeout = 0) => {
3132
var s = d.createElement('script');
3233
s.type = 'text/javascript';
3334
s.async = true;
35+
if (cspNonce) s.nonce = cspNonce;
3436
s.src = 'https://widget.intercom.io/widget/' + appId;
3537
var x = d.getElementsByTagName('script')[0];
3638
x.parentNode.insertBefore(s, x);

packages/react-use-intercom/src/provider.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const IntercomProvider: React.FC<
2828
shouldInitialize = !isSSR,
2929
apiBase,
3030
initializeDelay,
31+
cspNonce,
3132
...rest
3233
}) => {
3334
const isBooted = React.useRef(false);
@@ -101,8 +102,8 @@ export const IntercomProvider: React.FC<
101102
],
102103
);
103104

104-
if (!isSSR && shouldInitialize && !isInitialized.current) {
105-
initialize(appId, initializeDelay);
105+
if (!isSSR && shouldInitialize && !isInitialized.current) {
106+
initialize(appId, initializeDelay, cspNonce);
106107

107108
if (autoBoot) {
108109
boot(autoBootProps);

packages/react-use-intercom/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,8 @@ export type IntercomProviderProps = {
521521
* Pass properties to `boot` method when `autoBoot` is `true`
522522
*/
523523
autoBootProps?: IntercomProps;
524+
/**
525+
* Content-Security-Policy nonce to use for the Intercom <script> tag during initializing
526+
*/
527+
cspNonce?: string;
524528
};

0 commit comments

Comments
 (0)