简体   繁体   English

Wicket:正确缓存 CSS 资源

[英]Wicket: proper caching of CSS resources

We just moved from GWT to Wicket 1.4.我们刚刚从 GWT 迁移到 Wicket 1.4。 While very happy in general, we still have a bit of a learning curve for certain things that GWT made simple.虽然总的来说很高兴,但对于 GWT 简化的某些事情,我们仍然有一些学习曲线。 One of those things is resource caching.其中之一是资源缓存。

How can I configure our app's resources (CSS in particular) so that they aren't downloaded with each page request?如何配置我们应用程序的资源(尤其是 CSS),以便它们不会随每个页面请求一起下载?

We're seeing some flickering on certain page elements because the styles aren't being applied immediately, presumably because the style sheets are being downloaded each time.我们在某些页面元素上看到一些闪烁,因为 styles 没有立即应用,大概是因为每次都在下载样式表。 I traced through our logs and indeed, I'm seeing requests for the stylesheets on nearly every page request.我跟踪了我们的日志,实际上,我几乎在每个页面请求中都看到了对样式表的请求。

We're referencing the CSS directly in our HTML files like so:我们在 HTML 文件中直接引用 CSS,如下所示:

    <link href="/css/ag.screen.css" rel="stylesheet" type="text/css">
    <link href="/css/ag.base.css" rel="stylesheet" type="text/css">

I'm not sure it matters, but all our pages are mounted with IndexedParamUrlCodingStrategy , as we need them to be both bookmarkable and have particular URL structures.我不确定这是否重要,但我们所有的页面都安装了IndexedParamUrlCodingStrategy ,因为我们需要它们既可收藏又具有特定的 URL 结构。

I tried implementing what Stefan Fußenegger suggests in his blog (ie referencing the CSS via a header contribution rather than in the HTML directly), but that didn't seem to help (I tried checking the headers with "curl -I").我尝试实现 Stefan Fußenegger 在他的博客中建议的内容(即通过 header 贡献引用 CSS,而不是在 HTML 中直接尝试),但似乎没有使用“-curl help”检查标题。

Is there a standard way of setting Cache-Control or Expiration headers for resources in Wicket 1.4?在 Wicket 1.4 中是否有为资源设置 Cache-Control 或 Expiration 标头的标准方法? Is it worth moving to Wicket 1.5 for this?值得为此迁移到 Wicket 1.5 吗? (I'm hesitant to do so because it's not officially released yet). (我犹豫是否这样做,因为它还没有正式发布)。

Edit: I just gave 1.5 another try, but the lack of documentation is really a roadblock at this point.编辑:我刚刚又尝试了 1.5,但目前缺乏文档确实是一个障碍。 IndexedParamUrlCodingStrategy and related classes are are gone, and the migration guide has almost nothing to say about it. IndexedParamUrlCodingStrategy和相关类都没有了,迁移指南几乎没有什么可说的。

Edit 2: I just noticed something very odd- when I arrive at one of my pages either by directly entering the URL, or by clicking a BookmarkablePageLink, the resources referenced on that page (CSS, images, etc) seem to get cached (I see no requests for them in my logs).编辑 2:我刚刚注意到一些非常奇怪的事情——当我通过直接输入 URL 或单击 BookmarkablePageLink 到达我的一个页面时,该页面上引用的资源(CSS、图像等)似乎被缓存了(我在我的日志中看不到他们的请求)。 But if I arrive at the same page via a form submission, all of the resources are downloaded again.但是如果我通过表单提交到达同一页面,所有资源都会再次下载。 Why does a form submission cause resources referenced on the page to be re-downloaded?为什么表单提交会导致页面上引用的资源被重新下载?

Since you are moving to Wicket I'd recommend you to use 1.5RC4.2.由于您要迁移到 Wicket,我建议您使用 1.5RC4.2。 Looking at the bug reports about this RC I think the next one may be 1.5.0Final.查看有关此 RC 的错误报告,我认为下一个可能是 1.5.0Final。 About caching see this wiki page: https://cwiki.apache.org/WICKET/caching-in-wicket-15.html .关于缓存,请参阅此 wiki 页面: https://cwiki.apache.org/WICKET/caching-in-wicket-15.html It is about 1.5 but it also says what are the differences against 1.4.它大约是 1.5,但它也说明了与 1.4 的区别。

I had the same need as you, the need to cache resource client side the more I can.我和你有同样的需求,我越需要缓存资源客户端。

For now I am stuck under 1.4.x and I found a solution that suits me.现在我被困在 1.4.x 下,我找到了适合我的解决方案。 I use the great library Stefan Fußenegger wrote and he made available here .我使用 Stefan Fußenegger 写的很棒的图书馆,他在这里提供。 Implementation is pretty straightforward, I use a StaticResourceVersionProvider in production mode.实现非常简单,我在生产模式下使用 StaticResourceVersionProvider。 I had no issues so far (rock solid for my clients for now).到目前为止我没有任何问题(现在对我的客户来说坚如磐石)。

Don't hesitate if you need details on how I use this solution.如果您需要有关我如何使用此解决方案的详细信息,请不要犹豫。

You get fingerprints on resources out-of-the-box.您可以获得开箱即用的资源指纹。 The strategy for creating cache fingerprints can be modified or altered quite easily.创建缓存指纹的策略可以很容易地修改或更改。

There's more information at the Wicket wiki page about how resource caching works in 1.5 . Wicket wiki 页面上提供了有关 1.5 中资源缓存如何工作的更多信息。

To manage the headers of non-Wicket resources you can use plain Servlet Filter that is listed before WicketFilter in your web.xml.要管理非 Wicket 资源的标头,您可以使用 web.xml 中 WicketFilter 之前列出的普通 Servlet 过滤器。 Just check the request Url and if the file extension is.css/.js/... then just set the cache headers in the response.只需检查请求 Url ,如果文件扩展名为 .css/.js/... 则只需在响应中设置缓存标头。 About **UrlCodingStrategies in Wicket 1.5 read http://wicketinaction.com/2011/07/wicket-1-5-request-mapper/ and the next two articles.关于 Wicket 1.5 中的 **UrlCodingStrategies,请阅读http://wicketinaction.com/2011/07/wicket-1-5-request-mapper/以及接下来的两篇文章。 I hope it will be more clear for you.我希望你会更清楚。

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

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