Skip to content

Commit 52e4a36

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 <[email protected]>
1 parent 8ce7da9 commit 52e4a36

File tree

9 files changed

+3776
-781
lines changed

9 files changed

+3776
-781
lines changed

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

Lines changed: 1 addition & 0 deletions
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.LiveReloadScriptInjectingFilter;
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+
LiveReloadScriptInjectingFilter liveReloadScriptInjectingFilter(DevToolsProperties properties) {
41+
return new LiveReloadScriptInjectingFilter(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

Lines changed: 2 additions & 0 deletions
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,72 @@
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+
import java.util.regex.Pattern;
22+
23+
import jakarta.servlet.FilterChain;
24+
import jakarta.servlet.ServletException;
25+
import jakarta.servlet.http.HttpServletRequest;
26+
import jakarta.servlet.http.HttpServletResponse;
27+
28+
import org.springframework.http.MediaType;
29+
import org.springframework.web.filter.OncePerRequestFilter;
30+
import org.springframework.web.util.ContentCachingResponseWrapper;
31+
32+
/**
33+
* A Servlet filter that appends LiveReload.js script to web pages.
34+
*
35+
* @author Vedran Pavic
36+
* @since 3.5.0
37+
*/
38+
public class LiveReloadScriptInjectingFilter extends OncePerRequestFilter {
39+
40+
private static final Pattern htmlHeadTagsPattern = Pattern
41+
.compile("<html(?![^>]*/>)[^>]*>(\\s+)?(<head(?![^>]*/>)[^>]*>)?", Pattern.CASE_INSENSITIVE);
42+
43+
private final String scriptElement;
44+
45+
public LiveReloadScriptInjectingFilter(int liveReloadPort) {
46+
this.scriptElement = String.format("<script src=\"/livereload.js?port=%d\"></script>", liveReloadPort);
47+
}
48+
49+
@Override
50+
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
51+
throws ServletException, IOException {
52+
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
53+
filterChain.doFilter(request, responseWrapper);
54+
if (shouldInjectScript(responseWrapper)) {
55+
String content = new String(responseWrapper.getContentAsByteArray(), StandardCharsets.UTF_8);
56+
String modifiedContent = htmlHeadTagsPattern.matcher(content)
57+
.replaceFirst((matchResult) -> matchResult.group() + this.scriptElement);
58+
if (!modifiedContent.equals(content)) {
59+
response.setContentLength(modifiedContent.length());
60+
response.getWriter().write(modifiedContent);
61+
return;
62+
}
63+
}
64+
responseWrapper.copyBodyToResponse();
65+
}
66+
67+
private boolean shouldInjectScript(HttpServletResponse response) {
68+
String contentType = response.getContentType();
69+
return (contentType != null) && MediaType.TEXT_HTML.isCompatibleWith(MediaType.parseMediaType(contentType));
70+
}
71+
72+
}

0 commit comments

Comments
 (0)