Skip to content

Commit 0377f6e

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
1 parent 383f196 commit 0377f6e

File tree

9 files changed

+3681
-782
lines changed

9 files changed

+3681
-782
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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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
@ConditionalOnProperty(prefix = "spring.devtools.livereload", name = "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,53 @@
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+
response.getWriter().write(this.scriptSnippet);
50+
}
51+
}
52+
53+
}

0 commit comments

Comments
 (0)