Blame view

public/js/pwa/pwa.js 2.49 KB
毕凯 authored
1
import $ from 'yoho-jquery';
陈峰 authored
2
import yoho from 'js/yoho-app';
毕凯 authored
3
4 5
const canUse = location.protocol === 'https:' || location.hostname === 'localhost';
const isDev = process.env.NODE_ENV === 'development' || !process.env.NODE_ENV;
毕凯 authored
6
毕凯 authored
7
function register() {
8 9
    if (canUse && navigator && navigator.serviceWorker && navigator.serviceWorker.register) {
        navigator.serviceWorker.register(`/sw.js${isDev ? '?dev' : ''}`).then(() => {
郭成尧 authored
10 11 12 13 14
            if (window._hmt && window._hmt.push) {
                window._hmt.push(['_trackEvent', 'SW registered', 'SUCCESS',
                    document.title, location.href]);
            }
        }).catch(err => {
15
            console.error(err);
郭成尧 authored
16 17 18 19 20 21
            if (window._hmt && window._hmt.push) {
                window._hmt.push(['_trackEvent', 'SW registered', 'FAILED',
                    document.title, JSON.stringify(err)]);
            }
        });
    }
毕凯 authored
22 23 24
}

function unregister() {
25
    if (canUse && navigator && navigator.serviceWorker && navigator.serviceWorker.getRegistrations) {
毕凯 authored
26 27
        navigator.serviceWorker.getRegistrations().then(registrations => {
            for (let registration of registrations) {
郭成尧 authored
28 29 30 31 32
                registration.unregister().then(() => {
                    if (window._hmt && window._hmt.push) {
                        window._hmt.push(['_trackEvent', 'SW unregistered', 'SUCCESS',
                            document.title, location.href]);
                    }
毕凯 authored
33
                }).catch(err => {
郭成尧 authored
34 35 36 37
                    if (window._hmt && window._hmt.push) {
                        window._hmt.push(['_trackEvent', 'SW unregistered', 'FAILED',
                            document.title, JSON.stringify(err)]);
                    }
郭成尧 authored
38 39
                });
            }
毕凯 authored
40 41 42 43 44
        });
    }
}

window.addEventListener('load', function() {
郭成尧 authored
45 46
    // Android WebView 中 UserAgent 不正确
    if (!yoho.isApp && !(yoho.isAndroid && yoho.isWechat)) {
47
        if ('serviceWorker' in navigator) {
郭成尧 authored
48 49 50 51 52
            $.ajax({
                url: '//m.yohobuy.com/api/switch',
                dataType: 'jsonp',
                cache: false,
                success(res) {
53
                    const usePwa = res && res.usePwa;
毕凯 authored
54
郭成尧 authored
55 56 57 58 59
                    if (usePwa) {
                        register();
                    } else {
                        unregister();
                    }
毕凯 authored
60
                }
郭成尧 authored
61 62
            });
        }
毕凯 authored
63 64
    }
});
65 66 67 68 69 70

// 暂时不自动弹出添加到桌面按钮
window.addEventListener('beforeinstallprompt', function(e) {
    e.preventDefault();
    return false;
});