dtn43447 2015-03-20 08:00
浏览 112

如何为javascript / css文件加载0 ms?

I noticed that some files have 0 ms load time, and chrome writes that it is from cache.

You can see in the picture (stack overflow shows it bit small, but just zoom in or open image url in another tab):

Developer tools shot

And status column is interesting - those with 304 Not modified are having some load time. But at the bottom - admin.js and some other font file - in Size/Content column we see that are are loaded from cache and takes 0 ms.

So why could other files not be loaded from cache if they are not modified?

And is there a simple solution to load all from cache? And when I will want to reload, I will change the file name as I have done to first file - by adding timestamp.

  • 写回答

1条回答 默认 最新

  • douqi3064 2015-03-20 17:21
    关注

    Every time you navigate to other pages or reload one of them*, the browser must check if the resource has been changed or not in the server. If the resource is the same, the server doesn't have to send it again and sends a 304 status code (Not Modified); but if the resource has changed, then it sends the new resource

    By default, nginx doesn't send to the browser the Expires HTTP header, which tells the browser that the resource will not change over some period of time (giving him the ability to cache that resource and not asking for it over that period).

    So if you know you have some resource that are unlikely to change, you can configure nginx to send the Expires header. For example, if you want the user's browser to cache all images, CSS and JS on your site for one month, you can add the following to the nginx server configuration:

    location ~* \.(?:css|js|png|gif|jpe?g|ico|svg)$ {
        expires 30d;
    }
    

    If you update any of those files, browsers won't download the new version until the expires period is over. To avoid that and bypass browser's cache you can add a timestamp (or any other parameter) to your files. You can append to the URI the timestamp after a ?, so you don't have to change the internal name of that file on the server.

    *Note: When you reload a page, browsers will check if the resources have changed, even if a Expires header was sent when they were downloaded.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM