Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hatsilau_Microfrontends_MITCon_2019

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.
Avatar for Maksim Maksim
April 29, 2019

 Hatsilau_Microfrontends_MITCon_2019

Avatar for Maksim

Maksim

April 29, 2019

More Decks by Maksim

Other Decks in Technology

Transcript

  1. 2 • 5 years of experience in Software Development •

    Over 3 years at EPAM • Focused on frontend development MIKHAIL HATSILAU
  2. 7 Monolithic frontend application Pros ▪ Simpler configuration ▪ Simpler

    infrastructure setup ▪ Simpler and faster development Cons ▪ Not reusable ▪ Hard to scale ▪ Hard to maintain ▪ Invalid segment can negatively influence the stability of work of the entire app ▪ Hard to separate teams
  3. 15 entry.js file structure export const reducers = [] //

    micro app reducers export const routes = ( <Fragment> <Route>...</Route> </Fragment> )
  4. 17 Micro app webpack output config output: { filename: '[name].[hash].js’,

    path: 'dist’, library: '__systemJsonpLoader__’, libraryTarget: 'jsonp' },
  5. 19 Chunks [ { id: 'dashboard’, config: { mountPath: '/dashboard’,

    exact: true }, script: 'dashboard/dashboard.123abc.js’ } ]
  6. 21 Core app routes <Switch> {window.__INITIAL_STATE__.chunks.map((chunk) => ( <Route> key={chunk.config.mountPath}

    path={chunk.config.mountPath} exact={chunk.config.exact} render={() => ( <LazyComponent chunk={chunk} /> )} </Route> ))} </Switch>
  7. 22 LazyComponent implementation const useLazy = (chunk) => { const

    [stateChunk, setChunk] = useState(null); useEffect(async () => { const loadedChunk = await loadChunk( chunk.id, chunk.script ); setChunk(loadedChunk); replaceReducers(loadedChunk.reducers) // store.replaceReducers }, []); return stateChunk; };
  8. 23 loadComponent implementation const loadChunk = (chunkId, scriptName) => (

    new Promise((resolve) => { const script = document.createElement('script’); script.onload = () => { resolve(loadedChunks[chunkId]); }; script.src = scriptName; document.head.appendChild(element); }) );
  9. 27 Microfrontend vs Monolithic application Pros ▪ Easy to scale

    ▪ Easy to maintain ▪ Partial deployment ▪ Can be developed by separate teams w/o hard dependencies ▪ No need to rebuild the whole app when a micro app is changed Cons ▪ Hard to develop ▪ Slower development ▪ Complicated deployment process and configuration ▪ Hard to start the whole application locally
  10. 30 Lazy loading chunk with the monorepo approach const useLazy

    = (chunk) => { const [stateChunk, setChunk] = useState(null); useEffect(async () => { const loadedChunk = await loadChunk(chunk.id); setChunk(loadedChunk); replaceReducers(loadedChunk.reducers) // store.replaceReducers }, []); return stateChunk; };
  11. 31 Load chunk with the monorepo approach const loadChunk =

    async (chunkId) => { const chunk = await import(`@app/${chunkId}`); return chunk.default; }
  12. 32 Monorepo app with lerna Pros ▪ Easy to scale

    ▪ Easy to develop ▪ Faster development ▪ Easy to maintain ▪ Can be developed by separate teams w/o hard dependencies Cons ▪ Repository size ▪ Individual parts can’t be deployed w/o rebuilding the whole app