OneSignal / OneSignal-Website-SDK

OneSignal is a push notification service for web and mobile apps. This SDK makes it easy to integrate your website with OneSignal Push Notifications. https://onesignal.com
Other
389 stars 115 forks source link

[Bug]: Filed Service Worker Installation (SDK v160202) #1191

Closed aleksi-magner closed 2 months ago

aleksi-magner commented 2 months ago

What happened?

I can't get the Service Worker to initialize. I tried placing it both in the project root and in a separate directory. Version 15 works fine. Help me figure out what the problem might be.

At the same time, the OneSignal instance itself is seen as class nn. But it is not possible to register SW.

What browsers are you seeing the problem on?

Firefox, Chrome (Chromium)

What operating system are you running?

Linux, alpine stable version (docker image)

Steps to reproduce?

Uses NodeJS LTS, currently v20.17.0

SW file in the project root
1. Install devDependencies vite v5.4.3
2. Add file OneSignalSDKWorker.js to root directory
3. Build app `vite build`

SW file in a separate project directory
1. Install devDependencies vite v5.4.3
2. Add file OneSignalSDKWorker.js to /push/ directory
3. Change settings OneSignal in dashboard
[v] Service Workers block
  Path to service worker files: /push/
  Main service worker filename: OneSignalSDKWorker.js
  Updater service worker filename: OneSignalSDKWorker.js
  Service worker registration scope: /push/

4. Build app `vite build`

Contents of the OneSignalSDKWorker.js file
`importScripts('https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.sw.js');`

Contents of the index.html file
`<head>
  ...
  <script defer src="https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js"></script>

  <script>
    window.OneSignalDeferred = window.OneSignalDeferred || [];

    window.OneSignalDeferred.push(async function(instance) {
      await instance.init({
        appId: <appKey>,
      });
    });
  </script>
</head>`

What did you expect to happen?

Successful SW registration

Relevant log output

// Firefox
[Service Worker Installation] Installing service worker failed TypeError: ServiceWorker script at https://shifts-lapina.verme.ru/OneSignalSDKWorker.js?appId=eb3cddd0-1737-4262-b994-c4beda0a30f0&sdkVersion=160202 for scope https://shifts-lapina.verme.ru/ encountered an error during installation. ServiceWorkerManager.js:279:12
Uncaught (in promise) DOMException: The operation is insecure.

// Chrome
An unknown error occurred when fetching the script.
ServiceWorkerManager.js:279 [Service Worker Installation] Installing service worker failed TypeError: Failed to register a ServiceWorker for scope ('https://shifts-lapina.verme.ru/') with script ('https://shifts-lapina.verme.ru/OneSignalSDKWorker.js?appId=eb3cddd0-1737-4262-b994-c4beda0a30f0&sdkVersion=160202'): An unknown error occurred when fetching the script.
(анонимная) @ ServiceWorkerManager.js:279
a @ tslib.es6.js:119
Promise.then
l @ tslib.es6.js:120
r @ tslib.es6.js:118
Promise.then
l @ tslib.es6.js:120
r @ tslib.es6.js:118
Promise.then
l @ tslib.es6.js:120
(анонимная) @ tslib.es6.js:121
o @ tslib.es6.js:117
installWorker @ OneSignalSDK.page.es6.js?v=160202:1
(анонимная) @ InitHelper.js:20
(анонимная) @ tslib.es6.js:121
o @ tslib.es6.js:117
internalInit @ OneSignalSDK.page.es6.js?v=160202:1
(анонимная) @ OneSignal.js:141
r @ tslib.es6.js:118
Promise.then
l @ tslib.es6.js:120
r @ tslib.es6.js:118
Promise.then
l @ tslib.es6.js:120
(анонимная) @ tslib.es6.js:121
o @ tslib.es6.js:117
e @ OneSignalSDK.page.es6.js?v=160202:1
(анонимная) @ OneSignal.js:145
(анонимная) @ tslib.es6.js:121
o @ tslib.es6.js:117
_delayedInit @ OneSignalSDK.page.es6.js?v=160202:1
(анонимная) @ OneSignal.js:121
r @ tslib.es6.js:118
Promise.then
l @ tslib.es6.js:120
r @ tslib.es6.js:118
Promise.then
l @ tslib.es6.js:120
r @ tslib.es6.js:118
Promise.then
l @ tslib.es6.js:120
(анонимная) @ tslib.es6.js:121
o @ tslib.es6.js:117
init @ OneSignalSDK.page.es6.js?v=160202:1
(анонимная) @ (индекс):49
processItem @ ProcessOneSignalPushCalls.js:5
push @ OneSignal.js:194
processOneSignalDeferredArray @ ReplayCallsOnOneSignal.js:8
(анонимная) @ pageSdkInit.js:29
(анонимная) @ pageSdkInit.js:31
(анонимная) @ OneSignalSDK.page.es6.js?v=160202:1
(анонимная) @ OneSignalSDK.page.es6.js?v=160202:1
Показать ещё 26 фреймов
Свернуть
The script has an unsupported MIME type ('text/html').
(индекс):55 Uncaught (in promise) SecurityError: Failed to register a ServiceWorker for scope ('https://shifts-lapina.verme.ru/') with script ('https://shifts-lapina.verme.ru/OneSignalSDK.sw.js?appId=eb3cddd0-1737-4262-b994-c4beda0a30f0&sdkVersion=160202'): The script has an unsupported MIME type ('text/html').
jkasten2 commented 2 months ago

