|
|
import $ from 'yoho-jquery';
|
|
|
|
|
|
const isHttps = location.protocol === 'https:';
|
|
|
const usePwa = isHttps;
|
|
|
|
|
|
window.addEventListener('load', function() {
|
|
|
if ('serviceWorker' in navigator) {
|
|
|
if (usePwa) {
|
|
|
const t = window.STATIC_RESOURCE_HASH || '';
|
|
|
const staticServer = window.STATIC_RESOURCE_PATH || '';
|
|
|
function register() {
|
|
|
const t = window.STATIC_RESOURCE_HASH || '';
|
|
|
const staticServer = window.STATIC_RESOURCE_PATH || '';
|
|
|
|
|
|
navigator.serviceWorker.register(`/sw.js?t=${t}&staticServer=${staticServer}`, {
|
|
|
scope: '/'
|
|
|
}).then(function(registration) {
|
|
|
console.log('SW registered: ', registration);
|
|
|
}).catch(err => {
|
|
|
console.log('SW registration failed: ', err);
|
|
|
});
|
|
|
} else {
|
|
|
if (isHttps) {
|
|
|
navigator.serviceWorker.getRegistrations().then(function(registrations) {
|
|
|
for (let registration of registrations) {
|
|
|
registration.unregister().then(result => {
|
|
|
console.log('SW unregisted: ', result);
|
|
|
}).catch(err => {
|
|
|
console.log('SW unregistration failed: ', err);
|
|
|
});
|
|
|
}
|
|
|
navigator.serviceWorker.register(`/sw.js?t=${t}&staticServer=${staticServer}`, {
|
|
|
scope: '/'
|
|
|
}).then(registration => {
|
|
|
console.log('SW registered: ', registration);
|
|
|
}).catch(err => {
|
|
|
console.log('SW registration failed: ', err);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function unregister() {
|
|
|
if (isHttps) {
|
|
|
navigator.serviceWorker.getRegistrations().then(registrations => {
|
|
|
for (let registration of registrations) {
|
|
|
registration.unregister().then(result => {
|
|
|
console.log('SW unregisted: ', result);
|
|
|
}).catch(err => {
|
|
|
console.log('SW unregistration failed: ', err);
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
window.addEventListener('load', function() {
|
|
|
if ('serviceWorker' in navigator && isHttps) {
|
|
|
$.ajax({
|
|
|
url: '//m.yohobuy.com/api/switch',
|
|
|
dataType: 'jsonp',
|
|
|
cache: false,
|
|
|
success(res) {
|
|
|
const usePwa = isHttps && res && res.usePwa;
|
|
|
|
|
|
if (usePwa) {
|
|
|
register();
|
|
|
} else {
|
|
|
unregister();
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
|
...
|
...
|
|