简单的脚本尝试从我的 GitHub 页面root获取脚本,我认为我正确设置了 OneSignal。
怎么解决?
也许有一种方法可以为 OneSignalSDK.sw.js 设置“默认”路径?
错误:
[Service Worker Installation] Installing service worker failed TypeError: ServiceWorker script at
https://mandi-os.github.io/webdevelop.guide/OneSignalSDK.js?appId=777-777-777-777-777&sdkVersion=160204
for scope https://mandi-os.github.io/webdevelop.guide/ encountered an error during installation. [ServiceWorkerManager.js:294:12]
GET https://mandi-os.github.io/OneSignalSDK.sw.js? // <-- HERE
appId=777-777-777-777-777&sdkVersion=160204 [HTTP/2 404 9917ms]
Uncaught (in promise) Se: Registration of a Service Worker failed.
代码:
<!-- Push notifications service worker -->
<script src="https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js" defer></script>
<script>
window.OneSignalDeferred = window.OneSignalDeferred || [];
OneSignalDeferred.push(async function(OneSignal) {
await OneSignal.init({
appId: "777-777-777-777-777",
});
});
</script>
您可以在配置中使用 serviceWorkerParam 来设置不同的路径
<!-- Push notifications service worker -->
<script
src="https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js" defer>
</script>
<script>
window.OneSignalDeferred = window.OneSignalDeferred || [];
OneSignalDeferred.push(async function(OneSignal) {
await OneSignal.init({
appId: "777-777-777-777-777",
serviceWorkerParam: { scope: "/myPath/myCustomScope/" },
serviceWorkerPath: "myPath/OneSignalSDKWorker.js",
});
});
</script>