ssr.js 7.67 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 Handlebars = require('handlebars');
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;

let degradeHtml;

const hbs = fs.readFileSync(
  path.join(__dirname, '../views/index.hbs'),
  'utf-8',
);

const template = Handlebars.compile(hbs);

if (!isDev) {
  degradeHtml = fs.readFileSync(
    path.join(__dirname, `../../degrade-${pkg.version}.html`),
    'utf-8',
  );

  serverBundle = require('../../manifest.server.json');
  const clientManifest = require('../../manifest.json');

  renderer = createBundleRenderer(serverBundle, {
    runInNewContext: false,
    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 => {
  return {
    url: req.url,
    title: req.query.share_title || '',
    user: req.user,
    env: {
      isApp: req.yoho.isApp,
      isiOS: req.yoho.isiOS,
      isAndroid: req.yoho.isAndroid,
      isWechat: req.yoho.isWechat,
      isQQ: req.yoho.isQQ,
      isWeibo: req.yoho.isWeibo,
      isYohoBuy: req.yoho.isYohoApp,
      isChat: req.yoho.isChat,
      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('/grass/error/404');
  } else if (err.code === 500) {
    return res.redirect('/grass/error/500');
  }
  return next(err);
};

const getChannel = yoho => {
  if (yoho.isYohoApp && yoho.isiOS) {
    return 'ios';
  } else if (yoho.isYohoApp && yoho.isAndroid) {
    return 'android';
  } else if (yoho.isChat) {
    return 'chat';
  } else {
    return 'h5';
  }
};

const getCacheKey = (req, route) => {
  const urlObj = url.parse(req.url);

  let ck = urlObj.pathname;

  if (route.query) {
    const qks = Object.keys(route.query);

    ck += `?${qks
      .map(qk => `${qk}=${(req.query && req.query[qk]) || ''}`)
      .join('&')}`;
  }

  ck += `|${getChannel(req.yoho)}|${pkg.version}`;

  return md5(ck);
};

const render = route => {
  return async(req, res, next) => {
    try {
      res.setHeader('X-YOHO-Version', pkg.version);
      const isDegrade = _.get(
        req.app.locals.wap,
        `webapp.${config.appName}-degrade`,
        false,
      );

      if (isDegrade) {
        return res.send(degradeHtml);
      }
      const ck = getCacheKey(req, route);

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

          res.set({
            'Cache-Control': 'max-age=' + route.cacheTime || 60,
          });
          if (html) {
            logger.debug(`cached ${req.url}`);
            res.setHeader('X-YOHO-Cached', 'HIT');
            return res.send(html);
          }
          res.setHeader('X-YOHO-Cached', 'MISS');
        } catch (err) {
          logger.error(`get cache error: ${err.message}`);
        }
      } else {
        res.set({
          'Cache-Control': 'no-cache',
          Pragma: 'no-cache',
          Expires: new Date(1900, 0, 1, 0, 0, 0, 0).toUTCString(),
        });
      }
      let context = getContext(req);

      renderer.renderToString(context, (err, html) => {
        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 = {
          asyncJs: _.get(req.app.locals.wap, 'webapp.ios-async-js', true),
        };

        if (process.env.NODE_ENV === 'production') {
          zk.webperf = _.get(req.app.locals.wap, 'open.webperf', false);
        }

        scripts = scripts.replace(/defer/g, 'defer crossorigin="anonymous"');

        // resources = resources.replace(/link/g, 'link crossorigin="anonymous"');
        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, result);
        }
        return res.send(result);
      });
    } catch (error) {
      return next(error);
    }
  };
};
const devRender = route => {
  return async(req, res, next) => {
    try {
      res.setHeader('X-YOHO-Version', pkg.version);
      const ck = getCacheKey(req, route);

      const isDegrade = _.get(
        req.app.locals.wap,
        `webapp.${config.appName}-degrade`,
        false,
      );

      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);

        res.set({
          'Cache-Control': 'max-age=' + route.cacheTime || 60,
        });
        if (html) {
          logger.debug(`cached ${req.url}`);
          res.setHeader('X-YOHO-Cached', 'HIT');
          return res.send(html);
        }
        res.setHeader('X-YOHO-Cached', 'MISS');
      } else {
        res.set({
          'Cache-Control': 'no-cache',
          Pragma: 'no-cache',
          Expires: new Date(1900, 0, 1, 0, 0, 0, 0).toUTCString(),
        });
      }
      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);
          }
          let { styles, scripts, resources, states, html } = msg;

          scripts = scripts.replace(/defer/g, 'defer crossorigin="anonymous"');

          // resources = resources.replace(/link/g, 'link crossorigin="anonymous"');
          const result = template({
            html,
            styles,
            scripts,
            resources,
            states,
            routeHash: routeEncode.getRouter(req),
          });

          if (config.useCache && route.cache && ck) {
            redis.setex(ck, route.cacheTime || 60, result);
          }
          return res.end(result);
        }
      };

      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;