Skip to content

Commit

Permalink
add example of a root service worker
Browse files Browse the repository at this point in the history
This is to test OneSignal with conflicting a root service worker. This
causes the OneSignal SDK to sometimes not include a push token
when it makes a REST API to POST /users. This is due to the OneSignal
SDK sometimes getting the registration for the root service worker due
to a race condition, instead of the OneSignal service worker.

We will fix this problem in a follow up commit.
  • Loading branch information
jkasten2 committed Nov 15, 2023
1 parent 9fb9870 commit 5af7365
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions express_webpack/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
<!-- NOTE: This does not work with the relative pathed manifest.json above. -->
<!--<base href="https://www.example.com/"> -->
<script src="sdks/web/v16/Dev-OneSignalSDK.page.js" defer></script>

<script>
// Example of a root service worker a site may have.
// This is common for a PWA.
navigator.serviceWorker.register('sw.js');
</script>

<script>
// NOTE: Uncomment and open site in Safari on macOS 13+ to simulate
// a similar JS API as an iOS 16.4 WebApp.
Expand Down
4 changes: 4 additions & 0 deletions express_webpack/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ app.get('/:file', (req, res) => {
res.sendFile(sanitize(req.params.file), { root: __dirname });
});

app.get('/push/onesignal/:file', (req, res) => {
res.sendFile(path.join(DIST_DIR, '/push/onesignal/') + sanitize(req.params.file));
});

https.createServer(options, app).listen(4001, () => console.log("express_webpack: listening on port 4001 (https)"));

// http
Expand Down
19 changes: 19 additions & 0 deletions express_webpack/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Example of a root scope service worker a site might have.
// This tests to ensure OneSignal can use the correctly scoped one when
// 2 service workers are in play.

// This exists to point out the fact this service worker is being
// used instead of the intended OneSignalSDKWorker.js.
self.addEventListener('push', async (e) => {
console.error("push - Should not fire on sw.js");

const options = {
body: 'Non-SDK from sw.js',
};

// Display notification
const displayPromise = self.registration.showNotification('Non-SDK from sw.js', options);

// Must wait for all promises to finish before return from this event.
e.waitUntil(displayPromise);
});

0 comments on commit 5af7365

Please sign in to comment.