React + Vite + VitePWA 插件工作箱配置

问题描述 投票:0回答:1

我正在尝试缓存一些网址,以便让我的应用程序离线工作。

我正在使用 vite

5.0.5
vite-plugin-pwa
0.17.3

这是我的 vite.config.js

import react from '@vitejs/plugin-react';
import postcssNested from 'postcss-nested';
import { defineConfig } from 'vite';
import svgrPlugin from 'vite-plugin-svgr';
import { VitePWA } from 'vite-plugin-pwa';

export default defineConfig({
  build: {
    sourcemap: true, //@todo false in prod
  },
  plugins: [
    react(),
    svgrPlugin(),
    VitePWA({
      strategies: 'generateSW',
      registerType: 'autoUpdate',
      mode: 'development',
      workbox: {
        globPatterns: ['**/*.{js,jsx,css,html,png,svg,woff2,json}'],
        runtimeCaching: [
          {
            urlPattern: /\/api\//,
            handler: 'StaleWhileRevalidate',
            method: 'GET',
            options: {
              cacheName: 'api-cache',
              cacheableResponse: {
                statuses: [0, 200],
              },
            },
          },
        ],
      },
      devOptions: {
        enabled: true,
      },
      manifest: {
        name: 'AppName',
        short_name: 'AppName',
        description: 'App description',
        theme_color: '#ffffff',
        icons: [
          {
            src: '/manifestIcons/maskable_icon_x1024.png',
            sizes: '1024x1024',
            type: 'image/png',
            purpose: 'any maskable',
          },
        ],
      },
    }),
  ],
  css: {
    postcss: {
      plugins: [postcssNested],
    },
  },
  server: {
    port: 3000,
    open: true,
    host: true,
  },
});


但似乎不起作用,缓存中只有 1 个条目:

为什么我的“api-cache”不显示?

我有一个状态 200 请求,包括

/api/
:

我可以在 sw.js 中看到这一点:

reactjs progressive-web-apps vite workbox vite-plugin-pwa
1个回答
0
投票

对于遇到同样问题的人来说,它已经解决了这里

问题是“仅”

urlPattern: /regexp/
工作箱不会缓存 CORS 调用!

© www.soinside.com 2019 - 2024. All rights reserved.