当前位置:首页 > VUE

vue实现强缓存

2026-02-18 06:07:51VUE

Vue 实现强缓存的方法

强缓存通过 HTTP 响应头 Cache-ControlExpires 控制,浏览器在缓存有效期内直接使用本地资源,不发送请求到服务器。Vue 项目可以通过以下方式实现强缓存。

配置 Webpack 输出文件名哈希

vue.config.js 或 Webpack 配置中,为输出文件添加内容哈希。当文件内容变化时,哈希值改变,强制浏览器重新加载资源。

module.exports = {
  configureWebpack: {
    output: {
      filename: '[name].[contenthash].js',
      chunkFilename: '[name].[contenthash].js'
    }
  }
}

设置 Nginx 强缓存规则

在 Nginx 配置中,为静态资源设置 Cache-Control 响应头。通常对哈希命名的文件设置长期缓存,对非哈希文件禁止缓存。

vue实现强缓存

location / {
  try_files $uri $uri/ /index.html;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
  expires 1y;
  add_header Cache-Control "public, no-transform, immutable";
}

使用 Service Worker 缓存策略

通过 workbox-webpack-plugin 或自定义 Service Worker 实现更灵活的缓存控制。在 vue.config.js 中配置:

const { InjectManifest } = require('workbox-webpack-plugin');

module.exports = {
  configureWebpack: {
    plugins: [
      new InjectManifest({
        swSrc: './src/service-worker.js',
        swDest: 'service-worker.js'
      })
    ]
  }
}

在 Service Worker 中定义缓存策略

创建 src/service-worker.js 文件,使用 Cache API 实现强缓存逻辑:

vue实现强缓存

const CACHE_NAME = 'my-app-cache-v1';
const urlsToCache = [
  '/',
  '/index.html',
  '/static/js/main.[contenthash].js',
  '/static/css/main.[contenthash].css'
];

self.addEventListener('install', event => {
  event.waitUntil(
    caches.open(CACHE_NAME)
      .then(cache => cache.addAll(urlsToCache))
  );
});

self.addEventListener('fetch', event => {
  event.respondWith(
    caches.match(event.request)
      .then(response => response || fetch(event.request))
  );
});

通过 HTTP 头控制缓存

确保服务器返回正确的 HTTP 响应头。对于哈希命名的静态资源,可设置长期缓存:

Cache-Control: public, max-age=31536000, immutable

对于 HTML 文件,通常禁用缓存或设置较短的有效期:

Cache-Control: no-cache, no-store, must-revalidate

使用 Vue CLI 的 PWA 插件

Vue CLI 的 @vue/cli-plugin-pwa 自动生成 Service Worker 并支持缓存策略。在 vue.config.js 中配置:

module.exports = {
  pwa: {
    workboxOptions: {
      runtimeCaching: [
        {
          urlPattern: /\.(?:js|css|woff|woff2)$/,
          handler: 'CacheFirst',
          options: {
            cacheName: 'static-resources',
            expiration: {
              maxEntries: 100,
              maxAgeSeconds: 60 * 60 * 24 * 365
            }
          }
        }
      ]
    }
  }
}

标签: 缓存vue
分享给朋友:

相关文章

vue实现token

vue实现token

Vue 中实现 Token 认证的方法 在 Vue 中实现 Token 认证通常涉及前端与后端的交互,包括 Token 的获取、存储、发送和刷新等操作。以下是具体实现步骤: 获取 Token 用户登…

vue实现fullpage

vue实现fullpage

Vue 实现 FullPage 效果 在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法: 使用现有库 vue-fullpage.js 安装 vue-ful…

vue如何实现

vue如何实现

Vue 实现方法 在 Vue 中实现功能通常涉及组件化开发、状态管理和响应式数据绑定。以下是几种常见的实现方式: 组件化开发 使用 Vue 的单文件组件(.vue 文件)结构,将 UI 拆分为可复用…

vue ref实现

vue ref实现

Vue 中 ref 的实现 在 Vue 中,ref 是一个用于创建响应式引用的 API,适用于基本类型和对象。以下是 ref 的核心实现原理和使用方法。 基本实现原理 ref 通过 Reactive…

vue 实现穿透

vue 实现穿透

Vue 样式穿透的实现方法 在 Vue 中,样式穿透通常指在带有 scoped 属性的样式块中,强制影响子组件的样式。以下是几种常见的实现方式: 使用 >>> 或 /deep/ 选…

vue实现折叠组件

vue实现折叠组件

Vue 折叠组件实现方法 基于 v-show 和事件触发 通过 v-show 控制内容的显示与隐藏,结合点击事件切换状态。 <template> <div> &…