大网数据广告活动
网站运营与故障问答

Reliability of using query strings to refresh cache

· 52CE 我爱测

I have seen a number of articles suggesting the use of a query string to force refresh of a cached file. For example, style.css becomes style.css?ver=134 , and you update the "version number" whenever you change the file. Are the…

问题原文

原帖链接 · CaptainCodeman · CC BY-SA 4.0

I have seen a number of articles suggesting the use of a query string to force refresh of a cached file. For example, style.css becomes style.css?ver=134, and you update the "version number" whenever you change the file.

Are there any potential problems with this method? If so, is there an alternative?

社区高分解答

原帖链接 · Maximillian Laumeister · CC BY-SA 4.0

This idea is called "cache busting", and there aren't any problems with your method that I'm aware of. It's a common, solid method that works reliably.

The other common alternative, of course, is to version the file path itself (e.g. /styles/v134/style.css or /styles/style-v134.css).

There's one final method that I've found useful in a niche circumstance: static site generators. If you have a static generated site that you don't update that often, you can have the site generator append the site's build time to URLs for all your versioned resources, e.g. /styles/style.css?t=(unix-time-stamp). This has the advantage of you not needing to manually update your file versions when each file changes, but has the disadvantage of cache busting ALL your resources every time you push an update.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2022-04-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。