nhagen / react-intercom

A component to configure and enable Intercom in your react application
MIT License
237 stars 72 forks source link

Lazy load #59

Closed CarloSaccone closed 5 years ago

CarloSaccone commented 5 years ago

Is it possible to lazy-load the intercom script & init event? Loading the script in the head tag has a massive impact on the page load speed and time to interactive.

GarrettJMU commented 5 years ago

x2 on this. Was just coming here to post a question about this.

stianjensen commented 5 years ago

This should be fairly straight-forward to implement. Looking at the current install example from Intercom's documentation, they actually do wait for the onload event before adding the intercom script tag to the webpage there now: https://developers.intercom.com/installing-intercom/docs/basic-javascript

I assume wrapping the following lines in a function that is called when the onload event fires should work: https://github.com/nhagen/react-intercom/blob/6d1bf31f375a87f74b3aa6c6ddb0f3d65efd9ad7/src/index.js#L35-L50

JPWallhorn commented 5 years ago

@nhagen Do you think this can be implemented? It really slows down the website performance :/

GeKorm commented 5 years ago

Is there any reason we shouldn't implement this on our side? I'm just rendering <Intercom /> after a delay/scroll trigger and it works fine. I can't think of a reason why this should be implemented by react-intercom internally. Some quick pseudocode:


class MyComponent extends Component {
  state = { intercomVisible: false };

  componentDidMount() {
    this.intercomTimeout = setTimeout(this.showIntercom, 3000);
  }

  componentWillUnmount() {
    clearTimeout(this.intercomTimeout);
  }

  showIntercom = () => {
    this.setState({ intercomVisible: true });
  };

  render() {
    return this.state.IntercomVisible && <Intercom /> 
  }
}
JPWallhorn commented 5 years ago

@GeKorm That doesn't really make a difference.

GeKorm commented 5 years ago

@JPWallhorn Have you tried it? react-intercom has no way of loading the script before the 3 seconds (or whatever trigger you set) in the example above.

JPWallhorn commented 5 years ago

@GeKorm Yes, I've implemented a 1 sec delay and it still drags down the performance & time for visible content.

GeKorm commented 5 years ago

One second is probably just not enough, I use 7-10 and a scroll trigger. Try adding just a scroll trigger and nothing else, you'll see that the <script> just isn't there on page load.

nhagen commented 5 years ago

I believe I've implemented this in #73 and v2.0.0-alpha.1. Closing, but if this is actually not addressed, please let me know before a full 2.0 release.

devrnt commented 4 years ago

@JPWallhorn @GeKorm @CarloSaccone @GarrettJMU I'm keen to announce that I've released a React Intercom integration:
https://github.com/devrnt/react-use-intercom

Features

Motivation I've found it not very developer friendly to integrate Intercom in a React application.

Note I don't want to spam this issue with advertisement, that's why I'll delete this message in the near future.