diff --git a/htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptor.java b/htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptor.java index 97355bd..aac1d75 100644 --- a/htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptor.java +++ b/htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptor.java @@ -8,6 +8,7 @@ import java.lang.reflect.Method; import org.springframework.core.annotation.AnnotatedElementUtils; +import org.springframework.http.HttpHeaders; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; @@ -23,11 +24,16 @@ public boolean preHandle(HttpServletRequest request, Method method = ((HandlerMethod) handler).getMethod(); setHxTrigger(response, method); setHxRefresh(response, method); + setVary(response, method); } return true; } + private void setVary(HttpServletResponse response, Method method) { + response.addHeader(HttpHeaders.VARY, HtmxRequestHeader.HX_REQUEST.getValue()); + } + private void setHxTrigger(HttpServletResponse response, Method method) { HxTrigger methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxTrigger.class); if (methodAnnotation != null) { diff --git a/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptorTest.java b/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptorTest.java index c963a4a..46cf59b 100644 --- a/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptorTest.java +++ b/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptorTest.java @@ -9,6 +9,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.hamcrest.collection.IsIterableContainingInRelativeOrder.containsInRelativeOrder; @WebMvcTest(TestController.class) @WithMockUser @@ -59,6 +60,13 @@ public void testAnnotationCompositionWithAliasFor() throws Exception { .andExpect(header().string("HX-Trigger", "updateTrigger")); } + @Test + public void testVary() throws Exception { + mockMvc.perform(get("/hx-vary")) + .andExpect(status().isOk()) + .andExpect(header().stringValues("Vary", containsInRelativeOrder("HX-Request"))); + } + @Test public void testHxRefresh() throws Exception { mockMvc.perform(get("/hx-refresh")) diff --git a/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/TestController.java b/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/TestController.java index bec130c..f4cc67e 100644 --- a/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/TestController.java +++ b/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/TestController.java @@ -58,4 +58,10 @@ public String hxRefresh() { return ""; } + @GetMapping("/hx-vary") + @ResponseBody + public String hxVary() { + return ""; + } + }