Skip to content

Instantly share code, notes, and snippets.

@birendra-b
Created February 27, 2020 19:18
Show Gist options
  • Save birendra-b/3421b4505bad9aefab83b75e17e93207 to your computer and use it in GitHub Desktop.
Save birendra-b/3421b4505bad9aefab83b75e17e93207 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