braze-inc / braze-web-sdk

Public repo for the Braze Web SDK
https://www.braze.com
Other
70 stars 25 forks source link

[Bug]: Service worker update failed #119

Closed debbyji17 closed 2 years ago

debbyji17 commented 2 years ago

Braze Web SDK Version

~4.0.2

Integration Method

CDN

Browser

Chrome

Steps To Reproduce

Here's our own service worker file, we added self.importScripts('https://js.appboycdn.com/web-sdk/4.0/service-worker.js'); at the bottom of the file.

/src/app/service-worker.js

... (our own service worker code)

self.importScripts('https://js.appboycdn.com/web-sdk/4.0/service-worker.js');

We use workbox-webpack-plugin this plugin to build our service worker file, the build file will generate sw.js at root.

webpack.config.js

plugins: {
     new InjectManifest({
        swSrc: './app/service-worker.js',
        swDest: '../sw.js',
      }),
}

Expected Behavior

When I push notification in Braze dashboard, it should be sent successfully, and test user should be receive the notifications.

Actual Incorrect Behavior

When I push notification in Braze dashboard, it shows error popup,

image

console throw the error message below, because we didn't have service-worker.js in our root. Our service worker file name is sw.js.

Braze: ServiceWorker update failed: SecurityError: Failed to update a ServiceWorker for scope ('http://localhost:3000/') with script ('http://localhost:3000/service-worker.js'): The script has an unsupported MIME type ('text/html').

We found that if we changed build file name from sw.js to service-worker.js, it can work and don't have any error message. But sw.js file can work on v3.x, and there is no error message. So we just wonder that why we couldn't use sw.js as our service worker build file name.

Verbose Logs

No response

Additional Information

No response

davidbielik commented 2 years ago

Hi @debbyji17 can you try using the serviceWorkerLocation: "/sw.js" initialization option? https://js.appboycdn.com/web-sdk/latest/doc/modules/braze.html#initializationoptions

debbyji17 commented 2 years ago

Hi @davidbielik Yes, it works! Thanks for your quickly reply. :) Is it a new change in v4? Because it works in v3 without serviceWorkerLocation options needed.

davidbielik commented 2 years ago

Hi @debbyji17 you're welcome!

No, this isn't a new change so you should have the same behavior on v2 or v3.