Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/five-terms-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-use-intercom': patch
---

Add support for Content-Security-Policy nonce
2 changes: 1 addition & 1 deletion packages/react-use-intercom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@
"tsup": "^8.3.6",
"typescript": "^5.7.3"
}
}
}
4 changes: 3 additions & 1 deletion packages/react-use-intercom/src/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
*
* @param appId - Intercom app id
* @param [timeout=0] - Amount of milliseconds that the initialization should be delayed, defaults to 0
* @param [cspNonce=undefined] - Content-Security-Policy nonce to use for the Intercom <script> tag during initializing
*
* @see {@link https://developers.intercom.com/installing-intercom/docs/basic-javascript}
*/
const initialize = (appId: string, timeout = 0) => {
const initialize = (appId: string, timeout: number = 0, cspNonce?: string) => {
var w = window;
var ic = w.Intercom;
if (typeof ic === 'function') {
Expand All @@ -31,6 +32,7 @@ const initialize = (appId: string, timeout = 0) => {
var s = d.createElement('script');
s.type = 'text/javascript';
s.async = true;
if (cspNonce) s.nonce = cspNonce;
s.src = 'https://widget.intercom.io/widget/' + appId;
var x = d.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
Expand Down
5 changes: 3 additions & 2 deletions packages/react-use-intercom/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const IntercomProvider: React.FC<
shouldInitialize = !isSSR,
apiBase,
initializeDelay,
cspNonce,
...rest
}) => {
const isBooted = React.useRef(false);
Expand Down Expand Up @@ -101,8 +102,8 @@ export const IntercomProvider: React.FC<
],
);

if (!isSSR && shouldInitialize && !isInitialized.current) {
initialize(appId, initializeDelay);
if (!isSSR && shouldInitialize && !isInitialized.current) {
initialize(appId, initializeDelay, cspNonce);

if (autoBoot) {
boot(autoBootProps);
Expand Down
4 changes: 4 additions & 0 deletions packages/react-use-intercom/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,8 @@ export type IntercomProviderProps = {
* Pass properties to `boot` method when `autoBoot` is `true`
*/
autoBootProps?: IntercomProps;
/**
* Content-Security-Policy nonce to use for the Intercom <script> tag during initializing
*/
cspNonce?: string;
};
Loading