Authored by 毕凯

PWA 开关调试

module.exports = {
index(req, res) {
// 千万别缓存
res.set({
'Cache-Control': 'no-cache',
Pragma: 'no-cache',
Expires: (new Date(1900, 0, 1, 0, 0, 0, 0)).toUTCString()
});
res.jsonp({
usePwa: true
});
}
};
... ...
... ... @@ -17,6 +17,7 @@ const hotfix = require(`${cRoot}/hotfix`);
const apple = require(`${cRoot}/apple`);
const rn = require(`${cRoot}/rn`);
const forward = require(`${cRoot}/forward`);
const switchModel = require(`${cRoot}/switch`);
// routers
router.post('/api/upload/image', multipartMiddleware, uploadApi.uploadImg);
... ... @@ -25,5 +26,6 @@ router.post('/rn/v1', rn.v1);
router.get('/.well-known/apple-app-site-association', apple.appSiteAssociation);
router.get('/api/wechat/miniapp.jpg', forward.miniapp);
router.get('/api/switch', switchModel.index);
module.exports = router;
... ...
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();
}
}
});
}
});
... ...