Authored by 姜枫

support ajax cache

@@ -17,12 +17,9 @@ app.on('mount', function(parent) { @@ -17,12 +17,9 @@ app.on('mount', function(parent) {
17 delete parent.locals.settings; // 不继承父 App 的设置 17 delete parent.locals.settings; // 不继承父 App 的设置
18 Object.assign(app.locals, parent.locals); 18 Object.assign(app.locals, parent.locals);
19 }); 19 });
20 -app.set('views', path.join(__dirname, 'views/action'));  
21 app.use(global.yoho.hbs({ 20 app.use(global.yoho.hbs({
22 extname: '.hbs', 21 extname: '.hbs',
23 - defaultLayout: 'layout',  
24 - layoutsDir: doraemon,  
25 - partialsDir: path.join(__dirname, 'views/partial', `${doraemon}/partial`), 22 + partialsDir: path.join(__dirname, 'views/partial'),
26 views: path.join(__dirname, 'views/action'), 23 views: path.join(__dirname, 'views/action'),
27 helpers: Object.assign({}, global.yoho.helpers, helpers) 24 helpers: Object.assign({}, global.yoho.helpers, helpers)
28 })); 25 }));
@@ -3,12 +3,10 @@ @@ -3,12 +3,10 @@
3 3
4 const path = require('path'); 4 const path = require('path');
5 const cachePage = require('../../config/cache'); 5 const cachePage = require('../../config/cache');
  6 +const logger = global.yoho.logger;
6 7
7 module.exports = () => { 8 module.exports = () => {
8 return (req, res, next) => { 9 return (req, res, next) => {
9 - if (req.get('X-Requested-With') === 'XMLHttpRequest') {  
10 - res.set('Cache-Control', 'no-cache');  
11 - }  
12 10
13 function onRender() { 11 function onRender() {
14 let route = req.route ? req.route.path : ''; 12 let route = req.route ? req.route.path : '';
@@ -17,6 +15,8 @@ module.exports = () => { @@ -17,6 +15,8 @@ module.exports = () => {
17 15
18 req.app.set('etag', false); 16 req.app.set('etag', false);
19 17
  18 + logger.debug(`route: ${key} cache = ${cachePage[key]}`);
  19 +
20 // 如果存在cache配置,并且业务代码中没有设置 20 // 如果存在cache配置,并且业务代码中没有设置
21 if (cachePage[key] && res.get('Cache-Control') !== 'no-cache') { 21 if (cachePage[key] && res.get('Cache-Control') !== 'no-cache') {
22 res.set({ 22 res.set({
@@ -24,6 +24,8 @@ module.exports = () => { @@ -24,6 +24,8 @@ module.exports = () => {
24 }); 24 });
25 res.removeHeader('Pragma'); 25 res.removeHeader('Pragma');
26 res.removeHeader('Expires'); 26 res.removeHeader('Expires');
  27 + } else if (req.get('X-Requested-With') === 'XMLHttpRequest') {
  28 + res.set('Cache-Control', 'no-cache');
27 } else { 29 } else {
28 res.set({ 30 res.set({
29 'Cache-Control': 'no-cache', 31 'Cache-Control': 'no-cache',