如何在Angular 4 Universal中添加Cache-control和ETag标头?

时间:2017-06-26 14:34:52

标签: express caching cache-control etag angular-universal

运行Google页面速​​度测试后,我得到了建议:

  

在HTTP标头中为静态设置到期日期或最长期限   资源指示浏览器加载以前下载的   来自本地磁盘而非网络的资源。

server.ts

//import statements   

enableProdMode();
const app = express();
const port = Number(process.env.PORT || 8080);

app.engine('html', ngExpressEngine({
  bootstrap: ServerAppModule
}));

app.set('view engine', 'html');
app.set('views', 'src');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

import {
  getHomeApi, sendMailApi
} from './backend/api';

app.get('/api/homeData', getHomeApi);
app.post("/api/send", sendMailApi);


app.use(compression());

app.use('',express.static('dist'));

app.use('/assets',express.static('dist/assets'));

app.use('/static',express.static('dist/static', {
  index: false
}));

ROUTES.forEach(route => {
  app.get(route, (req, res) => {
    console.time(`GET: ${req.originalUrl}`);
    res.render('../dist/index', {
      req: req,
      res: res
    });
    console.timeEnd(`GET: ${req.originalUrl}`);
  });
});

app.listen(port,() => {
    console.log(`Listening at ${port}`);
});

它建议我利用缓存来跟踪静态资源。

  

http://00.000.00.00:8080/api/homeData(未指定过期)

     

http://dsms0mj1bbhn4.cloudfront.net/assets/pub/shareaholic.js   (10分钟)

     

https://www.google-analytics.com/analytics.js(2小时)

那我该怎么做呢? 谢谢。

0 个答案:

没有答案