Skip to content

Commit 551eb43

Browse files
committed
Add support for LiveReload without browser extensions
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>
1 parent ea89e18 commit 551eb43

File tree

9 files changed

+3728
-781
lines changed

9 files changed

+3728
-781
lines changed

spring-boot-project/spring-boot-devtools/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ dependencies {
4747
optional("org.springframework:spring-jdbc")
4848
optional("org.springframework:spring-orm")
4949
optional("org.springframework:spring-web")
50+
optional("org.springframework:spring-webmvc")
5051
optional("org.springframework.security:spring-security-config")
5152
optional("org.springframework.security:spring-security-web")
5253
optional("org.springframework.data:spring-data-redis")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.devtools.autoconfigure;
18+
19+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
21+
import org.springframework.boot.devtools.livereload.LiveReloadScriptFilter;
22+
import org.springframework.boot.devtools.restart.RestartScope;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration;
26+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
27+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
28+
29+
/**
30+
* Servlet-specific local LiveReload configuration.
31+
*
32+
* @author Vedran Pavic
33+
*/
34+
@Configuration(proxyBeanMethods = false)
35+
@ConditionalOnWebApplication(type = Type.SERVLET)
36+
class LiveReloadServletConfiguration {
37+
38+
@Bean
39+
@RestartScope
40+
LiveReloadScriptFilter liveReloadScriptFilter(DevToolsProperties properties) {
41+
return new LiveReloadScriptFilter(properties.getLivereload().getPort());
42+
}
43+
44+
@Configuration(proxyBeanMethods = false)
45+
static class LiveReloadResourcesConfiguration implements WebMvcConfigurer {
46+
47+
@Override
48+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
49+
ResourceHandlerRegistration registration = registry.addResourceHandler("/livereload.js");
50+
registration.addResourceLocations("classpath:/org/springframework/boot/devtools/livereload/");
51+
}
52+
53+
}
54+
55+
}

spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.context.ApplicationListener;
4545
import org.springframework.context.annotation.Bean;
4646
import org.springframework.context.annotation.Configuration;
47+
import org.springframework.context.annotation.Import;
4748
import org.springframework.context.annotation.Lazy;
4849
import org.springframework.context.event.ContextRefreshedEvent;
4950
import org.springframework.context.event.GenericApplicationListener;
@@ -69,6 +70,7 @@ public class LocalDevToolsAutoConfiguration {
6970
*/
7071
@Configuration(proxyBeanMethods = false)
7172
@ConditionalOnBooleanProperty(name = "spring.devtools.livereload.enabled", matchIfMissing = true)
73+
@Import(LiveReloadServletConfiguration.class)
7274
static class LiveReloadConfiguration {
7375

7476
@Bean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.devtools.livereload;
18+
19+
import java.io.IOException;
20+
import java.nio.charset.StandardCharsets;
21+
22+
import jakarta.servlet.FilterChain;
23+
import jakarta.servlet.ServletException;
24+
import jakarta.servlet.http.HttpServletRequest;
25+
import jakarta.servlet.http.HttpServletResponse;
26+
27+
import org.springframework.http.MediaType;
28+
import org.springframework.web.filter.OncePerRequestFilter;
29+
import org.springframework.web.util.ContentCachingResponseWrapper;
30+
31+
/**
32+
* A Servlet filter that appends LiveReload.js script to web pages.
33+
*
34+
* @author Vedran Pavic
35+
* @since 3.5.0
36+
*/
37+
public class LiveReloadScriptFilter extends OncePerRequestFilter {
38+
39+
private final String scriptSnippet;
40+
41+
public LiveReloadScriptFilter(int liveReloadPort) {
42+
this.scriptSnippet = String.format("<script src=\"/livereload.js?port=%d\"></script>", liveReloadPort);
43+
}
44+
45+
@Override
46+
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
47+
throws ServletException, IOException {
48+
ContentCachingResponseWrapper responseToUse = new ContentCachingResponseWrapper(response);
49+
filterChain.doFilter(request, responseToUse);
50+
String contentType = responseToUse.getContentType();
51+
if ((contentType != null) && MediaType.TEXT_HTML.isCompatibleWith(MediaType.parseMediaType(contentType))) {
52+
String content = new String(responseToUse.getContentAsByteArray(), StandardCharsets.UTF_8);
53+
String modifiedContent = content.replaceFirst("<head>", "<head>" + this.scriptSnippet);
54+
response.setContentLength(modifiedContent.length());
55+
response.getWriter().write(modifiedContent);
56+
}
57+
else {
58+
responseToUse.copyBodyToResponse();
59+
}
60+
}
61+
62+
}

0 commit comments

Comments
 (0)