有没有人知道在CSS文件中使用查询字符串的任何问题?

时间:2021-07-07 03:52:51

We're making changes to our main sprite and I'm debating the benefits of either changing its name completely or adding a query string to the end.

我们正在对我们的主精灵进行更改,并且我正在讨论完全更改其名称或在最后添加查询字符串的好处。

There's logic to keeping the old version to support Google cache, archive.com, etc., but it'd also be much cleaner on our system if I was to just edit the file and add a query string to the CSS image call:

保持旧版本支持Google缓存,archive.com等是有逻辑的,但如果我只是编辑文件并在CSS图像调用中添加查询字符串,它在我们的系统上也会更清晰:

#element-id { background-image: url('my-sprite.png?version1'); }

My question is, does anyone know of any browser issues with using a query string cache buster in a CSS file?

我的问题是,有没有人知道在CSS文件中使用查询字符串缓存破坏的任何浏览器问题?

My suspicion is that browsers handle css image requests the same way whether it's from CSS files or via HTML, so, so long as my server is expressing header information properly I should be OK.

我怀疑浏览器以相同的方式处理css图像请求,无论是来自CSS文件还是通过HTML,因此,只要我的服务器正确表达标题信息,我就应该没问题。

3 个解决方案

#1


1  

Unless the browser is seriously broken, there should be nothing wrong. Suppose you wanted to use a dynamic file, such as url('/layout.php?section=1') or something. Query strings are kind of required there, so if the browser didn't work it'd be broken quite badly.

除非浏览器严重破坏,否则应该没有错。假设您想使用动态文件,例如url('/ layout.php?section = 1')或其他东西。查询字符串在那里是必需的,所以如果浏览器不起作用,它将被打破得非常糟糕。

#2


20  

tl;dr Using query params is not a 100% solution.

tl; dr使用查询参数不是100%的解决方案。

There are basically two problems when using the asset pipeline:

使用资产管道时基本上存在两个问题:

  1. Making sure your resources get cached when you want them to
  2. 确保在需要时缓存资源
  3. Invalidating the cache when you rev the file.
  4. 在修改文件时使缓存无效。

Query string params will sometimes cause networks or browsers not to cache your resource at all. What's more as Mr. Irish points out "the query string approach is not reliable for clients behind a Squid Proxy Server" so it may me unreliable for busting the cache as well. Basically you don't want to have to rely on other peoples configuration.

查询字符串参数有时会导致网络或浏览器根本不缓存您的资源。更有意思的是,爱尔兰先生指出“查询字符串方法对于Squid代理服务器后面的客户端来说是不可靠的”,因此对于破坏缓存也是不可靠的。基本上你不想依赖其他人的配置。

