electron-updater更新遇到的问题 The URL protocol of the current origin (‘app://.‘) is not supported

淩亂°似流年 2023-01-13 06:28 309阅读 0赞

目前的Vue脚手架(我使用的是vue-cli4)默认会携带一个 registerServiceWorker的东西,这个东西是做什么的呢?

首先说明,registerServiceWorker可以运用于主流框架,它只是为了简化缓存机制产生的js包,以下的例子是在Vue中使用
这个文件可以视情况用或者不用,它是用来做离线缓存等任务的,实际上就是为Vue项目注册了一个service worker。这样的话,如果在线上,只要访问过一次该网站,以后即使没有网络也可以访问(此时使用的是之前缓存的资源)。只在生产环境中有效(process.env.NODE_ENV === ‘production’)

使用service worker的现象是什么呢?

他会在第一次访问该站点的时候,一次性请求加载所有资源,当然 除了当前页面的资源是通过浏览器去获取的,其他缓存资源都是通过service worker.

  1. /* eslint-disable no-console */
  2. import { register } from 'register-service-worker';
  3. if (process.env.NODE_ENV === 'production') {
  4. register(`${process.env.BASE_URL}service-worker.js`, {
  5. ready() {
  6. console.log(
  7. 'App is being served from cache by a service worker.\n' +
  8. 'For more details, visit https://goo.gl/AFskqB'
  9. );
  10. },
  11. registered() {
  12. console.log('Service worker has been registered.');
  13. },
  14. cached() {
  15. console.log('Content has been cached for offline use.');
  16. },
  17. updatefound() {
  18. console.log('New content is downloading.');
  19. },
  20. updated() {
  21. console.log('New content is available; please refresh.');
  22. },
  23. offline() {
  24. console.log(
  25. 'No internet connection found. App is running in offline mode.'
  26. );
  27. },
  28. error(error) {
  29. console.error('Error during service worker registration:', error);
  30. }
  31. });
  32. }

但是使用 electron-updater更新代码的时候会出现:

20210416174554644.png

Error during service worker registration: TypeError: Failed to register a ServiceWorker: The URL protocol of the current origin (‘app://.’) is not supported.

electron协议拦截是资源层面,跟网页serviceworker工作不是一个内容。sw无法在非https和localhost下工作。看一下你代码,把这个功能移除。使用协议拦截,就没必要在用sw。

因此,在electron环境当中需要把这个功能移除,注释掉:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzM2NzI4NTE4_size_16_color_FFFFFF_t_70

发表评论

表情:
评论列表 (有 0 条评论,309人围观)

还没有评论,来说两句吧...

相关阅读