Skip to content

Commit c693b2b

Browse files
committed
Add support for webjars-locator-lite
This is a follow-up to spring-projects/spring-framework#27619 This commit adds support for "org.webjars:webjars-locator-lite" for enabling the statis resources chain. As of this commit, support for "org.webjars:webjars-locator-core" is deprecated for obvious performance reasons. Closes gh-40146
1 parent f8d06d5 commit c693b2b

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ConditionalOnEnabledResourceChain.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,10 @@
2727
/**
2828
* {@link Conditional @Conditional} that checks whether the Spring resource handling chain
2929
* is enabled. Matches if {@link WebProperties.Resources.Chain#getEnabled()} is
30-
* {@code true} or if {@code webjars-locator-core} is on the classpath.
30+
* {@code true} or if one of {@code "org.webjars:webjars-locator-core"},
31+
* {@code "org.webjars:webjars-locator-lite"} is on the classpath.
32+
* <p>
33+
* Note that support for {@code "org.webjars:webjars-locator-core"} is deprecated.
3134
*
3235
* @author Stephane Nicoll
3336
* @since 1.3.0

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/OnEnabledResourceChainCondition.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,12 +32,15 @@
3232
* @author Stephane Nicoll
3333
* @author Phillip Webb
3434
* @author Madhura Bhave
35+
* @author Brian Clozel
3536
* @see ConditionalOnEnabledResourceChain
3637
*/
3738
class OnEnabledResourceChainCondition extends SpringBootCondition {
3839

3940
private static final String WEBJAR_ASSET_LOCATOR = "org.webjars.WebJarAssetLocator";
4041

42+
private static final String WEBJAR_VERSION_LOCATOR = "org.webjars.WebJarVersionLocator";
43+
4144
@Override
4245
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
4346
ConfigurableEnvironment environment = (ConfigurableEnvironment) context.getEnvironment();
@@ -47,10 +50,13 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
4750
Boolean match = Chain.getEnabled(fixed, content, chain);
4851
ConditionMessage.Builder message = ConditionMessage.forCondition(ConditionalOnEnabledResourceChain.class);
4952
if (match == null) {
53+
if (ClassUtils.isPresent(WEBJAR_VERSION_LOCATOR, getClass().getClassLoader())) {
54+
return ConditionOutcome.match(message.found("class").items(WEBJAR_VERSION_LOCATOR));
55+
}
5056
if (ClassUtils.isPresent(WEBJAR_ASSET_LOCATOR, getClass().getClassLoader())) {
5157
return ConditionOutcome.match(message.found("class").items(WEBJAR_ASSET_LOCATOR));
5258
}
53-
return ConditionOutcome.noMatch(message.didNotFind("class").items(WEBJAR_ASSET_LOCATOR));
59+
return ConditionOutcome.noMatch(message.didNotFind("class").items(WEBJAR_VERSION_LOCATOR));
5460
}
5561
if (match) {
5662
return ConditionOutcome.match(message.because("enabled"));

spring-boot-project/spring-boot-dependencies/build.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,13 @@ bom {
22062206
]
22072207
}
22082208
}
2209+
library("WebJars Locator Lite", "1.0.0") {
2210+
group("org.webjars") {
2211+
modules = [
2212+
"webjars-locator-lite"
2213+
]
2214+
}
2215+
}
22092216
library("WebJars Locator Core", "0.59") {
22102217
group("org.webjars") {
22112218
modules = [

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/web/servlet.adoc

+1-4
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,10 @@ Although this directory is a common standard, it works *only* with war packaging
145145

146146
Spring Boot also supports the advanced resource handling features provided by Spring MVC, allowing use cases such as cache-busting static resources or using version agnostic URLs for Webjars.
147147

148-
To use version agnostic URLs for Webjars, add the `webjars-locator-core` dependency.
148+
To use version agnostic URLs for Webjars, add the `org.webjars:webjars-locator-lite` dependency.
149149
Then declare your Webjar.
150150
Using jQuery as an example, adding `"/webjars/jquery/jquery.min.js"` results in `"/webjars/jquery/x.y.z/jquery.min.js"` where `x.y.z` is the Webjar version.
151151

152-
NOTE: If you use JBoss, you need to declare the `webjars-locator-jboss-vfs` dependency instead of the `webjars-locator-core`.
153-
Otherwise, all Webjars resolve as a `404`.
154-
155152
To use cache busting, the following configuration configures a cache busting solution for all static resources, effectively adding a content hash, such as `<link href="/css/spring-2a2d595e6ed9a0b24f027f2b63b134d6.css"/>`, in URLs:
156153

157154
[configprops,yaml]

0 commit comments

Comments
 (0)