makelove

WebApp 的静态 index.html 是不是也不应该设置 CDN 缓存?

  •  
  •   makelove · Nov 12, 2022 · 1149 views
    This topic created in 1280 days ago, the information mentioned may be changed or developed.

    比如一般 js/css/img 文件可以设置无限时长缓存,因为引用的时候是带 hash 的。

    index.html 文件里引用的也是带 hash 的主 js 文件比如 index.xxoo.js 。

    那问题来了,如果设置缓存,那 CDN 万一给客户端旧的 index.html 文件,里面引用的是旧的 hash 的 js,这个文件早没了,是不是就出问题了?

    5 replies    2022-11-17 20:59:30 +08:00
    julyclyde
        1
    julyclyde  
       Nov 14, 2022
    正常浏览器会发 if modified since 请求的
    makelove
        2
    makelove  
    OP
       Nov 14, 2022
    @julyclyde 这个问题和浏览器发 if modified since 请求没有关系。是我给 index.html 设置一个 max-age,让 cdn 不要每次都去后端取
    forty
        3
    forty  
       Nov 17, 2022
    index.html 显然不适合无限期缓存, 缓存一周甚至一天都可以, 看你业务需要而定. 另外, CDN 也是可以人工清除和更新的, CDN 厂商一般会提供这个管理功能. index.html 寿命比 js 短, 就不会出现 index.html 还在 但 它的 js 早没了 的情况
    makelove
        4
    makelove  
    OP
       Nov 17, 2022
    @forty 问题是缓存几小时在发新版时也很大可能会给客户旧的 index 文件吧?
    这边是 WebApp,这个 index 文件非常小除了引用的那个 js 文件名有变化外长年不会变的。
    makelove
        5
    makelove  
    OP
       Nov 17, 2022
    话说我碰到这个情况后现在临时搞了个方法似乎有效果,弄了之后没有再发生这情况。

    就是在 nginx 里直接通配 index.xxxxx.js 形式的文件名
    location ~ ^\/assets\/index\.\w+\.js$ {
    try_files $uri /assets/index.js;
    }

    这个 index.js 在打包时直接复制的新生成的 index.xxxxx.js

    这样就兜底了所有的版本。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3034 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 06:45 · PVG 14:45 · LAX 23:45 · JFK 02:45
    ♥ Do have faith in what you're doing.