Authored by 毕凯

PWA 开关调试

  1 +module.exports = {
  2 + index(req, res) {
  3 + // 千万别缓存
  4 + res.set({
  5 + 'Cache-Control': 'no-cache',
  6 + Pragma: 'no-cache',
  7 + Expires: (new Date(1900, 0, 1, 0, 0, 0, 0)).toUTCString()
  8 + });
  9 +
  10 + res.jsonp({
  11 + usePwa: true
  12 + });
  13 + }
  14 +};
@@ -17,6 +17,7 @@ const hotfix = require(`${cRoot}/hotfix`); @@ -17,6 +17,7 @@ const hotfix = require(`${cRoot}/hotfix`);
17 const apple = require(`${cRoot}/apple`); 17 const apple = require(`${cRoot}/apple`);
18 const rn = require(`${cRoot}/rn`); 18 const rn = require(`${cRoot}/rn`);
19 const forward = require(`${cRoot}/forward`); 19 const forward = require(`${cRoot}/forward`);
  20 +const switchModel = require(`${cRoot}/switch`);
20 21
21 // routers 22 // routers
22 router.post('/api/upload/image', multipartMiddleware, uploadApi.uploadImg); 23 router.post('/api/upload/image', multipartMiddleware, uploadApi.uploadImg);
@@ -25,5 +26,6 @@ router.post('/rn/v1', rn.v1); @@ -25,5 +26,6 @@ router.post('/rn/v1', rn.v1);
25 26
26 router.get('/.well-known/apple-app-site-association', apple.appSiteAssociation); 27 router.get('/.well-known/apple-app-site-association', apple.appSiteAssociation);
27 router.get('/api/wechat/miniapp.jpg', forward.miniapp); 28 router.get('/api/wechat/miniapp.jpg', forward.miniapp);
  29 +router.get('/api/switch', switchModel.index);
28 30
29 module.exports = router; 31 module.exports = router;
  1 +import $ from 'yoho-jquery';
  2 +
1 const isHttps = location.protocol === 'https:'; 3 const isHttps = location.protocol === 'https:';
2 -const usePwa = isHttps;  
3 4
4 -window.addEventListener('load', function() {  
5 - if ('serviceWorker' in navigator) {  
6 - if (usePwa) { 5 +function register() {
7 const t = window.STATIC_RESOURCE_HASH || ''; 6 const t = window.STATIC_RESOURCE_HASH || '';
8 const staticServer = window.STATIC_RESOURCE_PATH || ''; 7 const staticServer = window.STATIC_RESOURCE_PATH || '';
9 8
10 navigator.serviceWorker.register(`/sw.js?t=${t}&staticServer=${staticServer}`, { 9 navigator.serviceWorker.register(`/sw.js?t=${t}&staticServer=${staticServer}`, {
11 scope: '/' 10 scope: '/'
12 - }).then(function(registration) { 11 + }).then(registration => {
13 console.log('SW registered: ', registration); 12 console.log('SW registered: ', registration);
14 }).catch(err => { 13 }).catch(err => {
15 console.log('SW registration failed: ', err); 14 console.log('SW registration failed: ', err);
16 }); 15 });
17 - } else { 16 +}
  17 +
  18 +function unregister() {
18 if (isHttps) { 19 if (isHttps) {
19 - navigator.serviceWorker.getRegistrations().then(function(registrations) { 20 + navigator.serviceWorker.getRegistrations().then(registrations => {
20 for (let registration of registrations) { 21 for (let registration of registrations) {
21 registration.unregister().then(result => { 22 registration.unregister().then(result => {
22 console.log('SW unregisted: ', result); 23 console.log('SW unregisted: ', result);
@@ -26,7 +27,24 @@ window.addEventListener('load', function() { @@ -26,7 +27,24 @@ window.addEventListener('load', function() {
26 } 27 }
27 }); 28 });
28 } 29 }
  30 +}
  31 +
  32 +window.addEventListener('load', function() {
  33 + if ('serviceWorker' in navigator && isHttps) {
  34 + $.ajax({
  35 + url: '//m.yohobuy.com/api/switch',
  36 + dataType: 'jsonp',
  37 + cache: false,
  38 + success(res) {
  39 + const usePwa = isHttps && res && res.usePwa;
  40 +
  41 + if (usePwa) {
  42 + register();
  43 + } else {
  44 + unregister();
  45 + }
29 } 46 }
  47 + });
30 } 48 }
31 }); 49 });
32 50