【问题标题】:VueJS PWA: service worker nog working. Maybe due to .htaccess file?VueJS PWA:服务工作者不工作。可能是由于 .htaccess 文件?
【发布时间】:2020-12-04 11:07:08
【问题描述】:

我已经用vue 构建了一个PWA。在开发中一切正常,当我部署到 chrome 网络服务器时。 Service Worker 已注册并正在运行。到目前为止一切顺利。

当我部署到生产环境时,Service Worker 没有运行,因为 scopestart_urlmanifest.json 不匹配(这是 google 开发工具告诉我的)。

我相信这是因为我在历史模式下运行 vue-router 并且在生产环境中运行了一个 .htaccess 文件以按照建议的 here 重写规则。

我还对此进行了测试,以在 vue-router 中切换回哈希模式并再次部署到生产环境。然后 service worker 就可以正常工作了。

为了克服这个问题,我尝试将manifest.json 中的范围设置为index.html

register(`${process.env.BASE_URL}service-worker.js`, {
    registrationOptions: { scope: 'index.html' },
    ready () {
      console.log(
        'App is being served from cache by a service worker.\n'
      )
    }
})

manifest.json 也一样:

{
  "name": "myapp",
  "short_name": "myapp",
  "theme_color": "#4DBA87",
  "icons": [
    { "src": "./img/icons/android-chrome-192x192.png", "sizes": "192x192", "type": "image/png" },
    { "src": "./img/icons/android-chrome-512x512.png", "sizes": "512x512", "type": "image/png" },
    { "src": "./img/icons/android-chrome-maskable-192x192.png", "sizes": "192x192", "type": "image/png", "purpose": "maskable" },
    { "src": "./img/icons/android-chrome-maskable-512x512.png", "sizes": "512x512", "type": "image/png", "purpose": "maskable" }
  ],
  "start_url": "index.html",
  "scope": "index.html",
  "orientation": "portrait",
  "display": "standalone",
  "background_color": "#000000"
}

但 chrome 仍然给我关于范围不同步的消息。

对此有什么想法吗?

【问题讨论】:

  • 有同样的问题.. .htaccess 在服务人员中给出 403 错误。你解决了吗?
  • @Jos Faber 不,我在vue-router切换回哈希模式

标签: apache vue.js vue-router progressive-web-apps


【解决方案1】:

为 pwa 插件添加到 package.json 配置中:

  "vue": {
    "pwa": {
      "workboxOptions": {
        "exclude": [
          ".htaccess"
        ]
      }
    }
  }

更新:

此操作会从 SW 缓存中排除 .htaccess 文件。 因此,您的软件不会崩溃,也不会尝试从服务器下载 .htaccess 文件。

至少,我遇到了同样的问题,发现这个 StackOverflow 主题没有答案。所以,我阅读了 Vue PWA 文档,发现这个对我有用。

【讨论】:

  • 请稍微扩展您的答案。添加此配置后会发生什么情况以及如何解决问题?
  • 此解决方案有效。它基本上所做的是将.htaccess 文件排除在兑现之外(导致问题)。实际上,我通过从我的项目目录中完全删除我的.htaccess 文件获得了相同的效果,因为我只需要在我的生产服务器上使用它..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-13
  • 2015-05-07
相关资源
最近更新 更多