PrismJS / prism

Lightweight, robust, elegant syntax highlighting.
https://prismjs.com
MIT License
12.34k stars 1.29k forks source link

Library broken in Service worker #3794

Closed jcubic closed 7 months ago

jcubic commented 7 months ago

Information:

Library throw error in service worker when it receives a message.

Example

I've found when running on my site that it throw exception in this code:

        if (!_.disableWorkerMessageHandler) {
            // In worker
            _self.addEventListener('message', function (evt) {
                var message = JSON.parse(evt.data);
                var lang = message.language;
                var code = message.code;
                var immediateClose = message.immediateClose;

                _self.postMessage(_.highlight(code, _.languages[lang], lang));
                if (immediateClose) {
                    _self.close();
                }
            }, false);
        }

What is the purpose of this code. You basically can't send a normal message to service worker because JSON.parse will always throw error.

I've tried to set:

Prism.disableWorkerMessageHandler = true;

but it doesn't work.

jcubic commented 7 months ago

Found in source code:

This is needed before importScripts.

self.Prism = self.Prism || {};
self.Prism.disableWorkerMessageHandler = true;