1

I have built a lightning component which is place in the opportunity page. Lately we have been getting random errors trigger by this component.

It has been impossible for me to recreate the error to try understand what is happening as sometimes this error is happening when users are in a different page, the image below the user was in a contact page (this component is only in the opportunity page). The user did have other tabs open in the browser, but non of them was an opportunity record.

Based on the message it seems that the problem is when using the notificationsLibrary

<lightning:notificationsLibrary aura:id="notifLib"/>

JS:

component.find('notifLib').showNotice({
    "variant": variant_txt,
    "header": header_txt,
    "message": msg_txt
}); 

Error

Why is this happening?

2 Answers 2

4

It appears that your component never unsubscribed from the EMP API. This means that the library is holding a persistent reference to the component, even after it would have been unloaded, which causes both unnecessary event notifications as well as a memory leak that can only be resolved by reloading the page. Each time the component is loaded, it would be subscribed again, resulting in multiple event notifications and more memory leakage.

Make sure you're unsubscribing from the event notifications in an aura:valueDestroy handler:

<aura:handler name="destroy" value="{!this}" action="{!c.handleDestroy}"/>

...

    const empApi = component.find('empApi');
    // Get the subscription that we saved when subscribing
    const subscription = component.get('v.subscription');

    // Unsubscribe from event
    empApi.unsubscribe(subscription, $A.getCallback(unsubscribed => {
      // Confirm that we have unsubscribed from the event channel
      console.log('Unsubscribed from channel '+ unsubscribed.subscription);
      component.set('v.subscription', null);
    }));
3
  • in the documentation the unsubscribe is based on a button, if the user do this, and another event is stream then the component will not be trigger right? as it was unsubscribe? what will be the best way then to handle this as I dont want the component to miss out, but the user will probably forget to click the unsubscribe button before closing the window tab
    – manza
    Mar 23, 2022 at 23:53
  • @manza No button required, just do so in the disconnectedCallback handler. When your component is remove from the DOM, it will also stop getting events.
    – sfdcfox
    Mar 24, 2022 at 1:34
  • by disconnectedCallback are you talking about aura:valuedestroy? @sfdcfox, I am confused as the documentation talks about salesforce mobile nav menu, so this apply also to regular browser window?
    – manza
    Mar 25, 2022 at 1:10
1

The simple answer is that you can't find notifLib and you not checking if it's present before attempting to use it.

Fix:

const lib = component.find('notifLib');
if (!lib){
  return;
}
lib.showNotice({
  "variant": variant_txt,
  "header": header_txt,
  "message": msg_txt
}); 

You should definitely be stopping the empApi listener on unload as well, which would prevent this happening also.

This would involve calling the empApi unsubscribe method, probably on the Component Aura unload callback.

@sfdcfox describes this in more detail in his answer.

3
  • Thank you, yeah Iam implementing this, however, why I dont understand is: why it will fail to load if the cmp file ocntains the library and also why is it triggering in a page where the lightning component is not loading
    – manza
    Mar 22, 2022 at 6:50
  • 2
    undefined/null check and that is likely enough is a bad idea. If this is happening, that means there's a memory leak somewhere. One should definitely unsubscribe from the emp api when unloaded.
    – sfdcfox
    Mar 22, 2022 at 11:47
  • @sfdcfox I actually add into a try catch const lib = component.find('notifLib'); and my users are still reporting the component error pop up
    – manza
    Mar 23, 2022 at 23:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .