app.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * React Static Boilerplate
  3. * https://github.com/koistya/react-static-boilerplate
  4. * Copyright (c) Konstantin Tarkus (@koistya) | MIT license
  5. */
  6. import 'babel/polyfill';
  7. import React from 'react';
  8. import ReactDOM from 'react-dom';
  9. import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
  10. import Location from './core/Location';
  11. import Layout from './components/Layout';
  12. const routes = {}; // Auto-generated on build. See tools/lib/routes-loader.js
  13. const route = async (path, callback) => {
  14. const handler = routes[path] || routes['/404'];
  15. const component = await handler();
  16. await callback(<Layout>{React.createElement(component)}</Layout>);
  17. };
  18. function run() {
  19. const container = document.getElementById('app');
  20. Location.listen(location => {
  21. route(location.pathname, async (component) => ReactDOM.render(component, container, () => {
  22. // Track the page view event via Google Analytics
  23. window.ga('send', 'pageview');
  24. }));
  25. });
  26. }
  27. if (canUseDOM) {
  28. // Run the application when both DOM is ready and page content is loaded
  29. if (['complete', 'loaded', 'interactive'].includes(document.readyState) && document.body) {
  30. run();
  31. } else {
  32. document.addEventListener('DOMContentLoaded', run, false);
  33. }
  34. }
  35. export default { route, routes };