socket icon indicating copy to clipboard operation
socket copied to clipboard

[Bug] Vite dev server fails to load socket:* due to CORS

Open rmcdonald1442 opened this issue 2 years ago • 7 comments

What OS are you using (uname -a, or Windows version)?

Windows 11, amd64 (ryzen 7640s), not sure what IE version I'm using. Basically a fresh install of windows with all the Yes options selected for ssc.

What version Socket Runtime are you using?

PS C:\Users\Rory\coding> ssc --version 0.5.4 (f90ba121) Installation path: C:/Users/Rory/AppData/Roaming/npm/node_modules/@socketsupply/socket/node_modules/@socketsupply/socket-win32-x64\

What programming language are you using (C/C++/Go/Rust)?

node v20.10.0

What did you expect to see and what you saw instead?

I created a repo here with the issue: https://github.com/rorymcd98/socket-supply-vite-dev-bug/tree/main

This is mostly based off of this unnoficial guide https://gist.github.com/mecachisenros/12e3240a5d66cffa54089066f9ffe8ff using the recommended repo for external dev dependencies https://github.com/MilanKovacic/vite-plugin-externalize-dependencies

In regular build mode (ssc build) my app build correctly and I can see the default vite layout - this is what I expect for dev mode also.

Instead, when I run with ssc build -r --port=5173 I get the following cors message in the ssc runtime dev console:

init:1 
        
        
        Failed to load resource: net::ERR_FAILED
TypeError: Failed to fetch dynamically imported module: socket://internal/init
localhost/:1  Access to script at 'socket://os/' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
os/:1 
        
        
        Failed to load resource: net::ERR_FAILED```


rmcdonald1442 avatar Jan 04 '24 21:01 rmcdonald1442

I tried your repo on my macOS machine and it seems to work there. I'll try again later on my Windows machine and report back.

jwerle avatar Jan 05 '24 14:01 jwerle

@jwerle thanks I'll try again later on another windows intel machine.

rmcdonald1442 avatar Jan 05 '24 18:01 rmcdonald1442

More info. It doesn't work on my other windows 11 machine (identical error log I believe).

It does work on Linux Fedora 39.

rmcdonald1442 avatar Jan 05 '24 23:01 rmcdonald1442

@rorymcd98 okay! We'll dive in and figure this out

jwerle avatar Jan 05 '24 23:01 jwerle

Thanks - I'll work on Linux for now. Let me know if you need more info.

rmcdonald1442 avatar Jan 05 '24 23:01 rmcdonald1442

I think I may have found the issue... I get CORS errors when running from a regular command prompt, but if when starting from a Visual Studio Command Prompt the CORS errors are gone and the app loads.

Update:

This only fixes the issue with a production build. Starting the app in development (with vite) still has CORS errors even though vite is configured to allow any origin.

garth avatar Jan 10 '24 22:01 garth

I'm running into the same problem on Windows. It seems to work on Linux though.

Edit: I tried to investigate the differences between Linux and Windows. On Windows the request to "socket://internal/init" fails because the CORS headers are missing. On Linux there does not seem to be CORS check, so the resource is requested responded correctly.

So, the webviews on Windows and Linux seem to handle the request differently.

Edit 2: Running the app without the vite dev server ("ssc build -r") works. When using the context menu to reload the page one can the request to "socket://internal/init". In this case the request works and has response headers (Access-Control-Allow-Headers: * and Access-Control-Allow-Origin: *).

WolfBearGames avatar Feb 15 '24 10:02 WolfBearGames