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
This commit is contained in:
Brian Clozel 2024-07-12 19:17:17 +02:00
parent f8d06d568b
commit c693b2bd8c
4 changed files with 21 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -27,7 +27,10 @@ import org.springframework.context.annotation.Conditional;
/**
* {@link Conditional @Conditional} that checks whether the Spring resource handling chain
* is enabled. Matches if {@link WebProperties.Resources.Chain#getEnabled()} is
* {@code true} or if {@code webjars-locator-core} is on the classpath.
* {@code true} or if one of {@code "org.webjars:webjars-locator-core"},
* {@code "org.webjars:webjars-locator-lite"} is on the classpath.
* <p>
* Note that support for {@code "org.webjars:webjars-locator-core"} is deprecated.
*
* @author Stephane Nicoll
* @since 1.3.0

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -32,12 +32,15 @@ import org.springframework.util.ClassUtils;
* @author Stephane Nicoll
* @author Phillip Webb
* @author Madhura Bhave
* @author Brian Clozel
* @see ConditionalOnEnabledResourceChain
*/
class OnEnabledResourceChainCondition extends SpringBootCondition {
private static final String WEBJAR_ASSET_LOCATOR = "org.webjars.WebJarAssetLocator";
private static final String WEBJAR_VERSION_LOCATOR = "org.webjars.WebJarVersionLocator";
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
ConfigurableEnvironment environment = (ConfigurableEnvironment) context.getEnvironment();
@ -47,10 +50,13 @@ class OnEnabledResourceChainCondition extends SpringBootCondition {
Boolean match = Chain.getEnabled(fixed, content, chain);
ConditionMessage.Builder message = ConditionMessage.forCondition(ConditionalOnEnabledResourceChain.class);
if (match == null) {
if (ClassUtils.isPresent(WEBJAR_VERSION_LOCATOR, getClass().getClassLoader())) {
return ConditionOutcome.match(message.found("class").items(WEBJAR_VERSION_LOCATOR));
}
if (ClassUtils.isPresent(WEBJAR_ASSET_LOCATOR, getClass().getClassLoader())) {
return ConditionOutcome.match(message.found("class").items(WEBJAR_ASSET_LOCATOR));
}
return ConditionOutcome.noMatch(message.didNotFind("class").items(WEBJAR_ASSET_LOCATOR));
return ConditionOutcome.noMatch(message.didNotFind("class").items(WEBJAR_VERSION_LOCATOR));
}
if (match) {
return ConditionOutcome.match(message.because("enabled"));

View File

@ -2206,6 +2206,13 @@ bom {
]
}
}
library("WebJars Locator Lite", "1.0.0") {
group("org.webjars") {
modules = [
"webjars-locator-lite"
]
}
}
library("WebJars Locator Core", "0.59") {
group("org.webjars") {
modules = [

View File

@ -145,13 +145,10 @@ Although this directory is a common standard, it works *only* with war packaging
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.
To use version agnostic URLs for Webjars, add the `webjars-locator-core` dependency.
To use version agnostic URLs for Webjars, add the `org.webjars:webjars-locator-lite` dependency.
Then declare your Webjar.
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.
NOTE: If you use JBoss, you need to declare the `webjars-locator-jboss-vfs` dependency instead of the `webjars-locator-core`.
Otherwise, all Webjars resolve as a `404`.
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:
[configprops,yaml]