Explicat
Explicat

Reputation: 1095

Disable caching of static assets in Dropwizard

I have a Dropwizard webserver with a rest api which also serves some static content like html, css, javascript and jpg images. Unfortunately, when I change the html or add another image, the server always needs to be restarted to turn changes into effect.

As I thought that it might be a problem of caching, I explored bazaarvoice's Configurable Assets Bundle.

That's what I added to the configuration class:

@Valid
@NotNull
@JsonProperty
private final AssetsConfiguration assets = new AssetsConfiguration();

And in the main class

@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
    // ...
    CacheBuilderSpec cacheBuilderSpec = CacheBuilderSpec.disableCaching();
    bootstrap.addBundle(new ConfiguredAssetsBundle("/html", cacheBuilderSpec, "/", "index.html", "Static assets"));
}

@Override
public void run(MyConfiguration config, Environment env) {
    env.jersey().setUrlPattern("/api/*");
    // ...
}

No changes in the yaml configuration.

The static files are located in src/main/resources/html. How can caching be disabled such that Dropwizard shows changes instantly?

And second question, how can I make Dropwizard follow symbolic links from the assets directory?

Update

I found this in the ConfiguredAssetsBundle source:

// Let the cache spec from the configuration override the one specified in the code
CacheBuilderSpec spec = (config.getCacheSpec() != null)
    ? CacheBuilderSpec.parse(config.getCacheSpec())
    : cacheBuilderSpec;

This certainly overrides the cache builder spec which was set in the code with the configuration from the yaml file. After append

assets:
  cacheSpec: maximumSize=0

to the configuration, the debugger shows that maximum size is now 0. However, behaviour did not change.

Upvotes: 4

Views: 1563

Answers (1)

Can Bican
Can Bican

Reputation: 464

Static content doesn't change not because you need to restart, but because the running server actually serves files under the target directory. Changing the files in this directory only confuses things (so it's not really a solution), but change a few lines and wait a second to verify that the server now serves the modified file with no need to restart.

As a solution, I prefer opening the dropwizard project as a maven project in eclipse and run the project with mvn exec:java on a terminal, using the exec-maven-plugin. Eclipse updates the target directory as files change, but it takes a few seconds, depending on the size of the project.

Upvotes: 1

Related Questions