diff --git a/changes.xml b/changes.xml index 013943d..a076dbf 100644 --- a/changes.xml +++ b/changes.xml @@ -24,10 +24,10 @@ - + Do not include JS/CSS client libraries twice if the same library is requested multiple times within one request. - + Ensure request context path is added to client library URLs. diff --git a/src/main/java/io/wcm/wcm/ui/clientlibs/components/CSSInclude.java b/src/main/java/io/wcm/wcm/ui/clientlibs/components/CSSInclude.java index 5b9dd15..c6035bf 100644 --- a/src/main/java/io/wcm/wcm/ui/clientlibs/components/CSSInclude.java +++ b/src/main/java/io/wcm/wcm/ui/clientlibs/components/CSSInclude.java @@ -111,22 +111,13 @@ private void activate() { */ private @NotNull String buildIncludeString(@NotNull List libraryPaths, @NotNull Map attrs, @NotNull Map customAttrs) { - RequestIncludedLibraries includedLibraries = new RequestIncludedLibraries(request); - StringBuilder markup = new StringBuilder(); - for (String libraryPath : libraryPaths) { - // ignore libraries that are already included - if (includedLibraries.isInlucded(libraryPath)) { - continue; - } + return new RequestIncludedLibraries(request).buildMarkupIgnoringDuplicateLibraries(libraryPaths, libraryPath -> { HtmlTagBuilder builder = new HtmlTagBuilder("link", false, xssApi); builder.setAttrs(attrs); builder.setAttrs(customAttrs); builder.setAttr("href", request.getContextPath() + libraryPath); - markup.append(builder.build()); - // mark library as included - includedLibraries.storeIncluded(libraryPath); - } - return markup.toString(); + return builder; + }); } /** diff --git a/src/main/java/io/wcm/wcm/ui/clientlibs/components/JSInclude.java b/src/main/java/io/wcm/wcm/ui/clientlibs/components/JSInclude.java index 7c5a28c..089fc42 100644 --- a/src/main/java/io/wcm/wcm/ui/clientlibs/components/JSInclude.java +++ b/src/main/java/io/wcm/wcm/ui/clientlibs/components/JSInclude.java @@ -147,22 +147,13 @@ private void activate() { */ private @NotNull String buildIncludeString(@NotNull List libraryPaths, @NotNull Map attrs, @NotNull Map customAttrs) { - RequestIncludedLibraries includedLibraries = new RequestIncludedLibraries(request); - StringBuilder markup = new StringBuilder(); - for (String libraryPath : libraryPaths) { - // ignore libraries that are already included - if (includedLibraries.isInlucded(libraryPath)) { - continue; - } + return new RequestIncludedLibraries(request).buildMarkupIgnoringDuplicateLibraries(libraryPaths, libraryPath -> { HtmlTagBuilder builder = new HtmlTagBuilder("script", true, xssApi); builder.setAttrs(attrs); builder.setAttrs(customAttrs); builder.setAttr("src", request.getContextPath() + libraryPath); - markup.append(builder.build()); - // mark library as included - includedLibraries.storeIncluded(libraryPath); - } - return markup.toString(); + return builder; + }); } /** diff --git a/src/main/java/io/wcm/wcm/ui/clientlibs/components/RequestIncludedLibraries.java b/src/main/java/io/wcm/wcm/ui/clientlibs/components/RequestIncludedLibraries.java index fe8f107..22e9325 100644 --- a/src/main/java/io/wcm/wcm/ui/clientlibs/components/RequestIncludedLibraries.java +++ b/src/main/java/io/wcm/wcm/ui/clientlibs/components/RequestIncludedLibraries.java @@ -20,7 +20,9 @@ package io.wcm.wcm.ui.clientlibs.components; import java.util.HashSet; +import java.util.List; import java.util.Set; +import java.util.function.Function; import org.apache.sling.api.SlingHttpServletRequest; @@ -71,4 +73,27 @@ void storeIncluded(@NotNull String libraryPath) { getLibaryPathsSetFromRequest().add(libraryPath); } + /** + * Builds the markup for all given HTML libraries that are not already included in the current request. + * @param libraryPaths Library paths + * @param htmlTagBuilderFactory Factory to create HTML tag builders + * @return Markup + */ + String buildMarkupIgnoringDuplicateLibraries(@NotNull List libraryPaths, + @NotNull Function htmlTagBuilderFactory) { + RequestIncludedLibraries includedLibraries = new RequestIncludedLibraries(request); + StringBuilder markup = new StringBuilder(); + for (String libraryPath : libraryPaths) { + // ignore libraries that are already included + if (includedLibraries.isInlucded(libraryPath)) { + continue; + } + // build markup for library + markup.append(htmlTagBuilderFactory.apply(libraryPath).build()); + // mark library as included + includedLibraries.storeIncluded(libraryPath); + } + return markup.toString(); + } + }