@aleksi-magner It looks like your site is currently configured to go to https://shifts-lapina.verme.ru/push/OneSignalSDKWorker.js?appId=eb3cddd0-1737-4262-b994-c4beda0a30f0&sdkVersion=160202 however it is not returning JavaScript and is instead returning some kind of HTML page. This is called out with the error "The script has an unsupported MIME type ('text/html')". in the log.

Make sure your server is correctly hosting the .js file.

aleksi-magner commented 2 months ago

@jkasten2 Yes, absolutely. If you open files using separate links in a new tab, everything will load. Moreover, if you connect version 15 in the same files, in the same places, it works correctly.

изображение

изображение

Firstly, all errors point to the OneSignal file being used - ServiceWorkerManager.js:279:12. Secondly, Firefox also shows the error "operation insecure"

jkasten2 commented 2 months ago

@aleksi-magner not sure which page in your site your are testing your browser with but it seems the difference might be between push/OneSignalSDKWorker.js vs OneSignalSDKWorker.js being accesses intead based on your OneSignal settings.

aleksi-magner commented 2 months ago

@jkasten2 I test both here and there. Initially it was necessary to replace SW in the /push/ directory. When nothing worked, I tried adding the file to the project root

изображение

изображение

изображение

It's as if the script is trying to register index.html, where the OneSignal SDK is initialized, instead of registering SW from the /push/ directory

aleksi-magner commented 2 months ago

It’s as if where there is an error about the mime type ('text/html') in the script location search variable, the directory from the settings is missing. Looks for the path server.com/SDK.sw.js?appId=... instead of server.com/push/SDK.sw.js?appId=...

jkasten2 commented 2 months ago

@aleksi-magner what status do you seen in the network tab? It is showing the service worker registering for me on the network tab: image

aleksi-magner commented 2 months ago

@jkasten2 First there was status 200, but content type html

изображение

Now, after many days of attempts, the worker has finally registered. Did they change anything in the SW registration? This is a test circuit, I’m worried that this is an accident and there will be problems in production

image

jkasten2 commented 2 months ago

The file is hosted on your server, my best guess would be either caching on you server or the browser, but it could be something else on your server setup.

aleksi-magner commented 2 months ago

It seems like I’ve already double-checked everything, all the caches, and even tried it in new browsers. Since the SW connection was ultimately successful, then I close the current problem. Thank you for your assistance