-
Notifications
You must be signed in to change notification settings - Fork 40.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
9 changed files
with
3,718 additions
and
781 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
.../java/org/springframework/boot/devtools/autoconfigure/LiveReloadServletConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/"); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...ls/src/main/java/org/springframework/boot/devtools/livereload/LiveReloadScriptFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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))) { | ||
try { | ||
response.getWriter().write(this.scriptSnippet); | ||
} | ||
catch (IllegalStateException ex) { | ||
// ignored | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.