Skip to content

Commit f1522bd

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 7900023 commit f1522bd

File tree

9 files changed

+3718
-781
lines changed

9 files changed

+3718
-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.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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.context.ApplicationListener;
4646
import org.springframework.context.annotation.Bean;
4747
import org.springframework.context.annotation.Configuration;
48+
import org.springframework.context.annotation.Import;
4849
import org.springframework.context.annotation.Lazy;
4950
import org.springframework.context.event.ContextRefreshedEvent;
5051
import org.springframework.context.event.GenericApplicationListener;
@@ -70,6 +71,7 @@ public class LocalDevToolsAutoConfiguration {
7071
*/
7172
@Configuration(proxyBeanMethods = false)
7273
@ConditionalOnBooleanProperty(name = "spring.devtools.livereload.enabled", matchIfMissing = true)
74+
@Import(LiveReloadServletConfiguration.class)
7375
static class LiveReloadConfiguration {
7476

7577
@Bean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
21+
import jakarta.servlet.FilterChain;
22+
import jakarta.servlet.ServletException;
23+
import jakarta.servlet.http.HttpServletRequest;
24+
import jakarta.servlet.http.HttpServletResponse;
25+
26+
import org.springframework.http.MediaType;
27+
import org.springframework.web.filter.OncePerRequestFilter;
28+
29+
/**
30+
* A Servlet filter that appends LiveReload.js script to web pages.
31+
*
32+
* @author Vedran Pavic
33+
* @since 3.5.0
34+
*/
35+
public class LiveReloadScriptFilter extends OncePerRequestFilter {
36+
37+
private final String scriptSnippet;
38+
39+
public LiveReloadScriptFilter(int liveReloadPort) {
40+
this.scriptSnippet = String.format("<script src=\"/livereload.js?port=%d\"></script>", liveReloadPort);
41+
}
42+
43+
@Override
44+
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
45+
throws ServletException, IOException {
46+
filterChain.doFilter(request, response);
47+
String contentType = response.getContentType();
48+
if ((contentType != null) && MediaType.TEXT_HTML.isCompatibleWith(MediaType.parseMediaType(contentType))) {
49+
try {
50+
response.getWriter().write(this.scriptSnippet);
51+
}
52+
catch (IllegalStateException ex) {
53+
// ignored
54+
}
55+
}
56+
}
57+
58+
}

0 commit comments

Comments
 (0)