首页 > 解决方案 > 如何在 workbox-config.js 中为 POST 操作配置 runtimeCaching

问题描述

我正在工作箱中为“POST”操作进行后台队列配置。请指导我在 workbox-config.js 中的“runtimeCaching”配置中为“POST”操作提供选项的位置

module.exports = {
  "globDirectory": "dist/",
  "globPatterns": [
    "**/*.{txt,ico,html,js,css}"
  ],
  "swDest": "dist\\sw.js",
  runtimeCaching: [{
    urlPattern: /api/,
    handler: 'NetworkOnly',
    options: {
      // Configure background sync.
      backgroundSync: {
        name: 'product-bgsync-queue1',
        options: {
          maxRetentionTime: 24 * 60 * 60,
        },
      },
    },
  }]
};

上面的代码在 dist/sw.js 中为“GET”生成默认配置。

workbox.routing.registerRoute(/api/,
new workbox.strategies.NetworkOnly({  
   plugins:[  
      new workbox.backgroundSync.Plugin("product-bgsync-queue1",
      {  
         maxRetentionTime:86400
      }      )
   ]
}),
'GET');

请指导如何为“POST”操作生成相同的配置。

标签: workboxbackground-sync

解决方案


添加method: 'POST'应该会给你你想要的行为:

runtimeCaching: [{
  urlPattern: /api/,
  handler: 'NetworkOnly',
  method: 'POST',
  options: {
    // Configure background sync.
    backgroundSync: {
      name: 'product-bgsync-queue1',
      options: {
        maxRetentionTime: 24 * 60 * 60,
      },
    },
  },
}]

推荐阅读