Angular 5 和 Service Worker : How to exclude a particular path from ngsw-config. json

标签 angular service-worker angular-service-worker

我有 ngsw-config.json(取自 docs ):

    {
      "index": "/index.html",
      "assetGroups": [{
        "name": "app",
        "installMode": "prefetch",
        "resources": {
          "files": [
            "/favicon.ico",
            "/index.html"
          ],
          "versionedFiles": [
            "/*.bundle.css",
            "/*.bundle.js",
            "/*.chunk.js"
          ]
        }
      }, {
        "name": "assets",
        "installMode": "lazy",
        "updateMode": "prefetch",
        "resources": {
          "files": [
            "/assets/**"
          ]
        }
      }]
    }

在我的网站上有一个指向 RSS 提要 /api/rss 的链接,该链接应该在新的浏览器选项卡中打开,而无需加载 Angular 应用程序。如何将其从请求重定向到 index.html 的资源列表中排除?

UPD:我试过但没有使用以下配置(参见 !/api/rss):

    {
      "index": "/index.html",
      "assetGroups": [{
        "name": "app",
        "installMode": "prefetch",
        "patterns": ["!/api/rss"],
        "resources": {
          "files": [
            "/favicon.ico",
            "/index.html",
            "!/api/rss"
          ],
          "versionedFiles": [
            "/*.bundle.css",
            "/*.bundle.js",
            "/*.chunk.js"
          ]
        }
      }, {
        "name": "assets",
        "installMode": "lazy",
        "updateMode": "prefetch",
        "resources": {
          "files": [
            "/assets/**"
          ]
        }
      }]
    }

最佳答案

感谢 Pedro Arantes advice ,我到达了下一个工作配置(参见 dataGroups"maxAge": "0u"):

{
  "index": "/index.html",
  "dataGroups":
  [
    {
      "name": "api",
      "urls": ["/api"],
      "cacheConfig": {
        "maxSize": 0,
        "maxAge": "0u",
        "strategy": "freshness"
      }
    }
  ],
  "assetGroups":
  [
    {
      "name": "app",
      "installMode": "prefetch",
      "resources": {
        "files": [
          "/favicon.ico",
          "/index.html"
        ],
        "versionedFiles": [
          "/*.bundle.css",
          "/*.bundle.js",
          "/*.chunk.js"
        ]
      }
    },
    {
      "name": "assets",
      "installMode": "lazy",
      "updateMode": "prefetch",
      "resources": {
        "files": [
          "/assets/**"
        ]
      }
    }
  ]
}

关于Angular 5 和 Service Worker : How to exclude a particular path from ngsw-config. json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47693093/

相关文章:

typescript - 当前组件后的angular 2附加指令

angular - 在 Angular 2 的模板中进行类型转换

javascript - 在 Angular 2 组件内的 javascript 中访问 Typescript 变量值

angular - 如何从 Angular 4的url获取参数?

javascript - 如何通过 PostMessage 将 Serviceworker 响应主体 ReadableByteStream 作为 Blob 返回?

angular - 在 IIS 上部署 Angular 项目时,Service Worker 不在离线模式下工作

reactjs - NextJs 中的 PWA 支持

.net-core - 在 .NET Core Worker 服务中根据 Configuration 注册 Hosted Services

angular - 我们如何将 FCM Service Worker 与现有的 Service Worker 一起使用?

带有 POST 请求的 Angular Service Worker