microfrontends-with-angular icon indicating copy to clipboard operation
microfrontends-with-angular copied to clipboard

Use fingerprinted file name for microfrontend entrypoint bundle to enable caching it

Open xkons opened this issue 2 years ago • 0 comments

This PR adds caching for the micro frontend bundles on top of https://github.com/fboeller/microfrontends-with-angular/pull/1

To make our our micro frontends cacheable, we adjust their build step to generate the main.js entrypoint file with a content based hash in the filename. This can easily be done using the Angular CLI. Then we just need to store the information of this filename in a file on the static storage (e.g. netlify) of the micro frontend so that we can load it. This new frontend-meta.json just weighs 1kB and will not be cached, so that it always contains the latest hashed entrypoint file name.

When loading a micro frontend, we now first load the frontend-meta.json and then the fingerprinted main.<hash>.js file. The latter will be cached by the browser. So until we deploy a new version of a micro frontend, the browser of our users will not have to download the roughly 500kB to open a page with that micro frontend anymore, but it is directly loaded from the users cache stored on their computer.

Important to know: The frontend-meta.json should never be cached by your clients to ensure that they always get the latest entrypoint bundle of your microfrontend.

This PR enables us to add a caching header to the response that fetches the entrypoint bundle of a microfrontend like.

Since the file name of the entrypoint bundle contains a md5 hash based on the content of the file, we know that if a bundle with the same name has been fetched by the browser, it is unchanged.

E.g. caching for one year: Cache-Control: max-age=31536000

The main.js file name we used before did not allow for caching, while still ensuring that all customers always get the latest micro frontend code.

xkons avatar Jan 12 '23 13:01 xkons