|
|
import $ from 'yoho-jquery';
|
|
|
|
|
|
const isHttps = location.protocol === 'https:';
|
|
|
const usePwa = isHttps;
|
|
|
|
|
|
window.addEventListener('load', function() {
|
|
|
if ('serviceWorker' in navigator) {
|
|
|
if (usePwa) {
|
|
|
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) {
|
|
|
}).then(registration => {
|
|
|
console.log('SW registered: ', registration);
|
|
|
}).catch(err => {
|
|
|
console.log('SW registration failed: ', err);
|
|
|
});
|
|
|
} else {
|
|
|
}
|
|
|
|
|
|
function unregister() {
|
|
|
if (isHttps) {
|
|
|
navigator.serviceWorker.getRegistrations().then(function(registrations) {
|
|
|
navigator.serviceWorker.getRegistrations().then(registrations => {
|
|
|
for (let registration of registrations) {
|
|
|
registration.unregister().then(result => {
|
|
|
console.log('SW unregisted: ', result);
|
...
|
...
|
@@ -26,7 +27,24 @@ window.addEventListener('load', function() { |
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
|
...
|
...
|
|