简体   繁体   English

使用快速CDN将动态CSS URL放在HTTP标头中

[英]Putting dynamic CSS URLs in HTTP headers with Fastly CDN

I'm generating dynamic CSS URLs for cache-busting. 我正在生成用于缓存清除的动态CSS URL。 Ie they're in the format styles-thisisthecontenthash123.css . 即它们采用styles-thisisthecontenthash123.css格式。

I also want to use HTTP Link headers to load the files slightly faster. 我还想使用HTTP Link标头来稍微更快地加载文件。 Ie have the header Link: <styles-thisisthecontenthash123.css>; rel=stylesheet 即具有标题Link: <styles-thisisthecontenthash123.css>; rel=stylesheet Link: <styles-thisisthecontenthash123.css>; rel=stylesheet

I'm pretty sure it's possible to do this in Fastly using VCL, but I'm not familiar enough with the ecosystem to figure it out. 我很确定可以在使用VCL的Fastly中做到这一点,但是我对生态系统并不十分了解。 The CSS URL is in index.html , which is cached. CSS URL在index.html ,已缓存。 I'm thinking I can open index.html and maybe use regex to parse out the CSS URL. 我想我可以打开index.html ,也许可以使用正则表达式来解析CSS URL。 How would I do this? 我该怎么做?

If I'm understanding your question correctly, you want to include a link header for all requests for index.html . 如果我正确理解了您的问题,则希望为所有对index.html请求都包含一个link标题。 You can do that with Fastly, but if the URL for the CSS file is changing you're not going to be able to pull that info out with VCL (you can't inspect the response body). 您可以使用Fastly做到这一点,但是如果CSS文件的URL发生更改,您将无法使用VCL提取该信息(您无法检查响应正文)。

You could use edge dictionaries and whenever your CSS filename changes, update the reference via the API. 您可以使用边缘字典,并且只要CSS文件名更改,就可以通过API更新参考。

Thing is, if you're going to make an API call whenever the file changes, might as well just keep the filename consistent ( styles.css ) and whenever you publish a new version send a cache invalidation (purge). 事实是,如果要在文件更改时进行API调用,则最好保持文件名的一致性( styles.css ),并且每当发布新版本时都发送缓存无效 (清除)。 Fastly will clear the cache in ~150ms, so you then all you have to do is add the header which is can be done in the Fastly web portal with a condition . Fastly将在约150ms内清除缓存,因此您要做的就是添加标头,这可以在带有条件的Fastly Web门户中完成。

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

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