Serving Static Assets

Serving static assets #

Static assets refer to JavaScript, stylesheets, images, fonts, etc. that are used in your views.

By default, the web server is set up to serve static files from {diego.resourceBase}/public, {projectRoot}/public and {classpath}/public directories at/public/* .

Serving additional directories #

You can specify additional directories to serve in your application:

class MyApp extends WebMvcConfiguration {
    
    public void configure(WebMvcApplication app) {
        app.serve("/files/*", Paths.get("some/dir/on/disk"));
    }
}

The WebMvcApplication#serve method returns a StaticResourceHandler which you can configure further.

WebJars #

WebJars are served at /webjars/* and are cached in all environments for the maximum time allowed by the browser. There is no additional configuration required.

Configuration #

You can configure caching and other basic behaviours for the static file handler by modifying the appropriate section in the application.conf file:

# The URL prefix
diego.asset.path = "/public/*" 
# Caching is disabled by default. You should turn it on in production when you think your assets are finalized.
diego.assets.cache = false
# Sets the Cache-Control header. In the form of a duration such as 1h (1 hour), 30d (30 days), etc.
diego.assets.maxAge = "1h"

The default StaticResourceHandler sets a Cache-Control header of max-age={diego.assets.maxAge}, private.

Next: Forms