Skip to content

Commit 127b2ec

Browse files
committed
Add caching headers to unmodified static resources
per https://www.rfc-editor.org/rfc/rfc7232#section-4.1 The server generating a 304 response MUST generate any of the following header fields that would have been sent in a 200 (OK) response to the same request: Cache-Control, Content-Location, Date, ETag, Expires, and Vary. Signed-off-by: James Yuzawa <[email protected]>
1 parent a0763d1 commit 127b2ec

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-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.
@@ -538,6 +538,9 @@ public void handleRequest(HttpServletRequest request, HttpServletResponse respon
538538
// Supported methods and required session
539539
checkRequest(request);
540540

541+
// Apply cache settings, if any
542+
prepareResponse(response);
543+
541544
// Header phase
542545
String eTagValue = (this.getEtagGenerator() != null) ? this.getEtagGenerator().apply(resource) : null;
543546
long lastModified = (this.isUseLastModified()) ? resource.lastModified() : -1;
@@ -546,9 +549,6 @@ public void handleRequest(HttpServletRequest request, HttpServletResponse respon
546549
return;
547550
}
548551

549-
// Apply cache settings, if any
550-
prepareResponse(response);
551-
552552
// Check the media type for the resource
553553
MediaType mediaType = getMediaType(request, resource);
554554
setHeaders(response, resource, mediaType);

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

+4
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,13 @@ void configureVersionResourceResolver() throws Exception {
465465

466466
@Test
467467
void shouldRespondWithNotModifiedWhenModifiedSince() throws Exception {
468+
this.handler.setCacheSeconds(3600);
468469
this.handler.afterPropertiesSet();
469470
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
470471
this.request.addHeader("If-Modified-Since", resourceLastModified("test/foo.css"));
471472
this.handler.handleRequest(this.request, this.response);
472473
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_MODIFIED);
474+
assertThat(this.response.getHeader("Cache-Control")).isEqualTo("max-age=3600");
473475
}
474476

475477
@Test
@@ -484,12 +486,14 @@ void shouldRespondWithModifiedResource() throws Exception {
484486

485487
@Test
486488
void shouldRespondWithNotModifiedWhenEtag() throws Exception {
489+
this.handler.setCacheSeconds(3600);
487490
this.handler.setEtagGenerator(resource -> "testEtag");
488491
this.handler.afterPropertiesSet();
489492
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
490493
this.request.addHeader("If-None-Match", "\"testEtag\"");
491494
this.handler.handleRequest(this.request, this.response);
492495
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_MODIFIED);
496+
assertThat(this.response.getHeader("Cache-Control")).isEqualTo("max-age=3600");
493497
}
494498

495499
@Test

0 commit comments

Comments
 (0)