-
Notifications
You must be signed in to change notification settings - Fork 187
fix: Update link tag on stylesheet modification #22504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f384a69
Revert "feat: Watch the CSS files in the project's public static reso…
mshabarov 30572e8
fix: Update link tag on stylesheet modification
mshabarov f830a88
Merge branch 'main' into hot-reload-for-css-modifications
mshabarov e4025a7
take into account gradle path to build dir
mshabarov 5f82132
add unit tests
mshabarov 3c12c5d
Merge remote-tracking branch 'origin/hot-reload-for-css-modifications…
mshabarov 65ecfd5
Merge branch 'main' into hot-reload-for-css-modifications
mshabarov e694f6e
add method to PropertyDeploymentConfiguration
mshabarov 7740e3c
Merge remote-tracking branch 'origin/hot-reload-for-css-modifications…
mshabarov 869217f
fix maven/gradle detection, imports
mshabarov d9f9a4a
Merge branch 'main' into hot-reload-for-css-modifications
mshabarov a1405cf
Merge remote-tracking branch 'origin/main' into hot-reload-for-css-mo…
mshabarov e24dac3
Merge remote-tracking branch 'origin/hot-reload-for-css-modifications…
mshabarov fc55ffc
use all entries and swap the resource folder detection
mshabarov 008a09c
use util method
mshabarov db1204e
Merge branch 'main' into hot-reload-for-css-modifications
mshabarov 140e7b6
improve cache killer token, use replaceAll, refactorings
mshabarov 1b53372
Merge remote-tracking branch 'origin/hot-reload-for-css-modifications…
mshabarov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
flow-server/src/test/java/com/vaadin/flow/hotswap/HotswapperResourcesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Copyright 2000-2025 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.hotswap; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.file.Files; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import com.vaadin.flow.internal.BrowserLiveReload; | ||
import com.vaadin.flow.internal.BrowserLiveReloadAccessor; | ||
import com.vaadin.flow.server.MockVaadinServletService; | ||
import com.vaadin.flow.server.startup.ApplicationConfiguration; | ||
import com.vaadin.flow.server.startup.ApplicationConfigurationFactory; | ||
import com.vaadin.tests.util.MockDeploymentConfiguration; | ||
|
||
public class HotswapperResourcesTest { | ||
|
||
private MockVaadinServletService service; | ||
private BrowserLiveReload liveReload; | ||
private Hotswapper hotswapper; | ||
private File tempProjectDir; | ||
mcollovati marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
@Before | ||
public void setUp() throws IOException { | ||
MockDeploymentConfiguration dc = new MockDeploymentConfiguration(); | ||
// Create a temporary project directory, required for build resources | ||
// folder | ||
tempProjectDir = Files.createTempDirectory("vaadin-hotswap-test") | ||
.toFile(); | ||
dc.setProjectFolder(tempProjectDir); | ||
|
||
service = new MockVaadinServletService(dc); | ||
|
||
// Wire BrowserLiveReload into Lookup via BrowserLiveReloadAccessor | ||
liveReload = Mockito.mock(BrowserLiveReload.class); | ||
Mockito.when( | ||
service.getLookup().lookup(BrowserLiveReloadAccessor.class)) | ||
.thenReturn(context -> liveReload); | ||
|
||
ApplicationConfiguration appConfig = Mockito | ||
.mock(ApplicationConfiguration.class); | ||
Mockito.when(appConfig.isProductionMode()).thenAnswer( | ||
i -> service.getDeploymentConfiguration().isProductionMode()); | ||
Mockito.when(service.getLookup() | ||
.lookup(ApplicationConfigurationFactory.class)) | ||
.thenReturn(context -> appConfig); | ||
|
||
hotswapper = new Hotswapper(service); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
if (tempProjectDir != null) { | ||
deleteRecursively(tempProjectDir); | ||
caalador marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
private static void deleteRecursively(File f) throws IOException { | ||
if (f == null || !f.exists()) { | ||
return; | ||
} | ||
if (f.isDirectory()) { | ||
File[] children = f.listFiles(); | ||
if (children != null) { | ||
for (File c : children) { | ||
deleteRecursively(c); | ||
} | ||
} | ||
} | ||
Files.deleteIfExists(f.toPath()); | ||
} | ||
|
||
@Test | ||
public void cssResourceChange_triggersLiveReloadUpdateWithRelativePath() | ||
throws Exception { | ||
File buildResources = service.getDeploymentConfiguration() | ||
.getOutputResourceFolder(); | ||
// Mimic a static resources folder under build resources | ||
File publicDir = new File(buildResources, "public"); | ||
File css = new File(publicDir, "styles/app.css"); | ||
css.getParentFile().mkdirs(); | ||
Files.writeString(css.toPath(), "body{}\n"); | ||
|
||
URI modified = css.toURI(); | ||
hotswapper.onHotswap(new URI[0], new URI[] { modified }, new URI[0]); | ||
|
||
// Expect BrowserLiveReload.update to be called with relative URL path | ||
// "styles/app.css" | ||
Mockito.verify(liveReload).update("styles/app.css", null); | ||
Mockito.verifyNoMoreInteractions(liveReload); | ||
} | ||
|
||
@Test | ||
public void cssResourceChange_noLiveReloadAvailable_noCrash() | ||
throws Exception { | ||
// Create a new service without BrowserLiveReload in Lookup | ||
MockDeploymentConfiguration dc = new MockDeploymentConfiguration(); | ||
dc.setProjectFolder(tempProjectDir); | ||
MockVaadinServletService serviceNoLR = new MockVaadinServletService(dc); | ||
// Provide ApplicationConfiguration via factory to avoid NPE in | ||
// BrowserLiveReloadAccessor | ||
com.vaadin.flow.server.startup.ApplicationConfiguration appConfig = Mockito | ||
.mock(com.vaadin.flow.server.startup.ApplicationConfiguration.class); | ||
Mockito.when(appConfig.isProductionMode()).thenAnswer(i -> serviceNoLR | ||
.getDeploymentConfiguration().isProductionMode()); | ||
Mockito.when(serviceNoLR.getLookup().lookup( | ||
com.vaadin.flow.server.startup.ApplicationConfigurationFactory.class)) | ||
.thenReturn(context -> appConfig); | ||
|
||
Hotswapper noLR = new Hotswapper(serviceNoLR); | ||
|
||
File buildResources = serviceNoLR.getDeploymentConfiguration() | ||
.getOutputResourceFolder(); | ||
File staticDir = new File(buildResources, "static"); | ||
File css = new File(staticDir, "theme.css"); | ||
css.getParentFile().mkdirs(); | ||
Files.writeString(css.toPath(), "html{}\n"); | ||
|
||
// Should not throw even though live reload is not available; just logs | ||
noLR.onHotswap(new URI[0], new URI[] { css.toURI() }, new URI[0]); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note for a future improvement: we could refactor a bit of the code to add a
onResourceEvent(ResourceEvent event)
onVaadinHotswapper
interface and move the logic in this method into two separate classes (for styles and translations).