I am experiencing an issue with my web app on a webOS 5+ LG TV. When I try to load a module script using the <script type="module"> tag, I receive a “Failed to load module script” error in the browser console. The error message reads:
I have tried setting the MIME type explicitly for the module script by adding the type="text/javascript" attribute to the <script> tag, but this did not resolve the issue.
The web application is working perfectly on webOS 4.x model of LG TV.
I am using Svelte framework, Vite as my build tool, and I have also tried using the Vite polyfill plugin to add support for older browsers, but this did not resolve the issue either.
I believe this issue may be related to the way LG TVs with webOS 5+ handles module scripts or the way the TV’s browser handles MIME types, but I am not sure.
Steps to reproduce:
Load a web app on a webOS 5 LG TV.
Attempt to load a module script using the <script type="module"> tag.
Observe the “Failed to load module script” error in the browser console.
To use the script type=“module”, you must serve the html file through a server as follows due to JavaScript module security requirements: window.location="your-remote-server-url/index.html"
For more information, please refer to the guide here. Thank you.
I encountered the same problems as you. The solution was first using the vite polyfill and plugin-legacy plugin, and then going into the index.html (in dist folder). Once in the index.html, remove the script import for the one with “type=module”, and then go to the script imports which imports the polyfill and legacy scripts and remove the nomodule tag. This means it’s forced to import the legacy js and it should work. Kind of stupid, but I guess that encapsulates LG TV-app development in general.
where exactly should i put the window.location="" ? My react app that uses vite build also doesn’t work and when i remove the module from the it just got worse … So i would like to know what are my options
Have you found a workaround ? I am also building using Vite in my React app and i cannot run it in an emulator / TV. It does work on the simulator though when i run the ./dist directory directly, but when i try the .ipk build it just struggle with the Failed to load module script: The server responded with a non-JavaScript MIME type of "". …
If you target older browsers when building your Vite project, it will generate two type of scripts. Module scripts and legacy scripts. By default, browsers try to load module scripts so that’s the reason app throws “Failed to load module script”. To fix this issue, just try to comment module scripts from your dist/index.html and test it. It worked on me on Vite & Svelte project. I’m not sure about React, but config should be similar. I just automated that process by writing Vite plugin.
I think that you’re shipping module scripts in your .ipk build and in emulator it’s probably older version of chromium that doesn’t support module scripts. So make sure that you’re shipping legacy scripts
To use the <script type="module">, you must serve the html file through a server due to JavaScript module security requirements. Please try again with a hosted web app. Thank you.
Thank you, i tried to ship it via a hosted web app, but the emulator doesn’t have network connection so i cannot make a test. And i don’t have a LG TV at home to make sure everything work correctly…
I tried with a hosted app, but unfortunately it still doesn’t seem to accept a <script type="module">. When i inspect with the debugger i see that the .css files have loaded correctly but no sign of .js files being loaded…
I am desperate since i finished building the whole app and now i am testing on a LG physical device the app doesn’t run…
I tried to build with legacy scripts but some of my code (such as import.meta.env) doesn’t work and some syntax throw errors (Unexpected token "?" and more)
“Unexpected token ?..” comes from optional chaining. Make sure you’re building your project with proper polyfills and use older EcmaScript version for build target.