Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

ServiceWorker loaded with workbox-window is 'undefined' on production server

I am using workbox-build (version 5.1.4) in Grunt to generate a service worker for an AngularJs project. I use workbox-window (version 5.1.4) to load the service worker. It loads fine and works great on my dev machine. When I deploy to the production server I get this error:

"DOMException: Failed to register a ServiceWorker for scope ('https://app.url.com/') with script ('https://app.url.com/undefined'): The script has an unsupported MIME type ('text/html')."

Example (this gives an error on production):

import { Workbox} from 'workbox-window';

if ('serviceWorker' in navigator) {
  let registration;
  const wb = new Workbox('/sw.js');
  wb.register().then(r => {
            registration = r;
            console.log('SW scope', r.scope);
        });
 }

The same service worker loads without errors on the production server if I don't use workbox-window so I don't think it is a permission problem or a path problem. Example (this works fine on production):

 window.addEventListener('load', () => {
        let registration;
        navigator.serviceWorker.register('/sw.js').then(r => {
            registration = r;
            console.log('SW scope', r.scope);
        });
    });

This is my service worker config:

workboxBuild.generateSW({
        globDirectory: user_config.app.base_dir,
        globPatterns: [
            'app/**/*.{html,js,css,png,jpg,svg,gif,map,tff,woff}',
            'app/vendor/**/*.{html,js,css,png,jpg,svg,gif,map}',
            'app/*.css',
            'app/images/*.{png,jpg,svg,gif,tff,woff}',
        ],
      //  mode: 'debug',
        runtimeCaching: [
            {
                method: 'GET',
                urlPattern: /.*/,
                handler: 'NetworkFirst'
            }
        ],

        clientsClaim: true,
        ignoreURLParametersMatching: [/.*/],
        navigateFallback: 'app/index.html',
        offlineGoogleAnalytics: true,
        maximumFileSizeToCacheInBytes: 18000000,
        swDest: user_config.app.base_dir + '/sw.js'
    })

Any help would be greatly appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...