Improve Spring Resource Handling support#3123
Conversation
|
Don't merge this right now! This must be discussed with @snicoll (and other Boot members).
Also, to enable URL rewriting in templates, you need your template engine to either:
This PR is configuring the filter for Thymeleaf and Velocity - but I don't think we can register it for JSP users without impacting everyone. Also, since SPR-12323, Spring Framework can now serve "version-agnostic" dependencies and/or rewrite "version agnostic" URLs in templates. This should make this part of webjars documentation for Spring much easier. But this requires to add the |
|
Found a few glitches (no need to fix that, we can manage that when merging). This looks good to me even though I think it may be good to explain a bit more what Are these mutually exclusive or can you use them at the same time? |
|
Thanks for the initial review - I've updated my PR. Now I'm (too) familiar with the Resource Handling features in Spring Framework and I'd like to make them really easy to understand and accessible in Boot apps. Version strategies explainedBasically, a The "content versioning" one calculates a hash of the content and include that hash in the file name, such as The "fixed versioning" adds a static version string that represents the application version; something like a git hash "d34db33f". This strategy is less optimal, since resources change URL even if they've not been modified. But this is essential as soon as those resources are loaded from a dynamic loader such as the ES6 module loader, or require.js. Concrete examplesBasically applications that don't use dynamic resource loaders (for example, angularJS 1.x apps) will use a content strategy for Applications that use resource loaders (such as the new ES6 module loader) will use a content strategy for css, images - but a fixed strategy for resources loaded by this module loader; see this example. So both can be used at the same time and IMO both make sense for modern applications. TODOI guess that we need to find proper props keys+descriptions that 1) refer to the real spring mvc resource handling features 2) still make sense in this context. Any idea? |
|
for the record, we've discussed how we could rework the configuration part to offer the following configuration: This provides a better flexibility to offer additional items in the future, if need to be. Note that we need to add |
e9628ca to
bcda586
Compare
|
I've updated this PR with those changes. To summarize my feelings about this PR:
What do you think @sdeleuze @rstoyanchev ? |
|
Overall seems to strike the right balance. A few comments and questions.
What's the exact consideration for this? I don't quite see the issue.
Could WebJarsResourceResolver be registered if webjars-locator is on the classpath?
Looking at this there is no hint of what resolver this is about, nor that it's for the same resolver. How about inserting |
Great, thanks!
Well the Boot team is asking developers to seriously reconsider the use of JSPs for good reasons. So registering the
Yes, it's already the case with Spring Framework itself; the question here is to add that dependency to a boot starter. Registering asset locations is free, but adding such a JAR to all Boot web applications is probably not the best solution.
I like it, it's more readable. spring:
resources:
chain:
enabled: true
cache: true
version:
content:
enabled: true
paths: /static/**
fixed:
enabled: true
version: 1.0
paths: /js/**,/foo/**Or this: spring:
resources:
chain:
enabled: true
cache: true
version.content:
enabled: true
paths: /static/**
version.fixed:
enabled: true
version: 1.0
paths: /js/**,/foo/**@snicoll Is this something usual, i.e. having "empty" keys for better readability? |
Okay that makes sense. Maybe the presence of an InternalResourceViewResolver bean could be a giveaway that JSP is in use? But it's not always straight forward to detect. For example it could be nested inside ContentNegotiatingViewResolver#getViewResovlers, or it could also be in ViewResolverComposite#getViewResolvers, or both. |
|
Good catch. |
42e5798 to
13f63a6
Compare
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:/** ``` Fixes spring-projects#1604
|
The PR looks good. I think that enabling one of the strategies should enable the chain if necessary. We can fix that with a I'll merge it early next week. Thanks! |
* pr/3123: Polish 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
for more details.
The
ResourceUrlEncodinFilteris 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
ResourcePropertiesin order to configurethe Resource Handling chain.
ResourceResolversandResourceTransformersare registered accordingly inWebMvcAutoConfiguration.If enabled with
"spring.resources.chain.enabled:true", the defaultconfiguration will add a
ContentVersionStrategyon all staticresources, meaning their names will be changed for cache busting
purposes by adding a content hash at the end of the file name.
Fixes: #1604