ssr.js 4.31 KB
const fs = require('fs');
const path = require('path');
const url = require('url');
const _ = require('lodash');
const os = require('os');
const md5 = require('yoho-md5');
const pkg = require('../../package.json');
const routes = require('../../config/ssr-routes');
const redis = require('../../utils/redis');
const routeEncode = require('../../utils/route-encode');
const {createBundleRenderer} = require('vue-server-renderer');
const logger = global.yoho.logger;
const config = global.yoho.config;

const isDev = process.env.NODE_ENV === 'development' || !process.env.NODE_ENV;

let renderer;
let serverBundle;

if (!isDev) {
  const template = fs.readFileSync(path.join(__dirname, '../../apps/index.html'), 'utf-8');

  serverBundle = require(`../../dist/yohoblk-wap/bundle/yoho-ssr-server-${pkg.version}.json`);
  const clientManifest = require(`../../dist/yohoblk-wap/bundle/yoho-ssr-client-${pkg.version}.json`);

  renderer = createBundleRenderer(serverBundle, {
    runInNewContext: false,
    template,
    clientManifest
  });
}

const getContext = (req) => {
  return {
    url: req.url,
    title: 'BLK!',
    user: req.user,
    env: {
      isApp: req.yoho.isApp,
      isiOS: req.yoho.isiOS,
      isAndroid: req.yoho.isAndroid,
      isYohoApp: req.yoho.isYohoApp,
      clientIp: req.yoho.clientIp,
    },
    ua: req.get('user-agent'),
    hostname: os.hostname(),
    route: `[${req.method}]${_.get(req, 'route.path', '')}`, // 请求路由
    udid: _.get(req, 'cookies.udid', 'yoho'),
    path: `[${req.method}]${routeEncode.getRouter(req)}`,
  };
};

const handlerError = (err = {}, req, res, next) => {
  if (err.code === 404) {
    return res.redirect('/error/404');
  } else if (err.code === 500) {
    return res.redirect('/error/500');
  }
  return next(err);
};

const getCacheKey = (urlPath, cackeKey = '') => {
  const urlObj = url.parse(urlPath);

  return md5(cackeKey
    .replace('$url', urlObj.pathname)
    .replace('$params', urlObj.query));
};

const render = (route) => {
  return async(req, res, next) => {
    try {
      res.setHeader('X-YOHO-Version', pkg.version);
      const ck = route.cackeKey ? getCacheKey(req.url, route.cackeKey) : void 0;

      if (config.useCache && route.cache && ck) {
        const html = await redis.getAsync(ck);

        if (html) {
          logger.debug(`cached ${req.url}`);
          res.setHeader('X-YOHO-Cached', 'HIT');
          return res.send(html);
        }
        res.setHeader('X-YOHO-Cached', 'MISS');
      }
      let context = getContext(req);

      renderer.renderToString(context, (err, html) => {
        if (err) {
          return handlerError(err, req, res, next);
        }
        if (config.useCache && route.cache && ck) {
          redis.setex(ck, route.cacheTime || 60, html);
        }
        return res.send(html);
      });
    } catch (error) {
      return next(error);
    }
  };
};
const devRender = (route) => {
  return async(req, res, next) => {
    try {
      res.setHeader('X-YOHO-Version', pkg.version);
      const ck = route.cackeKey ? getCacheKey(req.url, route.cackeKey) : void 0;

      if (config.useCache && route.cache && ck) {
        const html = await redis.getAsync(ck);

        if (html) {
          logger.debug(`cached ${req.url}`);
          res.setHeader('X-YOHO-Cached', 'HIT');
          return res.send(html);
        }
        res.setHeader('X-YOHO-Cached', 'MISS');
      }
      let context = getContext(req);

      process.send({action: 'ssr_request', context});
      let event = msg => {
        process.removeListener('message', event);
        if (msg.action === 'ssr_request') {
          if (msg.err) {
            let err = msg.err;

            try {
              err = JSON.parse(msg.err);
            } catch (error) {} // eslint-disable-line
            return handlerError(err, req, res, next);
          }
          if (config.useCache && route.cache && ck) {
            redis.setex(ck, route.cacheTime || 60, msg.html);
          }
          return res.end(msg.html);
        }
      };

      process.on('message', event);
    } catch (error) {
      return next(error);
    }
  };
};

const router = require('express').Router(); //eslint-disable-line

_.each(routes, r => {
  if (!r.disable) {
    router.get(r.route, isDev ? devRender(r) : render(r));
  }
});

exports.ssrRender = isDev ? devRender({}) : render({});

exports.routers = router;