Skip to content

Commit 9e3d938

Browse files
authored
fix: eager load css from CssImport.themeFor (#17408)
* fix: eager load css from CssImport.themeFor CSS from CssImport.themeFor should be eagerly loaded, otherwise the framework tries to apply them after the web-component is already registered, resulting in an error in the browser. Although CssImport.themeFor is not recommended with Vaadin 24 it can still be used and in some cases it is required to work around some of the limitations mentioned in vaadin/docs#2552. This change forces CSS from CssImport.themeFor to be loaded eagerly. Fixes #17345 Fixes #17221 * Apply review suggestions
1 parent a108dae commit 9e3d938

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/frontend/AbstractUpdateImports.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ private Map<File, List<String>> process(Map<ChunkInfo, List<CssData>> css,
176176
}
177177

178178
for (Entry<ChunkInfo, List<CssData>> entry : css.entrySet()) {
179-
if (isLazyRoute(entry.getKey())) {
179+
boolean hasThemeFor = entry.getValue().stream()
180+
.anyMatch(cssData -> cssData.getThemefor() != null);
181+
if (isLazyRoute(entry.getKey()) && !hasThemeFor) {
180182
List<String> cssLines = getCssLines(entry.getValue());
181183
if (!cssLines.isEmpty()) {
182184
lazyCss.put(entry.getKey(), cssLines);

‎flow-server/src/test/java/com/vaadin/flow/server/frontend/AbstractUpdateImportsTest.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ public static class FooCssImport extends Component {
8787

8888
}
8989

90+
@Route(value = "themefor")
91+
@CssImport(value = "./foo.css", themeFor = "something")
92+
public static class ThemeForCssImport extends Component {
93+
94+
}
95+
9096
@Route(value = "simplecss")
9197
@CssImport("./bar.css")
9298
public static class BarCssImport extends Component {
@@ -460,6 +466,29 @@ public void eagerCssImportsMerged() throws Exception {
460466
output.get(flowGeneratedImports));
461467
}
462468

469+
@Test
470+
public void themeForCssImports_eagerLoaded() throws Exception {
471+
Class<?>[] testClasses = { ThemeForCssImport.class, UI.class };
472+
ClassFinder classFinder = getClassFinder(testClasses);
473+
updater = new UpdateImports(classFinder, getScanner(classFinder),
474+
options);
475+
updater.run();
476+
477+
Map<File, List<String>> output = updater.getOutput();
478+
479+
File flowGeneratedImports = FrontendUtils
480+
.getFlowGeneratedImports(frontendDirectory);
481+
482+
assertOnce("import { injectGlobalCss } from",
483+
output.get(flowGeneratedImports));
484+
assertOnce("import { css, unsafeCSS, registerStyles } from",
485+
output.get(flowGeneratedImports));
486+
assertOnce("from 'Frontend/foo.css?inline';",
487+
output.get(flowGeneratedImports));
488+
assertOnce("import $cssFromFile_0 from",
489+
output.get(flowGeneratedImports));
490+
}
491+
463492
protected void assertOnce(String key, List<String> output) {
464493
int found = 0;
465494
for (String row : output) {

0 commit comments

Comments
 (0)