main.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import * as registerPushNotifications from 'mastodon/actions/push_notifications';
  4. import { setupBrowserNotifications } from 'mastodon/actions/notifications';
  5. import Mastodon, { store } from 'mastodon/containers/mastodon';
  6. import ready from 'mastodon/ready';
  7. const perf = require('./performance');
  8. function main() {
  9. perf.start('main()');
  10. if (window.history && history.replaceState) {
  11. const { pathname, search, hash } = window.location;
  12. const path = pathname + search + hash;
  13. if (!(/^\/web($|\/)/).test(path)) {
  14. history.replaceState(null, document.title, `/web${path}`);
  15. }
  16. }
  17. ready(() => {
  18. const mountNode = document.getElementById('mastodon');
  19. const props = JSON.parse(mountNode.getAttribute('data-props'));
  20. ReactDOM.render(<Mastodon {...props} />, mountNode);
  21. store.dispatch(setupBrowserNotifications());
  22. if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
  23. import('workbox-window')
  24. .then(({ Workbox }) => {
  25. const wb = new Workbox('/sw.js');
  26. return wb.register();
  27. })
  28. .then(() => {
  29. store.dispatch(registerPushNotifications.register());
  30. })
  31. .catch(err => {
  32. console.error(err);
  33. });
  34. }
  35. perf.stop('main()');
  36. });
  37. }
  38. export default main;