我有一个静态内容要使用 nodejs 缓存,据我所知有两种方法:
这可以使用 nodejs 来完成:
app.use(express.static(path.join(__dirname, 'public'), { maxAge: oneDay }));
将静态内容的x.appcahe文件添加到缓存中,然后在html标签中添加manifest属性:
<html manifest="x.appcache">
所以我的问题是这两种方式有什么区别?
最佳答案
- 首先,node.js 服务器正在设置 Cache-Control header使用 express.js 功能。
- 在第二种情况下,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/