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

@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