【问题标题】:How service worker works with sub-domain?服务工作者如何与子域一起工作?
【发布时间】:2018-11-03 13:15:03
【问题描述】:

我正在使用 Angular 6 和 ngsw-config.json 进行服务工作者配置,但我不知道它如何与子域一起使用。

据我了解

  1. 用户登陆 example.com
  2. Service Worker 为 example.com 注册。
  3. abc.example.comxyz.example.com 会发生什么?

如何缓存所有子域的assetsGroups和dataGroups?

这是我当前的 ngsw-config.json

{
  "index": "/index.html",
  "assetGroups": [{
    "name": "app",
    "installMode": "prefetch",
    "resources": {
      "files": [
        "/favicon.ico",
        "/index.html",
        "/*.css",
        "/*.js"
      ]
    }
  }, {
    "name": "assets",
    "installMode": "lazy",
    "updateMode": "prefetch",
    "resources": {
      "files": [
        "/assets/**"
      ]
    }
  },{
    "name": "fonts",
    "resources": {
      "urls": [
        "https://fonts.googleapis.com/**",
        "https://fonts.gstatic.com/**"
      ]
    }
  }],
  "dataGroups": [
    {
      "name": "database",
      "urls": [
        "https://**.api.example.com/pl/**",
        "https://**.api.example.com/en/**"
      ],
      "cacheConfig": {
        "maxSize": 100000000,
        "maxAge": "3d",
        "timeout": "2m"
      }
    }
  ]
}

【问题讨论】:

  • 你可能想检查这个相关的SO post,"每个子域都被认为是一个不同的来源,所以是的,你需要为每个子域注册一个服务工作者。这些工作者中的每一个都有自己的自己的缓存和范围。”希望这会有所帮助。

标签: angular service-worker progressive-web-apps angular-service-worker


【解决方案1】:

Service Worker 将每个域和子域视为一个独特的应用程序并且来源不同。因此,您要使用的每个子域都需要单独的服务工作者。 Here is a nice article 讨论了您拥有的各种选项。

如果您认为您可以将子域映射到单个域以及使用别名(例如 abc.example.com 到 example.com/abc/),您可以只使用一名服务人员管理您的应用程序。

但是当这不是一个选项时,拥有多个 Service Worker 并在适当的时候进行预缓存将是理想的替代方案。

【讨论】:

    猜你喜欢
    • 2019-11-27
    • 2015-05-07
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多