Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding optional prop for lazy load the intercom script #236

Merged
merged 16 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 9 additions & 7 deletions src/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @see {@link https://developers.intercom.com/installing-intercom/docs/basic-javascript}
*/
const initialize = (appId: string) => {
const initialize = (appId: string, timeout = 0) => {
var w = window;
var ic = w.Intercom;
if (typeof ic === 'function') {
Expand All @@ -23,12 +23,14 @@ const initialize = (appId: string) => {
};
w.Intercom = i;
var l = function() {
var s = d.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'https://widget.intercom.io/widget/' + appId;
var x = d.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
setTimeout(function() {
var s = d.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'https://widget.intercom.io/widget/' + appId;
var x = d.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
}, timeout);
};
if (document.readyState === 'complete') {
l();
Expand Down
3 changes: 2 additions & 1 deletion src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const IntercomProvider: React.FC<IntercomProviderProps> = ({
onUnreadCountChange,
shouldInitialize = !isSSR,
apiBase,
initializeDelayInMs,
devrnt marked this conversation as resolved.
Show resolved Hide resolved
...rest
}) => {
const isBooted = React.useRef(autoBoot);
Expand All @@ -36,7 +37,7 @@ export const IntercomProvider: React.FC<IntercomProviderProps> = ({
);

if (!isSSR && !window.Intercom && shouldInitialize) {
initialize(appId);
initialize(appId, initializeDelayInMs);
// Only add listeners on initialization
if (onHide) IntercomAPI('onHide', onHide);
if (onShow) IntercomAPI('onShow', onShow);
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,10 @@ export type IntercomProviderProps = {
* Format https://${INTERCOM_APP_ID}.intercom-messenger.com
*/
apiBase?: string;
/**
* Indicates if the intercom initialization should be delayed
devrnt marked this conversation as resolved.
Show resolved Hide resolved
*
* @remarks If not set delay is set to 0ms
* */
initializeDelayInMs?: number;
};