diff options
author | Yamagishi Kazutoshi <ykzts@desire.sh> | 2022-10-04 01:15:47 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-03 18:15:47 +0200 |
commit | 216dbaedaf587db834cfdd53b896e9c7e1284d9c (patch) | |
tree | 7b03a9e780675f0cd4a35795a0d32cabd663ec44 /app/javascript/mastodon/main.js | |
parent | 37eaa7fc01d8e9cbd06a62cc9ac4b3bd4d835240 (diff) |
Disable push notification when not logged in (#19272)
Diffstat (limited to 'app/javascript/mastodon/main.js')
-rw-r--r-- | app/javascript/mastodon/main.js | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/app/javascript/mastodon/main.js b/app/javascript/mastodon/main.js index a66975bfd..f33375b50 100644 --- a/app/javascript/mastodon/main.js +++ b/app/javascript/mastodon/main.js @@ -1,12 +1,14 @@ import React from 'react'; import ReactDOM from 'react-dom'; -import * as registerPushNotifications from 'mastodon/actions/push_notifications'; import { setupBrowserNotifications } from 'mastodon/actions/notifications'; import Mastodon, { store } from 'mastodon/containers/mastodon'; import ready from 'mastodon/ready'; -const perf = require('./performance'); +const perf = require('mastodon/performance'); +/** + * @returns {Promise<void>} + */ function main() { perf.start('main()'); @@ -18,7 +20,7 @@ function main() { } } - ready(() => { + return ready(async () => { const mountNode = document.getElementById('mastodon'); const props = JSON.parse(mountNode.getAttribute('data-props')); @@ -26,19 +28,28 @@ function main() { store.dispatch(setupBrowserNotifications()); if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { - import('workbox-window') - .then(({ Workbox }) => { - const wb = new Workbox('/sw.js'); - - return wb.register(); - }) - .then(() => { - store.dispatch(registerPushNotifications.register()); - }) - .catch(err => { - console.error(err); - }); + const [{ Workbox }, { me }] = await Promise.all([ + import('workbox-window'), + import('mastodon/initial_state'), + ]); + + const wb = new Workbox('/sw.js'); + + try { + await wb.register(); + } catch (err) { + console.error(err); + + return; + } + + if (me) { + const registerPushNotifications = await import('mastodon/actions/push_notifications'); + + store.dispatch(registerPushNotifications.register()); + } } + perf.stop('main()'); }); } |