Authored by 陈峰

commit

const fs = require('fs');
const path = require('path');
const cluster = require('cluster');
const express = require('express');
const chokidar = require('chokidar');
... ...
const routes = require('../config/ssr-routes');
const manifest = require('../dist/manifest.json');
const shelljs = require('shelljs');
const fs = require('fs');
const path = require('path');
const pkg = require('../package.json');
const webpackConfig = require('./webpack.base.conf');
const dir = path.join(__dirname, '../dist/statics/yoho-community-web');
const vender = {
version: pkg.version
};
const protocolPages = routes.filter(r => r.protocol);
const allFiles = [].concat(manifest.initial, manifest.async);
const files = ['../../degrade.html'].concat(allFiles).map(file => path.join(dir, file)).join(' ');
const zipFile = `${pkg.version}.zip`;
shelljs.exec(`zip -j -q ${path.join(dir, zipFile)} ${files}`);
vender.default = 'yoho-protocol://m.yohobuy.com/grass/default';
vender.files = allFiles.map(file => `https:${webpackConfig.output.publicPath}${file}`);
vender.pkgfile = `https://cdn.yoho.cn/yoho-community-web/static/${zipFile}`;
vender.routes = protocolPages.map(p => {
return {
route: p.route.toString(),
template: 'degrade.html'
};
});
fs.writeFileSync(path.join(dir, 'pkg.json'), JSON.stringify(vender, void 0, 2), {
encoding: 'utf-8'
});
... ...
... ... @@ -11,9 +11,9 @@ const isTest = process.env.NODE_ENV === 'test3';
const domains = {
// api: 'http://api.yoho.cn/',
// service: 'http://service.yoho.cn/',
// ufo: 'http://2.yohobuy.cn/',
api: 'http://api.yoho.cn/',
service: 'http://service.yoho.cn/',
ufo: 'http://2.yohobuy.cn/',
// liveApi: 'http://testapi.live.yohops.com:9999/',
// singleApi: 'http://api-test3.yohops.com:9999/',
... ... @@ -24,9 +24,9 @@ const domains = {
// platformApi: 'http://172.16.6.210:8088/',
api: 'http://api-test3.dev.yohocorp.com/',
service: 'http://api-test3.dev.yohocorp.com/',
ufo: 'http://java-yohoufo-fore.test3.ingress.dev.yohocorp.com/ufo-gateway/',
// api: 'http://api-test3.dev.yohocorp.com/',
// service: 'http://api-test3.dev.yohocorp.com/',
// ufo: 'http://java-yohoufo-fore.test3.ingress.dev.yohocorp.com/ufo-gateway/',
};
module.exports = {
... ...
module.exports = [
{
route: /grass\/article\/\d+$/,
cacheKey: '$url$yoho-protocol',
cacheKey: '$url',
cacheTime: 900,
cache: true
cache: true,
protocol: true
},
{
route: /grass\/article\/\d+\/user/,
cacheKey: '$url$yoho-protocol',
cacheKey: '$url',
cache: false
},
{
route: /grass\/article\/share\/\d+/,
cacheKey: '$url$yoho-protocol',
cacheKey: '$url',
cacheTime: 900,
cache: true
},
{
route: /grass\/article\/detail\/\d+/,
cacheKey: '$url$yoho-protocol',
cacheKey: '$url',
cacheTime: 900,
cache: true
},
{
route: /grass\/author\/\d+\/\d+/,
cacheKey: '$url$yoho-protocol',
cacheKey: '$url',
cacheTime: 900,
cache: true
},
{
route: /grass\/author\/mine/,
cacheKey: '$url$yoho-protocol',
cacheKey: '$url',
cacheTime: 900,
cache: true
cache: true,
protocol: true
},
{
route: /grass\/author\/follow\/\d+\/\d+/,
cacheKey: '$url$yoho-protocol',
cacheKey: '$url',
cacheTime: 900,
cache: true
},
{
route: /grass\/author\/fans\/\d+\/\d+/,
cacheKey: '$url$yoho-protocol',
cacheKey: '$url',
cacheTime: 900,
cache: true
},
{
route: /grass\/mine\/follow/,
cacheKey: '$url$yoho-protocol',
cacheKey: '$url',
cacheTime: 900,
cache: true
},
{
route: /grass\/mine\/fans/,
cacheKey: '$url$yoho-protocol',
cacheKey: '$url',
cacheTime: 900,
cache: true
},
... ...
... ... @@ -83,15 +83,12 @@ const handlerError = (err = {}, req, res, next) => {
};
const getCacheKey = (req, cacheKey = '') => {
const isYohoProtocol = _.get(req.app.locals.wap, `webapp.${config.appName}-yoho-protocol`, false);
const urlObj = url.parse(req.url);
const isIos = req.yoho.isiOS;
const yohoProtocol = (req.get('User-Agent').indexOf('yoho-protocol') >= 0 && isYohoProtocol) ? 'yoho-protocol' : '';
return md5(cacheKey
.replace('$url', urlObj.pathname)
.replace('$params', urlObj.query || '')
.replace('$yoho-protocol', yohoProtocol) + (isIos ? 'ios' : 'android'));
.replace('$params', urlObj.query || '') + (isIos ? 'ios' : 'android'));
};
const render = (route) => {
... ... @@ -99,7 +96,6 @@ const render = (route) => {
try {
res.setHeader('X-YOHO-Version', pkg.version);
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
const isYohoProtocol = _.get(req.app.locals.wap, `webapp.${config.appName}-yoho-protocol`, false);
if (isDegrade) {
return res.send(degradeHtml);
... ... @@ -146,13 +142,6 @@ const render = (route) => {
if (req.yoho.isiOS && zk.asyncJs) {
asyncScripts = asyncLoadScripts(scripts);
}
if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
route.cacheKey &&
route.cacheKey.indexOf('$yoho-protocol') >= 0 &&
isYohoProtocol) {
styles = styles.replace(/"\/\//g, '"yoho-protocol://');
resources = resources.replace(/<link rel="preload" href="[^"]+" as="style">/g, '');
}
const result = template({
html,
... ... @@ -223,11 +212,6 @@ const devRender = (route) => {
}
let {styles, scripts, resources, states, html} = msg;
if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
route.cacheKey &&
route.cacheKey.indexOf('$yoho-protocol') >= 0) {
styles = styles.replace(/"\/\//g, '"yoho-protocol://');
}
const result = template({
html,
styles,
... ...