Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for LiveReload without browser extensions
Browse files Browse the repository at this point in the history
This commit improves Dev Tools live reload capabilities by adding
support for appending LiveReload.js script to rendered web pages.

See gh-32111

Signed-off-by: Vedran Pavic <vedran@vedranpavic.com>
vpavic committed Jan 9, 2025
1 parent 7900023 commit f1522bd
Showing 9 changed files with 3,718 additions and 781 deletions.
1 change: 1 addition & 0 deletions spring-boot-project/spring-boot-devtools/build.gradle
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@ dependencies {
optional("org.springframework:spring-jdbc")
optional("org.springframework:spring-orm")
optional("org.springframework:spring-web")
optional("org.springframework:spring-webmvc")
optional("org.springframework.security:spring-security-config")
optional("org.springframework.security:spring-security-web")
optional("org.springframework.data:spring-data-redis")
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2012-2025 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.
* You may obtain a copy of the License at
*
* https://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 org.springframework.boot.devtools.autoconfigure;

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.devtools.livereload.LiveReloadScriptFilter;
import org.springframework.boot.devtools.restart.RestartScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* Servlet-specific local LiveReload configuration.
*
* @author Vedran Pavic
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
class LiveReloadServletConfiguration {

@Bean
@RestartScope
LiveReloadScriptFilter liveReloadScriptFilter(DevToolsProperties properties) {
return new LiveReloadScriptFilter(properties.getLivereload().getPort());
}

@Configuration(proxyBeanMethods = false)
static class LiveReloadResourcesConfiguration implements WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
ResourceHandlerRegistration registration = registry.addResourceHandler("/livereload.js");
registration.addResourceLocations("classpath:/org/springframework/boot/devtools/livereload/");
}

}

}
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.GenericApplicationListener;
@@ -70,6 +71,7 @@ public class LocalDevToolsAutoConfiguration {
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnBooleanProperty(name = "spring.devtools.livereload.enabled", matchIfMissing = true)
@Import(LiveReloadServletConfiguration.class)
static class LiveReloadConfiguration {

@Bean
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2012-2025 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.
* You may obtain a copy of the License at
*
* https://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 org.springframework.boot.devtools.livereload;

import java.io.IOException;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.web.filter.OncePerRequestFilter;

/**
* A Servlet filter that appends LiveReload.js script to web pages.
*
* @author Vedran Pavic
* @since 3.5.0
*/
public class LiveReloadScriptFilter extends OncePerRequestFilter {

private final String scriptSnippet;

public LiveReloadScriptFilter(int liveReloadPort) {
this.scriptSnippet = String.format("<script src=\"/livereload.js?port=%d\"></script>", liveReloadPort);
}

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
filterChain.doFilter(request, response);
String contentType = response.getContentType();
if ((contentType != null) && MediaType.TEXT_HTML.isCompatibleWith(MediaType.parseMediaType(contentType))) {
try {
response.getWriter().write(this.scriptSnippet);
}
catch (IllegalStateException ex) {
// ignored
}
}
}

}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 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.
@@ -39,6 +39,7 @@
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
import org.springframework.boot.devtools.classpath.ClassPathChangedEvent;
import org.springframework.boot.devtools.classpath.ClassPathFileSystemWatcher;
import org.springframework.boot.devtools.livereload.LiveReloadScriptFilter;
import org.springframework.boot.devtools.livereload.LiveReloadServer;
import org.springframework.boot.devtools.restart.FailureHandler;
import org.springframework.boot.devtools.restart.MockRestartInitializer;
@@ -116,6 +117,7 @@ void liveReloadServer() throws Exception {
this.context = getContext(() -> initializeAndRun(Config.class));
LiveReloadServer server = this.context.getBean(LiveReloadServer.class);
assertThat(server.isStarted()).isTrue();
assertThat(this.context.getBean(LiveReloadScriptFilter.class)).isNotNull();
}

@Test
@@ -154,6 +156,8 @@ void liveReloadDisabled() throws Exception {
this.context = getContext(() -> initializeAndRun(Config.class, properties));
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> this.context.getBean(OptionalLiveReloadServer.class));
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> this.context.getBean(LiveReloadScriptFilter.class));
}

