Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created June 26, 2020 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velotiotech/d1b6ececcff1b72734269ccce23c0ac6 to your computer and use it in GitHub Desktop.
Save velotiotech/d1b6ececcff1b72734269ccce23c0ac6 to your computer and use it in GitHub Desktop.
Utility to render MicroFrontend
function renderMicroFrontend(pathname) {
const microFrontend = routes[pathname || window.location.hash];
const root = document.getElementById('microfrontend');
root.innerHTML = microFrontend ? new microFrontend().render(): new Home().render();
$(window).scrollTop(0);
}
$(window).bind( 'hashchange', function(e) { renderFrontend(window.location.hash); });
renderFrontend(window.location.hash);
utility routes.js (A map of the hash route to the Microfrontend class)
const routes = {
'#': Home,
'': Home,
'#home': Home,
'#cart': Cart,
'#checkout': Checkout,
'#product': Product,
'#contact': Contact,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment