dqayok7935 2018-06-14 11:33
浏览 409
已采纳

Javascript文件不缓存

Im using the Keenthemes Metronic admin template and having real trouble with 1 file. Its a javascript file and for some reason no matter what i try i cannot get it to cache. Its a static file with a size of 3.5mb so loading this each time is killing the scripts.

I have tried adding bits to me htaccess and to the header but this changes nothing.

Has anyone else come across this?

Thanks

  • 写回答

1条回答 默认 最新

  • duanpo1498 2018-06-14 12:22
    关注

    As I could see from the comment section, you have already tried minimising the file, but it is still very large. You can control the cache of your own browser, but cannot control the cache of the browsers your users are using. Therefore you will not be able to enforce caching the file. You will therefore need to thoroughly read the file and divide it into separate files. You will end up having a core file, which will be useful everywhere and which will be needed to be downloaded and you will have some other files, which are specific to some features, like a file for register/login, another for handling separate features, like choosing colors and so on.

    You will need to load your core JS file everywhere, but your specific features will be needed at specific places. You will not need login features, for instance, if the user has already logged in, so you will be able to include the JS files for separate features where they are needed and nowhere else.

    Also, You might want to lazy load your JS files, so you will initially load the core file and when it has been successfully loaded, separately load the other files. Those features will be unable initially when the page is loaded, so the page will need to somehow handle or prevent attempts from users of using a feature before its script was loaded.

    It would not hurt to minify all these separate files either. Possibly RequireJS can help you to handle requirements, but you can implement your own feature to handle requirements as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?