Authored by 毕凯

增加离线页面

module.exports = {
offline(req, res) {
res.send('you are offline');
}
};
... ...
/**
* sub app channel
* @author: Bi Kai<kai.bi@yoho.cn>
* @date: 2016/05/09
*/
var express = require('express'),
path = require('path'),
hbsEvent = require('../../config/hbsevent');
var app = express();
// set view engin
var doraemon = path.join(__dirname, '../../doraemon/views'); // parent view root
app.on('mount', function(parent) {
delete parent.locals.settings; // 不继承父 App 的设置
Object.assign(app.locals, parent.locals);
});
app.disable('x-powered-by');
app.use(global.yoho.hbs({
extname: '.hbs',
defaultLayout: 'layout',
layoutsDir: doraemon,
partialsDir: path.join(__dirname, 'views/partial'),
views: path.join(__dirname, 'views/action'),
helpers: global.yoho.helpers,
cb: hbsEvent.cb
}));
// router
app.use(require('./router'));
module.exports = app;
... ...
/**
* router of sub app channel
* @author: Bi Kai<kai.bi@yoho.cn>
* @date: 2016/05/09
*/
'use strict';
const express = require('express');
const cRoot = './controllers';
const common = require(cRoot);
const router = express.Router(); // eslint-disable-line
router.get('/offline.html', common.offline); // 离线页面
module.exports = router;
... ...
... ... @@ -34,4 +34,7 @@ module.exports = app => {
// MIP
app.use('/mip', require('./apps/mip'));
// 一些公用页面,比如离线页面
app.use(require('./apps/common'));
};
... ...
... ... @@ -13,7 +13,7 @@ module.exports = {
module: {
rules: [{
test: /\.(js|mjs)$/,
include: [path.join(__dirname, '../../node_modules/workbox-sw')],
include: [path.join(__dirname, '../../node_modules/workbox-sw'), path.join(__dirname, '../js')],
use: 'babel-loader'
}]
},
... ...
... ... @@ -7,6 +7,9 @@ const config = {
/^https:\/\/(.*)cdn\.yoho\.cn/i,
/^https:\/\/(.*)static\.yhbimg\.com/i
],
precachePage: [
'/offline.html'
],
precacheStaticFile: [
'/index.css',
'/common.css',
... ... @@ -31,13 +34,17 @@ const cacheFirstStrategy = workboxSW.strategies.cacheFirst({
// 特殊路径的预缓存文件文件
const precacheFile = [{
url: '/sw.js?t=' + qs.t + '&staticServer=' + qs.staticServer
}].concat(config.precacheStaticFile.map(function(file) {
}].concat(config.precacheStaticFile.map(file => {
// 根据业务自定义资源路径
const url = self.location.protocol + qs.staticServer + file + '?t=' + qs.t;
return {
url: url
};
})).concat(config.precachePage.map(page => {
return {
url: page
};
}));
// 预加载文件
... ... @@ -50,3 +57,14 @@ config.customCacheUrl.forEach(function(urlRegExp) {
cacheFirstStrategy
);
});
workboxSW.router.registerRoute(/.*/, args => {
return workboxSW.strategies.networkFirst().handle(args).then(res => {
if (res || args.event.request.mode !== 'navigate') {
return res;
}
// navigate 请求失败后,返回网络异常页面
return caches.match('offline.html');
});
});
... ...
This diff could not be displayed because it is too large.