错误码:
Access to script at 'chunks:///_virtual/xxx.ts' from origin 'https://xxx.com' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.
原因:
Cocos 3.8.x打包成web-mobile或者web-desktop时,网速不佳时,被引用文件所在的bundle模块仍未加载时,必然报错。
调试bug必现方法:
在Chrome Network在Disable cache及Slow 3G模式下,必然会报这个错误。
解决方案:
给这些代码开一个bundles,确保会成一个独立的子包(除了代码不要包含其他文件,比如预制体prefab)
因此代码量小,而且没有单独的逻辑去加载,所以并不影响实际运行。
暴力一点,也可以把代码直接扔到主包里面去。
此方案包体会略微变大。