@Test
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2012-2025 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.
* You may obtain a copy of the License at
*
* https://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 org.springframework.boot.devtools.livereload;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import org.springframework.http.MediaType;
import org.springframework.mock.web.MockFilterChain;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link LiveReloadScriptFilter}.
*
* @author Vedran Pavic
*/
class LiveReloadScriptFilterTests {

@ParameterizedTest
@ValueSource(strings = { MediaType.TEXT_HTML_VALUE, "text/html; charset=utf-8" })
void givenHtmlCompatibleContentTypeThenResponseShouldContainScript(String contentType) throws Exception {
MockHttpServletResponse response = new MockHttpServletResponse();
response.getWriter().write("<html><head><title>test</title></head><body></body></html>");
response.setContentType(contentType);
LiveReloadScriptFilter filter = new LiveReloadScriptFilter(1234);
filter.doFilter(new MockHttpServletRequest(), response, new MockFilterChain());
assertThat(response.getContentAsString()).endsWith("<script src=\"/livereload.js?port=1234\"></script>");
}

@ParameterizedTest
@ValueSource(strings = { MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_PLAIN_VALUE })
void givenNonHtmlCompatibleContentTypeThenResponseShouldNotContainScript(String contentType) throws Exception {
MockHttpServletResponse response = new MockHttpServletResponse();
response.getWriter().write("{}");
response.setContentType(contentType);
LiveReloadScriptFilter filter = new LiveReloadScriptFilter(1234);
filter.doFilter(new MockHttpServletRequest(), response, new MockFilterChain());
assertThat(response.getContentAsString()).doesNotContain("<script src=\"/livereload.js?port=1234\"></script>");
}

@Test
void givenNoContentTypeThenResponseShouldNotContainScript() throws Exception {
MockHttpServletResponse response = new MockHttpServletResponse();
response.getWriter().write("test");
LiveReloadScriptFilter filter = new LiveReloadScriptFilter(1234);
filter.doFilter(new MockHttpServletRequest(), response, new MockFilterChain());
assertThat(response.getContentAsString()).doesNotContain("<script src=\"/livereload.js?port=1234\"></script>");
}

@Test
void givenResponseWriterAccessNotAllowedThenResponseShouldNotContainScript() throws Exception {
MockHttpServletResponse response = new MockHttpServletResponse();
response.setWriterAccessAllowed(false);
response.setContentType(MediaType.TEXT_HTML_VALUE);
LiveReloadScriptFilter filter = new LiveReloadScriptFilter(1234);
filter.doFilter(new MockHttpServletRequest(), response, new MockFilterChain());
assertThat(response.getContentAsString()).doesNotContain("<script src=\"/livereload.js?port=1234\"></script>");
}

}
Original file line number Diff line number Diff line change
@@ -284,8 +284,6 @@ If you find such a problem, you need to request a fix with the original authors.
== LiveReload

The `spring-boot-devtools` module includes an embedded LiveReload server that can be used to trigger a browser refresh when a resource is changed.
LiveReload browser extensions are freely available for Chrome, Firefox and Safari.
You can find these extensions by searching 'LiveReload' in the marketplace or store of your chosen browser.

If you do not want to start the LiveReload server when your application runs, you can set the configprop:spring.devtools.livereload.enabled[] property to `false`.

1 change: 1 addition & 0 deletions src/nohttp/suppressions.xml
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
<suppress files=".+\.(jar|git|ico|p12|gif|jks|jpg|svg)" checks="NoHttp" />
<suppress files="jquery.validate.js" checks="NoHttp" />
<suppress files="jquery-[0-9]\.[0-9]\.[0-9].js" checks="NoHttp" />
<suppress files="livereload.js" checks="NoHttp" />
<suppress files="[\\/]spring-boot-project.setup" checks="NoHttp" />
<suppress files="DockerHostTests\.java" checks="NoHttp" />
<suppress files="cyclonedx\.json" checks="NoHttp" />

0 comments on commit f1522bd

Please sign in to comment.