Improve Spring Resource Handling support
This commit improves support of the Resource Handling features introduced in Spring Framework 4.1. Those features add new ways to resolve and transform static resources in applications. See [this blog post](https://spring.io/blog/2014/07/24/spring-framework-4-1-handling-static-web-resources) for more details. The `ResourceUrlEncodinFilter` is added for compatible template engines: Velocity and Thymeleaf. It assists them with rewriting the URLs of static resources when rendering templates. New keys are added in the `ResourceProperties` in order to configure the Resource Handling chain. `ResourceResolvers` and `ResourceTransformers` are registered accordingly in `WebMvcAutoConfiguration`. Here is an example of enabling a `ContentVersionStrategy` on all static resources, meaning their names will be changed for cache busting purposes by adding a content hash at the end of the file name. Like "/js/jquery.js -> /js/jquery-872ca6a9fdda9e2c1516a84cff5c3bc6.js". ``` spring.resources.chain.enabled:true spring.resources.chain.strategy.content.enabled:true spring.resources.chain.strategy.content.paths:/** ``` Closes gh-1604 Closes gh-3123
This commit is contained in:
committed by
Stephane Nicoll
parent
e34bdcdd9a
commit
dd561d15cf
@@ -129,6 +129,14 @@ content into your application; rather pick only the properties that you need.
|
||||
# SPRING RESOURCES HANDLING ({sc-spring-boot-autoconfigure}/web/ResourceProperties.{sc-ext}[ResourceProperties])
|
||||
spring.resources.cache-period= # cache timeouts in headers sent to browser
|
||||
spring.resources.add-mappings=true # if default mappings should be added
|
||||
spring.resources.chain.enabled=false # enable the Spring Resource Handling chain
|
||||
spring.resources.chain.cache=false # enable in-memory caching of resource resolution
|
||||
spring.resources.chain.html5AppCache=false # enable HTML5 appcache manifest rewriting
|
||||
spring.resources.chain.strategy.content.enabled=false # enable a content version strategy
|
||||
spring.resources.chain.strategy.content.paths= # comma-separated list of regular expression patterns to apply the version strategy to
|
||||
spring.resources.chain.strategy.fixed.enabled=false # enable a fixed version strategy
|
||||
spring.resources.chain.strategy.fixed.paths= # comma-separated list of regular expression patterns to apply the version strategy to
|
||||
spring.resources.chain.strategy.fixed.version= # version string to use for this version strategy
|
||||
|
||||
# MULTIPART ({sc-spring-boot-autoconfigure}/web/MultipartProperties.{sc-ext}[MultipartProperties])
|
||||
multipart.enabled=true
|
||||
|
||||
@@ -1183,6 +1183,51 @@ TIP: Do not use the `src/main/webapp` directory if your application will be pack
|
||||
jar. Although this directory is a common standard, it will *only* work with war packaging
|
||||
and it will be silently ignored by most build tools if you generate a jar.
|
||||
|
||||
Spring Boot also supports advanced resource handling features provided by Spring MVC,
|
||||
allowing use cases such as cache busting static resources or using version agnostic URLs
|
||||
for Webjars.
|
||||
|
||||
For example, the following configuration will configure a cache busting solution
|
||||
for all static resources, effectively adding a content hash in URLs, such as
|
||||
`<link href="/css/spring-2a2d595e6ed9a0b24f027f2b63b134d6.css"/>`:
|
||||
|
||||
[source,properties,indent=0,subs="verbatim,quotes,attributes"]
|
||||
----
|
||||
spring.resources.chain.enabled=true
|
||||
spring.resources.chain.strategy.content.enabled=true
|
||||
spring.resources.chain.strategy.content.paths=/**
|
||||
----
|
||||
|
||||
NOTE: Links to resources are rewritten at runtime in template, thanks to a
|
||||
`ResourceUrlEncodingFilter`, auto-configured for Thymeleaf and Velocity. You should
|
||||
manually declare this filter when using JSPs. Other template engines aren't automatically
|
||||
supported right now, but can be with custom template macros/helpers and the use of the
|
||||
{spring-javadoc}/web/servlet/resource/ResourceUrlProvider.{dc-ext}[`ResourceUrlProvider`].
|
||||
|
||||
When loading resources dynamically with, for example, a JavaScript module loader, renaming
|
||||
files is not an option. That's why other strategies are also supported and can be combined.
|
||||
A "fixed" strategy will add a static version string in the URL, without changing the file name:
|
||||
|
||||
[source,properties,indent=0,subs="verbatim,quotes,attributes"]
|
||||
----
|
||||
spring.resources.chain.enabled=true
|
||||
spring.resources.chain.strategy.content.enabled=true
|
||||
spring.resources.chain.strategy.content.paths=/**
|
||||
spring.resources.chain.strategy.fixed.enabled=true
|
||||
spring.resources.chain.strategy.fixed.paths=/js/lib/
|
||||
spring.resources.chain.strategy.fixed.version=v12
|
||||
----
|
||||
|
||||
With this configuration, JavaScript modules located under `"/js/lib/"` will use a fixed
|
||||
versioning strategy `"/v12/js/lib/mymodule.js"` while other resources will still use
|
||||
the content one `<link href="/css/spring-2a2d595e6ed9a0b24f027f2b63b134d6.css"/>`.
|
||||
|
||||
See {sc-spring-boot-autoconfigure}/web/ResourceProperties.{sc-ext}[`ResourceProperties`]
|
||||
for more of the supported options.
|
||||
|
||||
This feature has been thoroughly described in a dedicated
|
||||
https://spring.io/blog/2014/07/24/spring-framework-4-1-handling-static-web-resources[blog post]
|
||||
and in Spring Framework's {spring-reference}/#mvc-config-static-resources[reference documentation].
|
||||
|
||||
|
||||
[[boot-features-spring-mvc-template-engines]]
|
||||
|
||||
Reference in New Issue
Block a user