Hkc

vuePress-theme-reco Hkc    2025
Hkc Hkc

Choose mode

  • dark
  • auto
  • light
TimeLine
GitHub
author-avatar

Hkc

25

Article

13

Tag

TimeLine
GitHub
  • Vue

  • Websocket

  • JS

  • CSS

  • Canvas

  • HTTP

  • GIT

  • SERVER

  • MORE

    • 强缓存和协商缓存
    • 前端单页面应用缓存策略

前端单页面应用缓存策略

vuePress-theme-reco Hkc    2025

前端单页面应用缓存策略

Hkc 2019-09-15 优化

在当前常用的单页面页面开发中。会将生产包发布为:固定名称的 html 文件和带 hash 的 js、css 文件。

所以我们需要保证 html 必须每次都实时请求服务器,但是 js 和 css 文件可以强缓存在本地,减少请求加速显示。

nginx 配置如下:

  • htm、html 文件配置协商缓存

  • js、css 文件配置强缓存

    location / {
    
        root html;
        index index.html index.htm;
    
        if ( $request_uri ~* /((.*)\.html?)?$ ) {
            add_header Cache-Control no-cache;
        }
        if ( $request_uri ~* /.*\.(js|css)$ ) {
            add_header Cache-Control max-age=86400;
        }
    
    }