Skip to content

Instantly share code, notes, and snippets.

@remibantos
Last active August 31, 2021 14:31
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save remibantos/5e86829e1ba6ad64eea1 to your computer and use it in GitHub Desktop.
Save remibantos/5e86829e1ba6ad64eea1 to your computer and use it in GitHub Desktop.
Wildfly 8 - HTTP cache headers tuning for rich client web application

Introduction

This Gist describes how to tune HTTP browser cache expiration for static contents served by Wildfly Undertow web server, as per RFC-2616 section 13.

Wildfly configuration

Description

In order to change HTTP browser cache behavior, a "Cache-Control" HTTP header has to be added to static content HTTP responses returned by Undertow.

Undertow subsystem configuration (standalone.xml)

NOTE: "..." means existing xml config elements to be kept

<subsystem xmlns="urn:jboss:domain:undertow:1.2">

  ...

  <server name="default-server">

    ...

    <host name="default-host" alias="localhost">
        ...
        <filter-ref name="custom-max-age" predicate="path-suffix['.js'] or path-suffix ['.json'] or path-suffix ['.html'] or path-suffix ['.css'] or path-suffix ['.jpg'] or path-suffix ['.jpeg'] or path-suffix ['.png'] or path-suffix ['.gif']"/>
    </host>
  </server>

  ...

  <filters>

      ...
      
      <response-header name="custom-max-age" header-name="Cache-Control" header-value="max-age=600, public"/>
  </filters>
</subsystem>

Description

A Cache-Control header will be added to HTTP responses header for static files having extensions: ".js, .json, .html, .css, .jpg, .jpeg, .png, .gif". (Complete list of handlers predicates, such as "path-suffix", is availaible here)

This header has a "max-age=600, public" value which tells to browser to expire these static content files after 600 seconds, as described in RFC-2616 section 14.9.3. (see this other section for public value description)

Links

@danhooper
Copy link

I came across this page when looking for setting cache headers. The predicate clause was hard to find documentation on, I found this page which explains a lot more about the predicates:
http://undertow.io/undertow-docs/undertow-docs-1.2.0/predicates-attributes-handlers.html

@remibantos
Copy link
Author

remibantos commented Jan 23, 2016

@danhooper, thanks for the link, indeed, seems that you can do many things with handlers predicates

@tggm
Copy link

tggm commented Feb 16, 2017

This doesn't seem to work. As far as I can understand, these days we need to add an ETAG tag also. Could you update the example with this tag, please ?

@remibantos
Copy link
Author

remibantos commented Mar 9, 2017

@tggm. I had tested this setting with Wildfly 8.
From what I understand with ETAG the browser does not request the resource automatically after its expiration if the fingerprint is still the same. So it's not the same caching behavior as with max-age header setting described here. See this article.

@benze
Copy link

benze commented Sep 27, 2017

@remibantos Do you know if there is a way to set a default cache-filter as well? For example, to have a default set a No-Cache EXCEPT for the static content indicated in your predicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment