Search code examples
javaannotationsstylesheettapestry

Customize Java Tapestry @IncludeStylesheet annotation


Using Tapestry 5, I'm looking at a (if possible) elegant way to customize the @IncludeStylesheet annotation in order to automatically add a version number to the style sheet file name.

For instance

  @IncludeStylesheet("context:/css/decoration.css")

would generate automatically the CSS inclusion with a version number

  <link href="/css/decoration.css?v=12345" rel="stylesheet" type="text/css">

Adding a new annotation, like @IncludeStylesheetVersion was my initial idea.
Looking at the Tapestry code, it looks like I'm ahead of some code copy / refactoring, with new implementations of RenderSupport and DocumentLinker (among others).

Would there be a simpler way to proceed?


Solution

  • You may not have to do that at all: Asset versioning is built into Tapestry. The default behaviour, according to the docs:

    Tapestry creates a new URL for assets (whether context or classpath). The URL is of the form /assets/application version number/folder/path.

    • application version number: Defined by symbol tapestry.application-version, the default value is a random hex number.

    If you just need to make sure clients will see changed stylesheets after an update and you don't restart your app all the time, the default behavior (random hex number) should do just fine.

    Edit based on comment: This is also available in 5.1:

    • app/app-version (for assets obtained at or beneath the application package)
    • classpath/app-version (for assets obtained from any otherwise
      unmapped package)

    Where version is the Tapestry framework version, and app-version is the application version (which will be a random string if not explicitly configured).

    Just check the URLs Tapestry generates for your CSS files, they should look like this: http://myserver.com/assets/ctx/942f15f778dca26c/css/styles.css