当前位置:首页 > 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实现滑动输入条

用vue实现滑动输入条

使用 Vue 实现滑动输入条 使用原生 HTML5 input range 实现 在 Vue 模板中直接使用 <input type="range">,通过 v-model 绑定数据:…

vue 实现滚动

vue 实现滚动

实现滚动的基本方法 在Vue中实现滚动效果可以通过多种方式完成,包括使用原生JavaScript、CSS或第三方库。以下是一些常见的方法: 使用window.scrollTo方法实现页面滚动 可以通…

vue实现suspense

vue实现suspense

Vue 3 中实现 Suspense Suspense 是 Vue 3 引入的一个内置组件,用于处理异步依赖的加载状态。它允许在等待异步组件或异步数据加载时显示一个后备内容(fallback),直到异…

vue实现粘贴

vue实现粘贴

Vue 实现粘贴功能的方法 在 Vue 中实现粘贴功能通常涉及监听粘贴事件并处理剪贴板数据。以下是几种常见的实现方式: 监听原生粘贴事件 通过 @paste 指令或原生 addEventListe…

vue实现driver

vue实现driver

Vue 实现 Driver.js 引导功能 Driver.js 是一个轻量级的 JavaScript 库,用于在网页上创建引导式导览。以下是在 Vue 项目中集成 Driver.js 的详细方法:…

利用vue 实现

利用vue 实现

以下是利用 Vue 实现常见功能的几种方法,分为不同场景和技术要点: 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现双向绑定: <template>…