(1) I have a Lightning Component Tab on which sits a Lightning Web Component.
(2) I navigate to the tab WITH CONTEXT using the NavigationMixin from other LWC components elsewhere in the application.
(3) So the URL when I navigate to the tab/lwc looks like this:
https://revdev1-dev-ed.lightning.force.com/lightning/n/Freight_Plan_Console?c__loadId=a0r1U00000600mNQAQ
Note the context parameter c__loadId. This value of course changes based on the Load (page) I am coming from.
Here is my issue:
(1) The first time the tab is navigated to everything works fine — The constructor, connectedCallback and @wire functions all execute and build the component. I am able to capture the URL parameters and act on them.
(2) However, subsequent navigation to the tab with a different load context does not work. The LWC is cached somewhere and so the functions above are not re-invoked.
(3) This is a problem as the URL context parameter has changed and I need to execute logic to deal with the change.
(4) Note that if I navigate around a lot or browser-refresh a page the LWC will rebuild itself correctly. But of course we cannot rely on that.
Another way to pose the question: When you navigate away from and then back to a Tab with an LWC on it, what hook can I use to execute code each time the LWC/tab is returned to?
Another way to pose the question: How do I guarantee an LWC is rebuilt each time it is navigated to?
Thanks!
Adding navigation code to LWC Tab:
addFreightPlanHandler() {
let namespace = LightningUtil.getNamespace();
let cnamespace = namespace ? namespace : 'c__';
let state = {};
state[cnamespace + 'loadId'] = this.load.Id.value;
this[NavigationMixin.Navigate]({ type: 'standard__navItemPage',
attributes: { apiName: namespace + 'Freight_Plan_Console' },
state: state });
}
Adding first part of LWC that we are navigating to. Nothing unique about it that I am aware of:
<template>
<lightning-layout multiple-rows="false">
<!-- FILTER/TEMPLATE PANEL -->
<template if:true={showFilterTemplatePanel}>
<lightning-layout-item size="3">
<div class="slds-card">
<div class="panelSpacer">
.....
Best Answer
We had a similar cache issue in the past and managed to resolve it using
@wire(CurrentPageReference)
and getter/setter methods instead ofconnectedCallback
. [as @nbrown pointed out in the comments]Reference docs :
@wire(CurrentPageReference)
captures the updated parameter state each time the page is loaded, then you can invoke your custom logic based on the new state parameter value.Here's some sample code to get you started using
CurrentPageReference
LWC componnent tab
HTML
JS
XML
LWC redirecting to custom tab
HTML
JS
XML