Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion .github/workflows/e2e-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
# Alternative: create and commit an empty cypress/screenshots folder
# to always have something to upload
- name: Upload Cypress artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: failure()
with:
name: cypress-screenshots
Expand Down
14 changes: 14 additions & 0 deletions packages/react-use-intercom/.changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
Comment thread
Pruxis marked this conversation as resolved.
Outdated
"$schema": "https://unpkg.com/@changesets/config@2.0.0/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"ignore": [
"playground",
"gatsby-example",
"nextjs-example"
],
"access": "public",
"updateInternalDependencies": "patch",
"baseBranch": "main",
"bumpVersionsWithWorkspaceProtocolOnly": true
}
5 changes: 5 additions & 0 deletions packages/react-use-intercom/.changeset/happy-squids-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
Comment thread
Pruxis marked this conversation as resolved.
'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 @@ -87,4 +87,4 @@
"tsup": "^6.6.3",
"typescript": "^4.9.4"
}
}
}
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 @@ -4,10 +4,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 @@ -28,6 +29,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
13 changes: 11 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 @@ -102,15 +103,23 @@ export const IntercomProvider: React.FC<

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

if (autoBoot) {
boot(autoBootProps);
}

isInitialized.current = true;
}
}, [appId, autoBoot, autoBootProps, boot, initializeDelay, shouldInitialize]);
}, [
appId,
autoBoot,
autoBootProps,
boot,
initializeDelay,
shouldInitialize,
cspNonce,
]);

const ensureIntercom = React.useCallback(
(functionName: string, callback: (() => void) | (() => string)) => {
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 @@ -515,4 +515,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;
};