A couple of references:

  • Rails Guides: Asset Pipeline - The Rails Asset Pipeline is built on Sprockets a project that has been focused on solving these types of problems for a good few years now. They specifically state in bold no less that "Not all caches will reliably cache content where the filename only differs by query parameters."

    Rails指南:资产管道 - Rails资产管道建立在Sprockets上,这个项目几年来一直专注于解决这些类型的问题。它们特别以粗体表示“并非所有缓存都能可靠地缓存文件名仅因查询参数而异的内容”。

  • Steve Soulders Article on revving assets - Steve Souders is something of a web performance guru and author of the O'Reilly book "High Performance Websites" wrote this article referenced to in the Rails guides that suggests using filename revving to avoid issues with people behind proxy servers.

    史蒂夫·索尔德斯(Steve Soulders)关于加速资产的文章 - 史蒂夫·索德斯(Steve Souders)是一位网络性能大师,奥莱利(O'Reilly)着作“高性能网站”(High Performance Websites)的作者写道,这篇文章在Rails指南中引用,建议使用文件名转换来避免代理人背后的问题服务器。

  • HTML5 Boilerplate Suggestion - The HTML5 Boilerplate project maintained by the venerable Paul Irish and Nicolas Gallagher use the .htaccess to essentially create a filter to do filename revving. They specifically suggest using filename revving in place of query string versions.

    HTML5 Boilerplate建议 - 由着名的Paul Irish和Nicolas Gallagher维护的HTML5 Boilerplate项目使用.htaccess来创建一个过滤器来进行文件名转换。他们特别建议使用文件名转换来代替查询字符串版本。

#3


0  

The only issue you may run into is css files tend to be cached more than you would probably want. If the output of my-sprite.png?version1 is going to change often, it would be better placed on the page itself.

您可能遇到的唯一问题是css文件往往比您可能想要的缓存更多。如果my-sprite.png?version1的输出经常发生变化,那么它最好放在页面本身上。

#1


1  

Unless the browser is seriously broken, there should be nothing wrong. Suppose you wanted to use a dynamic file, such as url('/layout.php?section=1') or something. Query strings are kind of required there, so if the browser didn't work it'd be broken quite badly.

除非浏览器严重破坏,否则应该没有错。假设您想使用动态文件,例如url('/ layout.php?section = 1')或其他东西。查询字符串在那里是必需的,所以如果浏览器不起作用,它将被打破得非常糟糕。

#2


20  

tl;dr Using query params is not a 100% solution.

tl; dr使用查询参数不是100%的解决方案。

There are basically two problems when using the asset pipeline:

使用资产管道时基本上存在两个问题:

  1. Making sure your resources get cached when you want them to
  2. 确保在需要时缓存资源
  3. Invalidating the cache when you rev the file.
  4. 在修改文件时使缓存无效。

Query string params will sometimes cause networks or browsers not to cache your resource at all. What's more as Mr. Irish points out "the query string approach is not reliable for clients behind a Squid Proxy Server" so it may me unreliable for busting the cache as well. Basically you don't want to have to rely on other peoples configuration.

查询字符串参数有时会导致网络或浏览器根本不缓存您的资源。更有意思的是,爱尔兰先生指出“查询字符串方法对于Squid代理服务器后面的客户端来说是不可靠的”,因此对于破坏缓存也是不可靠的。基本上你不想依赖其他人的配置。

A couple of references:

  • Rails Guides: Asset Pipeline - The Rails Asset Pipeline is built on Sprockets a project that has been focused on solving these types of problems for a good few years now. They specifically state in bold no less that "Not all caches will reliably cache content where the filename only differs by query parameters."

    Rails指南:资产管道 - Rails资产管道建立在Sprockets上,这个项目几年来一直专注于解决这些类型的问题。它们特别以粗体表示“并非所有缓存都能可靠地缓存文件名仅因查询参数而异的内容”。

  • Steve Soulders Article on revving assets - Steve Souders is something of a web performance guru and author of the O'Reilly book "High Performance Websites" wrote this article referenced to in the Rails guides that suggests using filename revving to avoid issues with people behind proxy servers.

    史蒂夫·索尔德斯(Steve Soulders)关于加速资产的文章 - 史蒂夫·索德斯(Steve Souders)是一位网络性能大师,奥莱利(O'Reilly)着作“高性能网站”(High Performance Websites)的作者写道,这篇文章在Rails指南中引用,建议使用文件名转换来避免代理人背后的问题服务器。

  • HTML5 Boilerplate Suggestion - The HTML5 Boilerplate project maintained by the venerable Paul Irish and Nicolas Gallagher use the .htaccess to essentially create a filter to do filename revving. They specifically suggest using filename revving in place of query string versions.

    HTML5 Boilerplate建议 - 由着名的Paul Irish和Nicolas Gallagher维护的HTML5 Boilerplate项目使用.htaccess来创建一个过滤器来进行文件名转换。他们特别建议使用文件名转换来代替查询字符串版本。

#3


0  

The only issue you may run into is css files tend to be cached more than you would probably want. If the output of my-sprite.png?version1 is going to change often, it would be better placed on the page itself.

您可能遇到的唯一问题是css文件往往比您可能想要的缓存更多。如果my-sprite.png?version1的输出经常发生变化,那么它最好放在页面本身上。