main.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { setupBrowserNotifications } from 'mastodon/actions/notifications';
  4. import Mastodon, { store } from 'mastodon/containers/mastodon';
  5. import { me } from 'mastodon/initial_state';
  6. import ready from 'mastodon/ready';
  7. const perf = require('mastodon/performance');
  8. /**
  9. * @returns {Promise<void>}
  10. */
  11. function main() {
  12. perf.start('main()');
  13. return ready(async () => {
  14. const mountNode = document.getElementById('mastodon');
  15. const props = JSON.parse(mountNode.getAttribute('data-props'));
  16. ReactDOM.render(<Mastodon {...props} />, mountNode);
  17. store.dispatch(setupBrowserNotifications());
  18. if (process.env.NODE_ENV === 'production' && me && 'serviceWorker' in navigator) {
  19. const { Workbox } = await import('workbox-window');
  20. const wb = new Workbox('/sw.js');
  21. /** @type {ServiceWorkerRegistration} */
  22. let registration;
  23. try {
  24. registration = await wb.register();
  25. } catch (err) {
  26. console.error(err);
  27. }
  28. if (registration) {
  29. const registerPushNotifications = await import('mastodon/actions/push_notifications');
  30. store.dispatch(registerPushNotifications.register());
  31. }
  32. }
  33. perf.stop('main()');
  34. });
  35. }
  36. export default main;