Created
April 30, 2021 12:25
-
-
Save puttpotsawee/8113c8e30f0be00191483a90c2144514 to your computer and use it in GitHub Desktop.
Micro Frontend Runtime Integration via JavaScript
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
<html> | |
<head> | |
<title>Feed me!</title> | |
</head> | |
<body> | |
<h1>Welcome to Feed me!</h1> | |
<!-- These scripts don't render anything immediately --> | |
<!-- Instead they attach entry-point functions to `window` --> | |
<script src="https://browse.example.com/bundle.js"></script> | |
<script src="https://order.example.com/bundle.js"></script> | |
<script src="https://profile.example.com/bundle.js"></script> | |
<div id="micro-frontend-root"></div> | |
<script type="text/javascript"> | |
// These global functions are attached to window by the above scripts | |
const microFrontendsByRoute = { | |
'/': window.renderBrowseRestaurants, | |
'/order-food': window.renderOrderFood, | |
'/user-profile': window.renderUserProfile, | |
}; | |
const renderFunction = microFrontendsByRoute[window.location.pathname]; | |
// Having determined the entry-point function, we now call it, | |
// giving it the ID of the element where it should render itself | |
renderFunction('micro-frontend-root'); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment