javascript未捕获(承诺)DOMException

时间:2019-08-27 07:20:04

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

我已经在网站(带有symfony 4的网站)中应用了pwa,但是在谷歌浏览器中出现错误..service worker未创建,但在Firefox中创建了,这是在chrome中显示的错误

  

未捕获(承诺)的DOMException

代码:

manifest.json:

{
  "lang" : "en",
  "dir" : "ltr",
  "name" : "test",
  "short_name" : "test",
  "icons": [{
    "src": "public/img/icon72x72.png",
    "sizes": "72x72",
    "type": "image/png"
  },{
    "src": "public/img/icon96x96.png",
    "sizes": "96x96",
    "type": "image/png"
  },{
    "src": "public/img/icon144x144.png",
    "sizes": "144x144",
    "type": "image/png"
  },{
    "src": "public/img/icon-48x48.png",
    "sizes": "48x48",
    "type": "image/png"
  },{
    "src": "public/img/icon512x512.png",
    "sizes": "152x152",
    "type": "image/png"
  }, {
    "src": "public/img/icon-192x192.png",
    "sizes": "192x192",
    "type": "image/png"
  }],
  "theme_color" : "#1a1a1a",
  "background_color" : "#1a1a1a",
  "start_url" : "/",
  "display" : "standalone",
  "orientation" : "natural"
}

Gulpfile.js:

'use strict';
    // Include Gulp & Tools We'll Use
    var gulp = require('gulp');
    gulp.task('generate-service-worker', function(callback) {
        var path = require('path');
        var swPrecache = require('sw-precache');
        var rootDir = 'public';
        swPrecache.write(path.join(rootDir, 'service-worker.js'), {
            staticFileGlobs: [rootDir + '/**/*.{js,html,css,png,jpg,gif}'],
            stripPrefix: rootDir,
            navigateFallback: '/',
            runtimeCaching: [{
                urlPattern: /\/planet/,
                handler: 'cacheFirst'
            }],
            verbose: true
        }, callback);
    });

也在页面加载时启动此代码:

if('serviceWorker' in navigator) {
    window.addEventListener('load', function() {
        navigator.serviceWorker
        .register('/service-worker.js', { scope: '/' })
        .then(function() { console.log("Service Worker Registered"); });
    });
}

当我启动此命令:gulp generate-service-worker时,它将在公共目录中创建文件service-worker

0 个答案:

没有答案