Authored by 毕凯

Merge branch 'feature/PWA' of git.yoho.cn:fe/yohobuywap-node into feature/PWA

const _ = require('lodash');
module.exports = {
index(req, res) {
// 千万别缓存
... ... @@ -7,8 +9,10 @@ module.exports = {
Expires: (new Date(1900, 0, 1, 0, 0, 0, 0)).toUTCString()
});
let closeUserPwa = _.get(req.app.locals.wap, 'close.usePwa', false);
res.jsonp({
usePwa: true
usePwa: !closeUserPwa
});
}
};
... ...
const headerModel = require('../../../doraemon/models/header'); // 头部model
module.exports = {
offline(req, res) {
res.send('you are offline');
res.render('offline', {
pageHeader: headerModel.setNav(),
module: 'common',
page: 'offline'
});
}
};
... ...
<div class="offline-page yoho-page">
<div class="offline-box">
<div class="offline-bg"></div>
<p class="slogan">页面加载失败,请重试</p>
<button class="refresh" id="refreshBtn">刷新重试</button>
</div>
</div>
... ...
... ... @@ -129,6 +129,7 @@
"webpack-dev-server": "^2.8.2",
"webpack-hot-middleware": "^2.19.1",
"webpack-merge": "^4.1.0",
"whatwg-fetch": "^2.0.3",
"workbox-sw": "^2.1.2",
"yoho-cookie": "^1.2.0",
"yoho-fastclick": "^1.0.6",
... ...
import 'common/offline.page.css';
import $ from 'yoho-jquery';
class Offline {
constructor() {
this.selector = {
refreshBtn: $('#refreshBtn')
};
this.selector.refreshBtn.on('click', () => {
location.reload();
});
}
}
export default new Offline();
... ...
... ... @@ -197,6 +197,10 @@ class ProductListWithFilter {
$('.no-result-new').remove();
}
if ($('.search-divide').length > 0) {
$('.search-divide').remove();
}
this.view.container.after(() => {
return '<div class="search-divide">正在加载...</div>';
});
... ...
... ... @@ -8,10 +8,16 @@ function register() {
navigator.serviceWorker.register(`/sw.js?t=${t}&staticServer=${staticServer}`, {
scope: '/'
}).then(registration => {
console.log('SW registered: ', registration);
}).then(() => {
if (window._hmt && window._hmt.push) {
window._hmt.push(['_trackEvent', 'SW registered', 'SUCCESS',
document.title, location.href]);
}
}).catch(err => {
console.log('SW registration failed: ', err);
if (window._hmt && window._hmt.push) {
window._hmt.push(['_trackEvent', 'SW registered', 'FAILED',
document.title, JSON.stringify(err)]);
}
});
}
... ... @@ -19,10 +25,16 @@ function unregister() {
if (isHttps) {
navigator.serviceWorker.getRegistrations().then(registrations => {
for (let registration of registrations) {
registration.unregister().then(result => {
console.log('SW unregisted: ', result);
registration.unregister().then(() => {
if (window._hmt && window._hmt.push) {
window._hmt.push(['_trackEvent', 'SW unregistered', 'SUCCESS',
document.title, location.href]);
}
}).catch(err => {
console.log('SW unregistration failed: ', err);
if (window._hmt && window._hmt.push) {
window._hmt.push(['_trackEvent', 'SW unregistered', 'FAILED',
document.title, JSON.stringify(err)]);
}
});
}
});
... ...
/* eslint-env worker */
import 'whatwg-fetch';
import WorkboxSW from 'workbox-sw';
import parseQs from 'yoho-qs/parse';
... ... @@ -13,7 +14,10 @@ const config = {
precacheStaticFile: [
'/index.css',
'/common.css',
'/libs.js'
'/libs.js',
'/common.offline.js',
'/font/iconfont.woff',
'/img/common/404.png'
]
};
const qs = parseQs(self.location.search.substr(1));
... ...
.offline-page {
.offline-box {
width: 100%;
position: fixed;
top: 50%;
text-align: center;
transform: translateY(-50%);
}
.offline-bg {
height: 200px;
width: 200px;
background: resolve("common/404.png") no-repeat;
background-size: 100% 100%;
display: inline-block;
}
.slogan {
margin-top: 40px;
}
.refresh {
width: 240px;
margin-top: 40px;
padding: 20px;
border-radius: 4px;
background-color: #444;
color: #fff;
}
}
... ...
This diff could not be displayed because it is too large.