Created
April 21, 2021 11:13
-
-
Save puttpotsawee/508dcb766c89d760bbbe461319efa541 to your computer and use it in GitHub Desktop.
[MicroFrontend Workshop 1] App Component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* app.js | |
* | |
* This is the entry file for the application, only setup and boilerplate | |
* code. | |
*/ | |
// Needed for redux-saga es6 generator support | |
import '@babel/polyfill'; | |
// Import all the third party stuff | |
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import { Provider } from 'react-redux'; | |
import FontFaceObserver from 'fontfaceobserver'; | |
import defaultHistory from 'utils/history'; | |
import 'sanitize.css/sanitize.css'; | |
// Import root app | |
import App from 'containers/App'; | |
// Load the favicon and the .htaccess file | |
import '!file-loader?name=[name].[ext]!./images/favicon.ico'; | |
import 'file-loader?name=.htaccess!./.htaccess'; // eslint-disable-line import/extensions | |
import configureStore from './configureStore'; | |
// Observe loading of Open Sans (to remove open sans, remove the <link> tag in | |
// the index.html file and this observer) | |
const openSansObserver = new FontFaceObserver('Open Sans', {}); | |
// When Open Sans is loaded, add a font-family using Open Sans to the body | |
openSansObserver.load().then(() => { | |
document.body.classList.add('fontLoaded'); | |
}); | |
// Create redux store with history | |
const initialState = {}; | |
// const store = configureStore(initialState, defaultHistory); | |
const NAME = 'Dogs'; | |
const render = (containerId, history) => { | |
const store = configureStore(initialState, history); | |
const MOUNT_NODE = document.getElementById(containerId); | |
ReactDOM.render( | |
<Provider store={store}> | |
<App history={history} /> | |
</Provider>, | |
MOUNT_NODE, | |
); | |
}; | |
const unmount = containerId => { | |
const UNMOUNT_NODE = document.getElementById(containerId); | |
ReactDOM.unmountComponentAtNode(UNMOUNT_NODE); | |
}; | |
window[`render${NAME}`] = render; | |
window[`unmount${NAME}`] = unmount; | |
if (!document.getElementById(`${NAME}-container`)) { | |
if (module.hot) { | |
// Hot reloadable React components and translation json files | |
// modules.hot.accept does not accept dynamic dependencies, | |
// have to be constants at compile-time | |
module.hot.accept(['containers/App'], () => { | |
unmount('app'); | |
render('app', defaultHistory); | |
}); | |
} | |
render('app', defaultHistory); | |
} | |
// Install ServiceWorker and AppCache in the end since | |
// it's not most important operation and if main code fails, | |
// we do not want it installed | |
if (process.env.NODE_ENV === 'production') { | |
require('offline-plugin/runtime').install(); // eslint-disable-line global-require | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment