main.js 1.2 KB

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