I've this html page with a module ...
<html>
<body>
<hello-world color="blue" />
<hello-world color="red" />
<hello-world />
<script type="module">
import { HelloWorld } from './HelloWorld.js'
window.customElements.define('hello-world', HelloWorld)
</script>
</body>
</html>
... and the module content is ...
export class HelloWorld extends HTMLElement {
get color () {
return this.getAttribute('color') || 'gray'
}
set color (value) {
this.setAttribute('color', value)
}
connectedCallback() {
window.requestAnimationFrame(() => {
const div = document.createElement('div')
div.textContent = 'Hello World!!!'
div.style.color = this.color
this.appendChild(div)
});
}
}
I run a PHP server using php -S localhost:8888 -t .
and all works fine:
Instead, ... if I move module in ./main.mjs
file with content ...
import { HelloWorld } from './HelloWorld.js'
window.customElements.define('hello-world', HelloWorld)
... changing the html part in ...
<html>
<body>
<hello-world color="blue" />
<hello-world color="red" />
<hello-world />
<script type="module" src="main.mjs"></script>
</body>
</html>
... I get the following error:
Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
Why? May I fix it? How?