PrismJS / PrismJS/prism

Web worker error on import

Open
#3,824 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
13k
Forks
1.4k
Avg merge
15h 36m
Merged PRs (30d)
3

Description

using the following sample code, i get an error immedietly when creating a worker

importScripts('https://cdn.jsdelivr.net/npm/prismjs@1.29.0/prism.min.js');
importScripts('https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-javascript.min.js');

self.onmessage = function(event) {
    
};
VM7:1 Uncaught SyntaxError: "[object Object]" is not valid JSON 
   at JSON.parse (<anonymous>) 
   at prism.js:1157:24
 (anonymous)	@	prism.js:1157

before proforming any other action but importing the lib, it does highlight though after that, but when ever i use the worker same error keeps popping up

the full web worker script

importScripts('https://cdn.jsdelivr.net/npm/prismjs@1.29.0/prism.min.js');
importScripts('https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-javascript.min.js');

self.onmessage = function(event) {
    const { content } = event.data;

    if (typeof content === 'string') {
        try {
            const highlightedCode = Prism.highlight(content, Prism.languages.javascript, 'javascript');
            self.postMessage(highlightedCode);
        } catch (error) {
            self.postMessage({ error: error.message, stack: error});
        }
    } else {
        self.postMessage({ error: 'Invalid content type' });
    }
};

any help will be gratly appricated!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the error using the two importScripts calls in the supplied web worker and inspect prism.js:1157, where the stack trace points. Confirm the worker can import Prism without the JSON SyntaxError and still highlight JavaScript through the shown onmessage handler.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.