如果我将新版本的javascript文件上传到Amazon S3,我是否应该期待浏览器缓存问题?(If I upload a new version of a javascript file to A

编程入门 行业动态 更新时间:2024-10-27 13:33:06
如果我将新版本的javascript文件上传到Amazon S3,我是否应该期待浏览器缓存问题?(If I upload a new version of a javascript file to Amazon S3, should I expect browser caching problems?)

我们有很多人(10k +)定期返回我的客户网站,使用我们建立,改进和托管他们的网络应用程序。 随着我们的应用程序的改进和发展,我们一直在对Web应用程序的javascript进行相当频繁的向后兼容的更新。 在部署期间,javascript被缩小并连接成一个文件,由require.js加载到浏览器中,并上传到Amazon S3并在其上托管。 更新期间文件名和URL目前根本不会更改。 上周我们为网络应用程序部署了一个主要的重构,并得到一些(但不是很多)报告,该应用程序停止为某些人工作,特别是在Firefox中。 这似乎是一个缓存问题。 我们最初在几个浏览器中看到它在测试中,但它似乎在一两次刷新之后就消失了。

我突然意识到,我真的不知道在S3上部署新版本的javascript文件(具有相同名称)的浏览器缓存后果会是什么,以及这种情况是否需要缓存破坏或操纵S3的标题或任何内容。 有人可以帮我解决这个问题吗? 我是否应该在部署期间采取措施以确保浏览器能够立即获得新版本的javascript文件? 如果没有,我们冒着javascript和服务器API不同步和失败的风险,我认为这发生在这里。

不确定是否重要,但网站的服务器运行Django,应用程序和数据库部署到Heroku。 使用S3Boto通过Django的collectstatic命令将静态文件部署到S3。

We have a large number of people (10k+) who return to my clients' sites on a regular basis to use a web app we built, improve, and host for them. We have been making fairly frequent backward-incompatible updates to the web app's javascript as our app has improved and evolved. During deployments, the javascript is minified and concatenated into one file, loaded in the browser by require.js, and is uploaded to and hosted on Amazon S3. The file name & url currently doesn't change at all during updates. This last week we deployed a major refactor to the web app and got a few (but not a lot) of reports back that the app stopped working for some people, particularly in firefox. It seemed like a caching issue. We were able to see it initially in a few browsers in testing but it seemed to go away after a refresh or two.

It dawned on me that I really don't know what browser-caching ramifications deploying a new version of a javascript file (with the same name) on S3 will have and whether this situation warrants cache-busting or manipulating S3's headers or anything. Can someone help me get a handle on this? Are there actions I should be taking during deployments to ensure that browsers will immediately get the new version of a javascript file? If not, we run the risk of the javascript and the server API being out of sync and failing, which I think happened here.

Not sure if it matters, but the site's server runs Django and the app and DB are deployed to Heroku. Static files are deployed to S3 using S3Boto via Django's collectstatic command.

最满意答案

这很大程度上取决于S3的行为以及它在S3上请求文件时发送的标头。 根据您的经验,浏览器将显示不同的缓存行为 - 因此最佳选择是使用唯一的文件名。

我建议使用cachebuster哈希 - 通过这种方式,您可以确保浏览器始终要求新文件,如果您在自己的服务器上托管文件,则可以使用长缓存生命周期头文件。 例如,您可以创建缩小文件的MD5哈希并附加它(如mycss-322242fadcd23.css )。 或者您可以使用源控制系统的修订号。 您必须在此文件的所有链接中使用缓存破坏程序,但通常可以在嵌入静态资源的模板中轻松执行此操作。 根据您的应用程序,您可能可以使用这个Django插件来完成这项工作。

This depends a lot on the behaviour of S3 and the headers it sends when requesting files on S3. As you experienced, browsers will show different caching behaviour - so the best option is to use unique filenames.

I would suggest to use cachebuster hashes - in this way you can be sure that the new file always gets requested by browsers and you can use long cache-lifetime headers if you host the files on your own server. You can for example create a MD5 hash of your minified file and append it (like mycss-322242fadcd23.css). Or you could use the revision number of your source control system. You have to use the cache buster in all links to this file, but you can normally easily do this in your templates where you embed your static resources. Depending on your application, you could probably use this Django plugin that should do this work for you.

更多推荐

javascript,部署,app,电脑培训,计算机培训,IT培训"/> <meta name="description

本文发布于:2023-07-04 10:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023044.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我将   缓存   新版本   文件上传   浏览器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!