0

I'm writing a web application where, to avoid a big JS file to load full of code that is rarely used, I decided to modularize and load the modules once they are needed.

The loading works fine: Each piece of JS code is correctly loaded and executed; I used the jQuery method "append" to append a <script> at the end of the <head>.

You can see the result on the picture below.

All pieces of JS code correctly loaded into the browser!

But now the question came when I'd like to remove the code that I no longer need.

I know Facebook has already met the issue due to their big JS size, but on the speech (https://jmperezperez.com/facebook-frontend-javascript/) it seems that they acted as "optimization" of the JS code moving things from the client to the server.

Any idea or clue is strongly welcome.

Thank you very much for your answers.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Francesco Piraneo G.
  • 882
  • 3
  • 11
  • 25
  • What exactly are you trying to remove from where, and why? Remove from the loaded page: you cannot. Remove from the cache: let it expire by setting appropriate caching headers. Remove from your application: just remove the code that loads it. – Bergi Oct 10 '18 at 18:57
  • Remove from the loaded page, and from the cache. – Francesco Piraneo G. Oct 10 '18 at 19:28
  • As I said, the former [cannot](https://stackoverflow.com/questions/591685/how-can-i-dynamically-unload-a-javascript-file) [be done](https://stackoverflow.com/questions/4365062/how-to-unload-a-javascript-from-an-html). As for the latter: just let them age out. – Bergi Oct 10 '18 at 20:38
  • Why do you even think this is necessary? Do you have any actual issues? – Bergi Oct 10 '18 at 20:39
  • @Bergi to _prevent_ issues. There are html properties to set to age out scripts or what else? – Francesco Piraneo G. Oct 11 '18 at 07:12
  • A properly written module does not cause issues when it is no longer used, it doesn't need to be explicitly "unloaded" for that. (Which is impossible anyway). Regarding caching, that's a [HTTP feature](https://en.wikipedia.org/wiki/Web_cache#Cache_control), it's not controlled by html. – Bergi Oct 11 '18 at 07:25

0 Answers0