CORS errors when loading resources from config.json
Category
- [x] Question
- [ ] Typo
- [ ] Additional article idea
Question
I have a webpart which uses external library according to (https://docs.microsoft.com/en-us/sharepoint/dev/spfx/web-parts/basics/add-an-external-library#load-a-script-from-a-cdn). Everything works fine however I always see CORS errors when SP page tries to load this library. Also same errors are seen when SP page tries to load localizedResources from config.json.
Errors are like: "Access to script at 'https:///development/sharepoint/v1/assets/v1-commonstrings_en-us_cc83c7b1d8f44bf102f81e223fdf4b06.js' from origin 'https://*.sharepoint.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."
I also investigated that localizedResources and external libs are loaded twice by script "https://spoprod-a.akamaihd.net/files/sp-client/sp-pages-assembly_en-gb_187009d50ea1060e33206e49000cbf6e.js":
- first time (when it fails due to CORS) with header Sec-Fetch-Mode: cors
- second time (then successful) Sec-Fetch-Mode: no-cors
Environment details (development & target environment)
- Your Developer Environment: [Chrome, Edge browser]
- Target Environment: [SharePoint Online]
- Framework: [spfx 1.10]
- Browser(s): [Chrome, Edge]
- Tooling: [e.g. VS Code | SPFx v1.10.0]
Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.
A CORS issue is going to result from the resource you're requesting, not from SharePoint. Not sure if "https:///development/sharepoint" is a real endpoint or something you used as an example. Can you clarify where that is?
That server must respond with the HTTP header value Access-Control-Allow-Origin that includes the requesting domain.
I am using some standard CDN resources, like https://appsforoffice.microsoft.com/embedded/1.0/visio-web-embedded.js
I dont control them, so I cant change HTTP headers. However it seems strange to me that requesting JS file (which is configured via config.json, "externals" section) would need CORS support on a server.
Maybe you can help me to understand the internal mechanics? I thought that these URS from config.json are added to the page via