Wicket:正确缓存 CSS 资源

发布于 2024-11-09 11:24:11 字数 1229 浏览 3 评论 0 原文

我们刚刚从 GWT 迁移到 Wicket 1.4。虽然总的来说我们很高兴,但对于 GWT 简化的某些事情我们仍然有一些学习曲线。其中之一就是资源缓存。

如何配置我们应用的资源(特别是 CSS),以便它们不会随每个页面请求一起下载?

我们发现某些页面元素出现闪烁,因为样式没有立即应用,大概是因为每次都会下载样式表。我跟踪了我们的日志,确实,我在几乎每个页面请求中都看到了对样式表的请求。

我们直接在 HTML 文件中引用 CSS,如下所示:

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

我不确定这是否重要,但我们所有的页面都使用 IndexedParamUrlCodingStrategy 挂载,因为我们需要它们既可添加书签又具有特定的 URL结构。

我尝试实现 Stefan Fußenegger 在他的 blog (即通过标头贡献引用 CSS,而不是直接在 HTML 中引用),但这似乎没有帮助(我尝试使用“curl -I”检查标头)。

Wicket 1.4 中是否有为资源设置 Cache-Control 或 Expiration 标头的标准方法?为此值得迁移到 Wicket 1.5 吗? (我对此犹豫不决,因为它还没有正式发布)。

编辑:我刚刚再次尝试 1.5,但缺乏文档在这一点上确实是一个障碍。 IndexedParamUrlCodingStrategy 和相关的类都消失了,迁移指南几乎没有什么可说的。

编辑 2: 我刚刚注意到一些非常奇怪的事情 - 当我通过直接输入 URL 或单击 BookmarkablePageLink 到达其中一个页面时,该页面上引用的资源(CSS、图像等) )似乎被缓存(我在日志中没有看到对它们的请求)。但是,如果我通过表单提交到达同一页面,所有资源都会再次下载。为什么表单提交会导致页面引用的资源被重新下载?

We just moved from GWT to Wicket 1.4. While very happy in general, we still have a bit of a learning curve for certain things that GWT made simple. 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?

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. 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:

    <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.

I tried implementing what Stefan Fußenegger suggests in his blog (i.e. 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").

Is there a standard way of setting Cache-Control or Expiration headers for resources in Wicket 1.4? Is it worth moving to Wicket 1.5 for this? (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. IndexedParamUrlCodingStrategy and related classes are are gone, and the migration guide has almost nothing to say about it.

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). 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?

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

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

发布评论

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

评论(4

睡美人的小仙女 2024-11-16 11:24:11

由于您要迁移到 Wicket,我建议您使用 1.5RC4.2。看看有关此 RC 的错误报告,我认为下一个可能是 1.5.0Final。
关于缓存请参阅此 wiki 页面: https://cwiki.apache.org/ WICKET/caching-in-wicket-15.html
它大约是 1.5,但也说明了与 1.4 的区别。

Since you are moving to Wicket I'd recommend you to use 1.5RC4.2. Looking at the bug reports about this RC I think the next one may be 1.5.0Final.
About caching see this wiki page: 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.

夏末 2024-11-16 11:24:11

我和你有同样的需求,需要尽可能多地缓存客户端资源。

现在我陷入了 1.4.x 的困境,我找到了一个适合我的解决方案。
我使用 Stefan Fußenegger 编写的优秀库,他提供了 此处。实现非常简单,我在生产模式下使用 StaticResourceVersionProvider。到目前为止我没有遇到任何问题(目前对我的客户来说坚如磐石)。

如果您需要有关我如何使用此解决方案的详细信息,请不要犹豫。

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.
I use the great library Stefan Fußenegger wrote and he made available here. Implementation is pretty straightforward, I use a StaticResourceVersionProvider in production mode. 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.

一绘本一梦想 2024-11-16 11:24:11

要管理非 Wicket 资源的标头,您可以使用 web.xml 中 WicketFilter 之前列出的纯 Servlet Filter。只需检查请求 Url,如果文件扩展名是 .css/.js/...,则只需在响应中设置缓存标头即可。
关于 Wicket 1.5 中的 **UrlCodingStrategies,请阅读 http://wicketinaction.com/ 2011/07/wicket-1-5-request-mapper/ 以及接下来的两篇文章。我希望你会更清楚。

To manage the headers of non-Wicket resources you can use plain Servlet Filter that is listed before WicketFilter in your web.xml. Just check the request Url and if the file extension is .css/.js/... then just set the cache headers in the response.
About **UrlCodingStrategies in Wicket 1.5 read http://wicketinaction.com/2011/07/wicket-1-5-request-mapper/ and the next two articles. I hope it will be more clear for you.

神回复 2024-11-16 11:24:11

您可以获得开箱即用的资源指纹。创建缓存指纹的策略可以很容易地修改或更改。

有关详细信息,请访问 有关资源缓存在 1.5 中如何工作的 Wicket wiki 页面

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.

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