javascript - 缓存静态页面和内容nodejs

标签 javascript html node.js

我有一个静态内容要使用 nodejs 缓存,据我所知有两种方法:

  1. 这可以使用 nodejs 来完成:

    app.use(express.static(path.join(__dirname, 'public'), { maxAge: oneDay }));

  2. 将静态内容的x.appcahe文件添加到缓存中,然后在html标签中添加manifest属性:

    <html manifest="x.appcache">

所以我的问题是这两种方式有什么区别?

最佳答案

  1. 首先,node.js 服务器正在设置 Cache-Control header使用 express.js 功能。
  2. 在第二种情况下,HTML 指定要由浏览器缓存的 list 。

如果您搜索“cache-control vs html manifest”,您会发现大量详细信息,超出了我在一个答案中所能写出的内容。但是,另一个标题为 HTML 5 Cache Manifest Vs. Etags, Expires or cache-control header 的问题有一个很好的总结和引用

The main differences between the HTML5 cache manifest vs. the traditional HTTP headers:

  • for the cache manifest you need support in the browser
  • for the HTTP headers you also need support in the browser of course but it's more universal
  • you have more control over the caching with cache manifest
  • your website or Web app can work correctly offline with no connection at all
  • you can have two version of every resource - for offline and online usage

The last point is very handy and lets you easily swap parts of your website that need connection with eg. placeholders containing optional comments that the user doesn't get full functionality without the connection or whatever you want.

还有一些兼容性问题,因为某些浏览器并不总是按照标准播放...所以这也值得研究并确定哪种(缓存控制与 list )最适合您的情况。

关于javascript - 缓存静态页面和内容nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24677113/

相关文章:

node.js - 如何使用 .ebextensions 在 Amazon Elastic Beanstalk 中运行 `npm install`

javascript - 使用javascript添加带有动态元素问题的html div

javascript - typescript 2 : Import statement generates TS2532: "Object is possibly ' undefined'.“

php - Bootstrap 输入类型 ="number"不执行任何操作

html - Firefox 复选框样式问题

javascript - Express JS Node JS multer 集成问题

javascript - 如果在单个 HTML 文档中多次使用具有相同 "src"属性的 &lt;script&gt; 标记会发生什么情况?

javascript - 为对话框获取值

jquery - 替代 fadeOut() 以创建脉冲效果

node.js - 在不捆绑 ORM 的情况下在前后端之间进行类型共享