Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.x] Web Workers return 404 #112

Closed
DarkGhostHunter opened this issue Aug 1, 2022 · 5 comments
Closed

[0.x] Web Workers return 404 #112

DarkGhostHunter opened this issue Aug 1, 2022 · 5 comments

Comments

@DarkGhostHunter
Copy link

DarkGhostHunter commented Aug 1, 2022

  • Laravel Vite Plugin Version: 0.5.2
  • Laravel Version: 9.22.1
  • Node Version: 17.2.0
  • NPM Version: 8.15.1
  • Host operating system: Windows 10 / Ubuntu 22.04
  • Web Browser & Version: Chrome 105.0.5195.10
  • Running in Sail / Docker: No

Description:

Using monaco-editor, which requires Web Workers to be registered to enable functionality, using it as instructed, the Web Workers are requested with invalid URLs.

image

image

Note that the workers URL is generated incorrectly by Vite by appending / to the worker URL:

image

Details in this Stack Overflow question.

Steps To Reproduce:

npm install monaco-editor
<script setup>
import {onMounted, onUnmounted, ref, defineEmits} from "vue"
import * as monaco from "monaco-editor";
import editorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker";
import jsonWorker from "monaco-editor/esm/vs/language/json/json.worker?worker";
import cssWorker from "monaco-editor/esm/vs/language/css/css.worker?worker";
import htmlWorker from "monaco-editor/esm/vs/language/html/html.worker?worker";
import tsWorker from "monaco-editor/esm/vs/language/typescript/ts.worker?worker";

self.MonacoEnvironment = {
    getWorker(_, label) {
        switch (label) {
            case 'json':
                return new jsonWorker();
            case 'css':
            case 'scss':
            case 'less':
                return new cssWorker();
            case 'html':
            case 'handlebars':
            case 'razor':
                return new htmlWorker();
            case 'typescript':
            case 'javascript':
                return new tsWorker();
            case 'yaml':
                return new yamlWorker();
            default:
                return new editorWorker()
        }
    }
};

monaco.languages.typescript.typescriptDefaults.setEagerModelSync(true);

const props = defineProps({
    modelValue: {
        type: String,
        default: '{"foo":"bar"}'
    },
});

const emit = defineEmits(['update:modelValue']);

const container = ref()

let instance = null

onMounted(() => {
    instance = monaco.editor.create(container.value, {
        model: monaco.editor.createModel(props.modelValue, 'json'),
        tabSize: 2,
    })

    instance.onDidChangeModelContent(() => {
        emit('update:modelValue', instance.getValue());
    });
})

onUnmounted(() => {
    instance?.dispose()
})
</script>

<template>
    <div ref="container"></div>
</template>
@jessarcher
Copy link
Member

Hi @DarkGhostHunter, this looks like the same issue as #106.

I believe this is an upstream issue with Vite, but I'd be curious to know whether the problem exists without this plugin, by manually configuring Vite following https://vitejs.dev/guide/backend-integration.html

@DarkGhostHunter
Copy link
Author

It's somewhat the same. My goal was to include monaco-yaml as I need a YAML Editor plus Schema verification.

I'll try the manual backend config.

@driesvints
Copy link
Member

Please see the other issue, thanks.

@DarkGhostHunter
Copy link
Author

DarkGhostHunter commented Aug 4, 2022

Hi @DarkGhostHunter, this looks like the same issue as #106.

I believe this is an upstream issue with Vite, but I'd be curious to know whether the problem exists without this plugin, by manually configuring Vite following https://vitejs.dev/guide/backend-integration.html

I was able to replicate the problem, and it seems that when using a backend instead of using Vite as a proxy won't allow web workers because what you said earlier: the browser doesn't allow web worker to run from different ports, even in the same domain.

In other words, to this to successfully work, the only workaround is to access the server through Vite:

[Vite Proxy] -> [PHP Web Server]

I elevated this to Vite.

@P-u-r-i-a
Copy link

There is a workaround for this issue: vitejs/vite#13680 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants