在 Subversion 中更改文件时更新资源 (CSS/JS) 路径

发布于 2024-10-04 11:04:40 字数 293 浏览 0 评论 0原文

有时,我会收到使用我网站的用户关于布局怪异的报告,答案往往是,使用 Ctrl-F5 刷新浏览器/清除缓存。

我认为处理此问题的最简单方法是将 SVN 修订版附加到像 styles.css?1234 这样的资源路径,但我想知道这是否有点严厉(例如 SVN 修订版不更改资源将强制进行不必要的获取)。此外,它还需要遍历并找到所有这些路径并添加一些代码来输出 SVN 修订版。

有没有更好/标准的方法来处理这个问题?

就其价值而言,我使用的是 Django,以及带有 mod_wsgi 的 Apache。

Occasionally I get reports of layout quirks from people using my sites, and the answer tends to be, refresh your browser with Ctrl-F5/clear your cache.

The easiest way I can think to handle this is to append the SVN revision to paths to resources like this styles.css?1234, but I wonder if this is a bit heavy-handed (e.g. SVN revisions that don't change the resource will force an unnecessary fetch). Also, it requires going through and finding all these paths and adding some code to output the SVN revision.

Is there a better/standard way to handle this?

For what it's worth, I'm using Django, and Apache with mod_wsgi.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哆兒滾 2024-10-11 11:04:41

我过去使用过的一项技术是确保对每个静态资源引用使用模板标签。然后,我可以根据需要更改该标签,如果我需要更改资产目录的 URL,也可以根据需要添加缓存清除。

即使文件没有更改,我也不会担心对每个 SVN 修订版强制进行不必要的获取 - 它仍然比每次提供新文件更有效。

有各种库可以为您做此类事情。 knutin已经提到了django-compress,还有django-static-managementdjango-static 以及可能还有很多其他的。除了处理缓存清除之外,这些应用程序通常还包括某种压缩/串联,以便将多个 CSS 或 JS 文件串联成一个,以减少 HTTP 请求。

One technique I've used in the past is to ensure I use a template tag for every static asset reference. Then I can change that tag as required, both if I need to change the URL to the asset directories, but also so I can add cachebusting as required.

I wouldn't worry about forcing an unnecessary fetch for each SVN revision even if the file hasn't changed - it's still more efficient than serving a new one each time.

There are various libraries that do this sort of thing for you. knutin has already mentioned django-compress, there's also django-static-management, django-static and probably lots of others. In addition to dealing with cachebusting, these apps usually include some sort of compression/concatenation so that multiple CSS or JS files are concatenated into one to reduce HTTP requests.

日裸衫吸 2024-10-11 11:04:40

最好使用文件的最后修改时间戳。这样您就可以避免因版本号而遇到的问题。您还必须在每个请求时访问磁盘以获取最新修订号(如果使用此方法,请使用缓存),或者在部署时在“编译”步骤中更改包含语句。

另外,如果您执行 styles.css?123 ,无论过期标头如何,某些代理/浏览器可能根本不会缓存该文件。最好使用 styles.123.css,所有代理都将其视为单独的文件。

有一个项目已经解决了这个问题,我自己使用它取得了巨大的成功:django-压缩。你告诉它你想如何压缩你的 javascript 和 css,然后它会缩小/压缩,将它们连接到一个大文件中,并给这个大文件一个包含时间戳的特殊文件名。您还可以获得一个在包含文件时使用的模板标记,该标记知道要包含哪个文件。

Django 1.3 还将包含一个名为 staticfiles 的应用程序,它有助于提供服务静态媒体,但它并没有解决版本控制的问题。

It would be better to use the last modification timestamp of the file. Then you would avoid the problems you would run into with the version number. You would also either have to go to disk on every request to get the latest revision number(use cache if using this approach) or change your include statements in a "compile" step when deploying.

Also, if you do styles.css?123 some proxies/browsers might not cache the file at all regardless of expiration headers. It is better to do styles.123.css, which all agents treat as a separate file.

There is a project that already solves this problem, which I've used myself to great success: django-compress. You tell it how you want to compress your javascript and css, then it will minify/compress, join them together into one big file and give the big file a special filename that includes the timestamp. You also get a template tag to use when including the files that knows which file to include.

Django 1.3 will also include an app called staticfiles which helps with serving up static media, but it does not solve the problem of versioning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文