Skip to content

Commit 0f73c64

Browse files
authored
Merge pull request #171 from xhaggi/feature/gh-170
Allow @HxRequest to ignore restore history requests and ignore them by default
2 parents 22fe2f3 + e1eacef commit 0f73c64

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxRequestMappingHandlerMapping.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ private RequestCondition<?> createCondition(HxRequest hxRequest) {
5555
conditions.add(new HeadersRequestCondition("!" + HX_BOOSTED.getValue()));
5656
}
5757

58+
if (!hxRequest.historyRestoreRequest()) {
59+
// exclude history restore requests by checking whether the header is absent
60+
conditions.add(new HeadersRequestCondition("!" + HX_HISTORY_RESTORE_REQUEST.getValue()));
61+
}
62+
5863
return new CompositeRequestCondition(conditions.toArray(RequestCondition[]::new));
5964
}
6065

htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxRequest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
*/
2222
boolean boosted() default true;
2323

24+
/**
25+
* Whether the mapping applies also for requests that have been made for history restoration.
26+
*
27+
* @see <a href="https://htmx.org/reference/#request_headers">HX-History-Restore-Request</a>
28+
* @since 4.1.0
29+
*/
30+
boolean historyRestoreRequest() default false;
31+
2432
/**
2533
* Restricts the mapping to the {@code id} of a specific target element.
2634
*

htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxRequestMappingHandlerMappingTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ void testHxRequestShouldIgnoreBoostedRequest() throws Exception {
4747
.andExpect(status().isNotFound());
4848
}
4949

50+
@Test
51+
void testHxRequestShouldHandleHistoryRestoreRequest() throws Exception {
52+
mockMvc.perform(get("/hx-request-handle-history-restore-request")
53+
.header(HX_REQUEST.getValue(), "true")
54+
.header(HX_HISTORY_RESTORE_REQUEST.getValue(), "true"))
55+
.andExpect(status().isOk())
56+
.andExpect(content().string("history-restore-request-handled"));
57+
}
58+
59+
@Test
60+
void testHxRequestShouldIgnoreHistoryRestoreRequest() throws Exception {
61+
mockMvc.perform(get("/hx-request")
62+
.header(HX_REQUEST.getValue(), "true")
63+
.header(HX_HISTORY_RESTORE_REQUEST.getValue(), "true"))
64+
.andExpect(status().isNotFound());
65+
}
66+
5067
@Test
5168
void testHxRequestTargetBar() throws Exception {
5269
mockMvc.perform(get("/hx-request-target")
@@ -155,6 +172,13 @@ public String hxRequestIgnoreBoosted() {
155172
return "boosted-ignored";
156173
}
157174

175+
@HxRequest(historyRestoreRequest = true)
176+
@GetMapping("/hx-request-handle-history-restore-request")
177+
@ResponseBody
178+
public String hxRequestHandleHistoryRestoreRequest() {
179+
return "history-restore-request-handled";
180+
}
181+
158182
@HxRequest(target = "bar")
159183
@GetMapping("/hx-request-target")
160184
@ResponseBody

0 commit comments

Comments
 (0)