...
|
...
|
@@ -9,6 +9,7 @@ const routes = require('../../config/ssr-routes'); |
|
|
const redis = require('../../utils/redis');
|
|
|
const routeEncode = require('../../utils/route-encode');
|
|
|
const {createBundleRenderer} = require('vue-server-renderer');
|
|
|
const Handlebars = require('handlebars');
|
|
|
const logger = global.yoho.logger;
|
|
|
const config = global.yoho.config;
|
|
|
|
...
|
...
|
@@ -19,8 +20,11 @@ let renderer; |
|
|
let serverBundle;
|
|
|
let degradeHtml;
|
|
|
|
|
|
const hbs = fs.readFileSync(path.join(__dirname, '../views/index.hbs'), 'utf-8');
|
|
|
|
|
|
const template = Handlebars.compile(hbs);
|
|
|
|
|
|
if (!isDev) {
|
|
|
const template = fs.readFileSync(path.join(__dirname, '../../index.html'), 'utf-8');
|
|
|
|
|
|
degradeHtml = fs.readFileSync(path.join(__dirname, '../../degrade.html'), 'utf-8');
|
|
|
|
...
|
...
|
@@ -29,11 +33,27 @@ if (!isDev) { |
|
|
|
|
|
renderer = createBundleRenderer(serverBundle, {
|
|
|
runInNewContext: false,
|
|
|
template,
|
|
|
clientManifest
|
|
|
clientManifest,
|
|
|
inject: false
|
|
|
});
|
|
|
}
|
|
|
|
|
|
const REG_SCRIPT = /src="([^"]+)"/g;
|
|
|
|
|
|
const asyncLoadScripts = (renderScripts) => {
|
|
|
let match;
|
|
|
const scripts = [];
|
|
|
|
|
|
while ((match = REG_SCRIPT.exec(renderScripts))) {
|
|
|
scripts.push({
|
|
|
src: match[1],
|
|
|
index: scripts.length
|
|
|
});
|
|
|
}
|
|
|
|
|
|
return scripts;
|
|
|
};
|
|
|
|
|
|
const getContext = (req) => {
|
|
|
let res = {
|
|
|
url: req.url,
|
...
|
...
|
@@ -84,11 +104,17 @@ const getCacheKey = (urlPath, cackeKey = '') => { |
|
|
const render = (route) => {
|
|
|
return async(req, res, next) => {
|
|
|
try {
|
|
|
if (!route.cache) {
|
|
|
res.set({
|
|
|
'Cache-Control': 'no-cache',
|
|
|
Pragma: 'no-cache',
|
|
|
Expires: (new Date(1900, 0, 1, 0, 0, 0, 0)).toUTCString()
|
|
|
});
|
|
|
} else {
|
|
|
res.set({
|
|
|
'Cache-Control': 'max-age=' + route.cacheTime || 60
|
|
|
});
|
|
|
}
|
|
|
res.setHeader('X-YOHO-Version', pkg.version);
|
|
|
const isDegrade = _.get(req.app.locals.wap, 'webapp.degrade', false);
|
|
|
|
...
|
...
|
@@ -113,10 +139,36 @@ const render = (route) => { |
|
|
if (err) {
|
|
|
return handlerError(err, req, res, next);
|
|
|
}
|
|
|
let styles = context.renderStyles();
|
|
|
let scripts = context.renderScripts();
|
|
|
let resources = context.renderResourceHints();
|
|
|
const states = context.renderState();
|
|
|
let asyncScripts;
|
|
|
let zk = {
|
|
|
webperf: _.get(req.app.locals.wap, 'open.webperf', false),
|
|
|
asyncJs: _.get(req.app.locals.wap, 'webapp.ios-async-js', true)
|
|
|
};
|
|
|
|
|
|
if (req.yoho.isiOS && zk.asyncJs) {
|
|
|
asyncScripts = asyncLoadScripts(scripts);
|
|
|
}
|
|
|
|
|
|
const result = template({
|
|
|
html,
|
|
|
styles,
|
|
|
scripts,
|
|
|
asyncScripts,
|
|
|
resources,
|
|
|
states,
|
|
|
zk,
|
|
|
routeHash: routeEncode.getRouter(req)
|
|
|
});
|
|
|
|
|
|
|
|
|
if (config.useCache && route.cache && ck) {
|
|
|
redis.setex(ck, route.cacheTime || 60, html);
|
|
|
redis.setex(ck, route.cacheTime || 60, result);
|
|
|
}
|
|
|
return res.send(html);
|
|
|
return res.send(result);
|
|
|
});
|
|
|
} catch (error) {
|
|
|
return next(error);
|
...
|
...
|
|