vue-microfrontends / root-config

Start here if you're learning microfrontends
MIT License
251 stars 63 forks source link

how to control import-map cache? #3

Closed guguji5 closed 4 years ago

guguji5 commented 4 years ago

is there any cache strategy for this link ?

joeldenning commented 4 years ago

You can control caching of the import map and other files with a cache-control header in the server response. Here are the cache-control headers I usually set:

importmap.json: Cache-Control: public, must-revalidate, max-age=0. (Example) All other javascript files: Cache-Control: public, max-age=31557600

^ This means the user always has to redownload the import map (to get the latest deployment), but doesn't have to redownload the javascript files themselves.

Alternatively, you can cache importmap.json for a short period of time, which means it will be faster but deployments won't take effect for a while.

And the most performant alternative is actually to inline the import map into your HTML file. This means that you must have a dynamic web-server that can modify the HTML response that is sent to the browser. The dynamic web server polls the import map once every 30 seconds, and then injects it into the HTML that is sent to the browser. That way, the browser does not even have to request the import map.

One thing to note: if you are using import-map-deployer, it often will automatically set the Cache-Control header for the importmap.json file. See these examples (1, 2) which shows that it does so for some of the io-methods.

Let me know if you have other questions!

guguji5 commented 4 years ago

Got it, thank you for your patient and detailed answer.