3

I have a lwc component, used on internal platform app and experience site, which listens to platform event. We use empApi to subscribe to platform events when user is internal and cometD library to subscribe when user is experience site user. This component stopped working for experience site users after we enabled Lightning Web Security in our org. We are getting below error

Error occurred initializing/configuring cometD Error: Lightning Web Security: Cannot create Worker with blob:https://ascapone--ashishdev.sandbox.my.site.com/b1b66214-97c6-4ac2-9b82-29456a499cda

Going through the error log, I can see the error is thrown at some point duing cometD.configure function execution. Error screenshot

//This function subscribes to the channel using the cometD library
initializeCometD(sessionId) {
    try {
        if (this.cometDInitialized) {
            return;
        }

        this.cometDInitialized = true;
        let cometD = new window.org.cometd.CometD();
        cometD.configure({
            url: window.location.protocol + '//' + window.location.hostname + '/cometd/58.0/',
            requestHeaders: { Authorization: 'Bearer ' + sessionId },
            appendMessageTypeToURL: false,
            logLevel: 'debug'
        });
        cometD.websocketEnabled = false;
        cometD.handshake(status => {
            if (status.successful) {
                //console.log('Status ', status);
                cometD.subscribe(CHANNEL_NAME, this.handleEvent);
                if (this.enableTimer) {
                    this.getTimeOutValueAndEnableTimer();
                }
            } else {
                console.error('Error in handshaking: ' + JSON.stringify(status));
            }
        });
    } catch (error) {
        if (this.enableTimer) {
            this.getTimeOutValueAndEnableTimer();
        }
        console.error('Error occurred initializing/configuring cometD', error);
    }

}

2 Answers 2

0

Edit: The issue was caused by updating CometD code inside a static resource from 3.1.1 to 3.1.14. Disabling Lightning Web Security makes it go away. Don't have any fix as of now

1
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – Derek F
    Commented Apr 24 at 13:40
0

I was able to resolve this issue by changing the cometD.js. As seen in the error, Lightning Web Security is not allowing creation of worker with Blob.

cometD.js has function

function _configure(configuration)

which has the condition

if (window.Worker && window.Blob && window.URL && _config.useWorkerScheduler) 

changed this condition to

if (window.Worker && window.Blob && window.URL && _config.useWorkerScheduler && false)

which prevents the following code block from execution. This code block was the cause of error.

let code = WorkerScheduler.toString();
// Remove the function declaration, the opening brace and the closing brace.
code = code.substring(code.indexOf('{') + 1, code.lastIndexOf('}'));
const blob = new window.Blob([code], {
      type: 'application/json'
});
const blobURL = window.URL.createObjectURL(blob);
const worker = new window.Worker(blobURL);
_scheduler.setTimeout = (funktion, delay) => {
      const id = _scheduler.register(funktion);
      worker.postMessage({
            id: id,
            type: 'setTimeout',
            delay: delay
      });
      return id;
};
_scheduler.clearTimeout = id => {
      _scheduler.unregister(id);
      worker.postMessage({
             id: id,
             type: 'clearTimeout'
      });
};
worker.onmessage = e => {
      const id = e.data.id;
      const funktion = _scheduler.unregister(id);
      if (funktion) {
          funktion();
      }
};

You must log in to answer this question.

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