Authored by 陈峰

wkwebview css preload

... ... @@ -39,7 +39,7 @@ exports.devServer = (app, cb) => {
};
clientConfig.entry.app = ['./build/client-hot.js', clientConfig.entry.app];
clientConfig.output.publicPath = 'http://m.yohobuy.com:6005/';
clientConfig.output.publicPath = '//m.yohobuy.com:6005/';
clientConfig.output.filename = '[name].js';
clientConfig.plugins.push(
new webpack.HotModuleReplacementPlugin(),
... ...
... ... @@ -46,7 +46,7 @@ module.exports = {
host: 'badjs.yoho.cn',
port: 80,
db: 'web-apm',
immediate: true
duration: 1000
},
subDomains: {
host: '.m.yohobuy.com',
... ... @@ -175,7 +175,7 @@ if (isProduction) {
host: 'badjs.yohoops.org',
port: 80,
db: 'web-apm',
immediate: true
duration: 1000
},
monitorReport: {
host: '10.66.4.25',
... ...
module.exports = [
{
route: /grass\/article\/\d+$/,
cacheKey: '$url$params',
cacheKey: '$url$params$yoho-protocol',
cacheTime: 900,
cache: true
},
{
route: /grass\/article\/\d+\/user/,
cacheKey: '$url$params',
cacheKey: '$url$params$yoho-protocol',
cacheTime: 900,
cache: true
},
{
route: /grass\/author\/\d+\/\d+/,
cacheKey: '$url$params',
cacheKey: '$url$params$yoho-protocol',
cacheTime: 900,
cache: true
}
... ...
... ... @@ -83,12 +83,14 @@ const handlerError = (err = {}, req, res, next) => {
return next(err);
};
const getCacheKey = (urlPath, cacheKey = '') => {
const urlObj = url.parse(urlPath);
const getCacheKey = (req, cacheKey = '') => {
const urlObj = url.parse(req.url);
const yohoProtocol = req.get('User-Agent').indexOf('yoho-protocol') >= 0 ? 'yoho-protocol' : '';
return md5(cacheKey
.replace('$url', urlObj.pathname)
.replace('$params', urlObj.query));
.replace('$params', urlObj.query || '')
.replace('$yoho-protocol', yohoProtocol));
};
const render = (route) => {
... ... @@ -96,11 +98,12 @@ 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);
}
const ck = route.cacheKey ? getCacheKey(req.url, route.cacheKey) : void 0;
const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0;
if (config.useCache && route.cache && ck) {
const html = await redis.getAsync(ck);
... ... @@ -124,11 +127,10 @@ const render = (route) => {
let context = getContext(req);
renderer.renderToString(context, (err, html) => {
if (err) {
return handlerError(err, req, res, next);
}
const styles = context.renderStyles();
let styles = context.renderStyles();
let scripts = context.renderScripts();
const resources = context.renderResourceHints();
const states = context.renderState();
... ... @@ -137,6 +139,11 @@ const render = (route) => {
if (req.yoho.isiOS) {
asyncScripts = asyncLoadScripts(scripts);
}
if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
route.cacheKey.indexOf('$yoho-protocol') >= 0 &&
isYohoProtocol) {
styles = styles.replace(/"\/\//g, '"yoho-protocol://');
}
const result = template({
html,
... ... @@ -161,11 +168,15 @@ const devRender = (route) => {
return async(req, res, next) => {
try {
res.setHeader('X-YOHO-Version', pkg.version);
const ck = route.cacheKey ? getCacheKey(req.url, route.cacheKey) : void 0;
const ck = route.cacheKey ? getCacheKey(req, route.cacheKey) : void 0;
const isDegrade = _.get(req.app.locals.wap, `webapp.${config.appName}-degrade`, false);
// return require('request-promise')({
// url: 'http://m.yohobuy.com:6005/degrade.html'
// }).pipe(res);
if (isDegrade) {
return require('request-promise')({
url: 'http://m.yohobuy.com:6005/degrade.html'
}).pipe(res);
}
if (config.useCache && route.cache && ck) {
const html = await redis.getAsync(ck);
... ... @@ -201,6 +212,10 @@ const devRender = (route) => {
}
let {styles, scripts, resources, states, html} = msg;
if (req.get('User-Agent').indexOf('yoho-protocol') >= 0 &&
route.cacheKey.indexOf('$yoho-protocol') >= 0) {
styles = styles.replace(/"\/\//g, '"yoho-protocol://');
}
const result = template({
html,
styles,
... ... @@ -209,7 +224,6 @@ const devRender = (route) => {
states
});
if (config.useCache && route.cache && ck) {
redis.setex(ck, route.cacheTime || 60, result);
}
... ...
... ... @@ -26,7 +26,6 @@
{{#if asyncScripts}}
<script>
document.addEventListener('DOMContentLoaded', function() {
console.log('DOMContentLoaded')
setTimeout(function() {
var s = document.getElementsByTagName("script")[0];
{{# asyncScripts}}
... ...