3

I would like to experiment with dynamic LWC components in my managed package. I also would like to keep the option for some clients to use my package in case some other package requires keeping the LWS turned off. So, for that purpose, I would like to be able to go with 2 different ways in the same LWC component - in one case use direct c-component inclusion in my html and in the other use the dynamic approach.

Is there a way to get that information inside LWC or via Apex controller call? but in case

1 Answer 1

2

We articulated how to approach this via the developer blog here

In nutshell you can use below code

function hasLightningWebSecurityEnabled() {
try {
  new Worker();
 } catch ({ message }) {
    if (message.includes('Lightning Web Security: Cannot create Worker')) {
       return true;
   }
 }
  return false;
}

You can use something like below

import { LightningElement } from 'lwc';

export default class LwsChecker extends LightningElement {
   hasLightningWebSecurityEnabled() {
    try {
      new Worker();
    } catch ({ message }) {
      if (message.includes('Lightning Web Security: Cannot create Worker')) {
        return true;
      }
    }
    return false;
}
connectedCallback() {
    let lwsIsEnabled = this.hasLightningWebSecurityEnabled();
    console.log(lwsIsEnabled);
    if (lwsIsEnabled) {
    //     do LWS-only stuff
    } else {
       //     fall back to not using LWS-only features
     }
  }
}

You must log in to answer this question.

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