Skip to content

Commit

Permalink
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
  • Loading branch information
vpavic committed Jan 6, 2025
1 parent 383f196 commit 0377f6e
Show file tree
Hide file tree
Showing 9 changed files with 3,681 additions and 782 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
Expand Up @@ -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")
Expand Down
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
@@ -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.
Expand Down Expand Up @@ -44,6 +44,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;
Expand All @@ -69,6 +70,7 @@ public class LocalDevToolsAutoConfiguration {
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = "spring.devtools.livereload", name = "enabled", matchIfMissing = true)
@Import(LiveReloadServletConfiguration.class)
static class LiveReloadConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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))) {
response.getWriter().write(this.scriptSnippet);
}
}

}
Loading

0 comments on commit 0377f6e

Please sign in to comment.