6

I have a LWC which is doing some conditional display. The LWC is encapsulated in a Lightning Component and receives a record id from it. When opening the parent Lightning Component for different records one after the other, the content is cached and it displays the content corresponding to the first record for all subsequent records.

The conditional display calculations are performed in the connectedCallback method, but it seems like the second time I open the Lightning component, connectedCallback is not executed at all.

As a note, Enable secure and persistent browser caching to improve performance is disabled in Session Settings.

How can I avoid this?


Edit: Here is the relevant piece of code to reproduce this: AuraContainer.cmp:

<aura:component implements="lightning:isUrlAddressable,force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId">
    <c:sendemail onshowtoast="{!c.handleShowToast}"/>
</aura:component>

SendEmail LWC:

import { LightningElement, api, wire } from 'lwc';
import { CurrentPageReference } from 'lightning/navigation';
import checkOrgValidity from '@salesforce/apex/SendEmailController.checkOrgValidity'
import { ShowToastEvent } from 'lightning/platformShowToastEvent'

export default class SendEmail extends LightningElement {
    @api recordId;

    // Injects the page reference that describes the current page
    @wire(CurrentPageReference)
        currentPageReference;

    async connectedCallback() {
        // the log is only displayed on the first load of the lightning component
        console.log('connectedCallback');
        this.recordId = this.currentPageReference.state.c__recordId;

        checkOrgValidity({sId: this.recordId})
                    .then(result => {
                        // processing in case of success                           
                    })
                    .catch(error => { });
    }
}

The Lightning component gets opened using a Detail Page Button:

Behavior: Display in existing window with sidebar
Button or Link URL: /lightning/cmp/c__SendEmailContainer?c__recordId={!Account.Id}
5
  • can you add relevant code so we can reproduce same at our end? Commented Apr 11, 2019 at 8:21
  • You can see the code implementation in this post.
    – Teodora
    Commented Apr 11, 2019 at 10:20
  • Please update your post with a minimal reproduce-able code sample instead of providing a link
    – glls
    Commented Apr 11, 2019 at 13:31
  • At a high level, what you're hitting is expected behavior where it might actually only be connected once. Aura had something similar and the fix was to listen to URL hash change or the pageRef object change and that's how you know it's a 2nd time.
    – tsalb
    Commented Apr 11, 2019 at 19:09
  • Same happening over here and still have found no way to force reconstruction Commented Apr 22, 2019 at 8:48

1 Answer 1

4

Managed to fix it by adding a page reference change handler and forcing a refresh:

AuraContainer.cmp:

<aura:component implements="lightning:isUrlAddressable,force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="recordId" type="Id" />
    <aura:handler name="change" value="{!v.pageReference}" action="{!c.onPageReferenceChanged}" />
    <c:sendemail onshowtoast="{!c.handleShowToast}"/>
</aura:component>

AuraContainerController.js:

({
    doInit : function(component, event, helper) {
        console.log(component.get("v.recordId"));
    },
    onPageReferenceChanged: function(cmp, event, helper) {
        var myPageRef = cmp.get("v.pageReference");
        var recordId = myPageRef.state.c__recordId;
        cmp.set("v.recordId", recordId);
        $A.get('e.force:refreshView').fire();
    }
 })
1
  • Perfect, I had literally the same issue. This could be seriously detrimental if data is preserved on the UI and then saved to the database. Great fix. Commented Jun 11, 2020 at 19:15

You must log in to answer this question.

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