microsoft / monaco-editor

A browser based code editor
https://microsoft.github.io/monaco-editor/
MIT License
40.2k stars 3.58k forks source link

[Bug] workerMain and tsWorker are fetched each time setCompilerOptions is called #2960

Open stefandobre opened 2 years ago

stefandobre commented 2 years ago

Reproducible in vscode.dev or in VS Code Desktop?

Reproducible in the monaco editor playground?

Monaco Editor Playground Code

monaco.editor.create(document.getElementById('container'), {
    value: "function hello() {\n\talert('Hello world!');\n}",
    language: 'javascript'
});

setTimeout(() => {
    monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
        target: monaco.languages.typescript.ScriptTarget.Latest
    });
}, 3000);

Have a look at the network tab and notice how after 3 seconds, workerMain.js and tsWorker.js are unnecessarily fetched again.

Observed in Monaco v0.32.1

alexdima commented 2 years ago

Currently, whenever the compiler options get changed the typescript worker gets stopped which eventually leads to it being recreated with the new options. For a well configured web server, this will not pose a problem, as it is standard practice to cache script resources client side (via e.g. a long lived Expires header). Even in the playground, I see that the new web worker uses the disk cache when being created:

Screen Shot 2022-02-18 at 14 55 13

We can leave this issue open as a feature request to update the TS compiler options without killing the entire TS worker, and with only recreating the language service on the worker side.