layout-tools.js 674 Bytes
'use strict';

const _ = require('lodash');
const config = require('../../config/common');
const dnsPrefetch = require('../../config/dns-prefetch');

module.exports = () => {
    return (req, res, next) => {

        if (_.get(req.app.locals, 'pc.qcloud_cdn')) {
            res.locals.cdn = 'qcloud';
        } else {
            res.locals.cdn = config.cdn || 'aliyun';
        }

        let cdn = [];

        if (res.locals.cdn === 'qcloud') {
            cdn = _.uniq(_.values(dnsPrefetch));
        } else {
            cdn = Object.keys(dnsPrefetch);
        }

        Object.assign(res.locals, {
            dnsPrefetch: cdn
        });

        next();
    };
};