简体   繁体   English

使用Nginx的Angular i18n json缓存问题

[英]Angular i18n json cache issues using nginx

I'm using angular with i18n translations in json files like de.json and en.json. 我在de.json和en.json之类的json文件中使用带i18n转换的angular。 In my production environment (nginx) I have the problem that these JSON files are cached by the web browser. 在我的生产环境(nginx)中,我有一个问题,这些JSON文件被Web浏览器缓存。 After an upgrade, Chrome will not download the new version of the current json file even though the date header has changed. 升级后,即使日期标题已更改,Chrome也不会下载当前json文件的新版本。

Request Information (Chrome): 请求信息(Chrome):

Response Headers: 响应标题:

  • content-encoding: gzip 内容编码:gzip
  • content-type: application/json 内容类型:application / json
  • date: Fri, 15 Feb 2019 09:04:42 GMT 日期:2019年2月15日星期五09:04:42 GMT
  • etag: W/"5c62bf4d-2aea" etag:W /“ 5c62bf4d-2aea”
  • last-modified: Tue, 12 Feb 2019 12:42:53 GMT 最后修改日期:周二,12 Feb 2019 12:42:53 GMT
  • server: nginx/1.14.0 (Ubuntu) 伺服器:nginx / 1.14.0(Ubuntu)
  • status: 304 状态:304

Does anyone have experience with this problem and can help me? 有没有人有这个问题的经验,可以帮助我吗?

Not specifically a fix for Angular/nginx, but a practice I often use is to append a query string parameter to the resource when you load it. 不是专门针对Angular / nginx的修复程序,而是我经常使用的一种做法是在加载资源时将查询字符串参数附加到资源上。 For me, this is typically derived from version number of the .js file / application, eg using it as a seed for a RNG 对我来说,这通常是从.js文件/应用程序的版本号得出的,例如,将其用作RNG的种子

So, instead of: <script src="/assets/de.json" /> 因此,而不是: <script src="/assets/de.json" />

use <script src="/assets/de.json?_=12345" /> 使用<script src="/assets/de.json?_=12345" />

Bonus points- in your Angular application, you can keep track of what version of the assets that you want to include, meaning that you can release new asset files without having clients immediately update to them if they have them in local cache (although note that new clients will get the new version regardless) 奖励积分-在Angular应用程序中,您可以跟踪要包含的资产的版本,这意味着您可以释放新的资产文件,而不必让客户端将其存储在本地缓存中而立即对其进行更新(尽管请注意,新客户将获得新版本,无论如何)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM