Skip to content

Commit

Permalink
[rest] Add no-cache directive to cached REST responses (#3970)
Browse files Browse the repository at this point in the history
Fixes openhab/openhab-webui#2102.

This forces the browser to revalidate the cache every time it is accessed to ensure the data is always fresh.
See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#directives.

This is approach is also suggested in the mdn web docs, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#up-to-date_contents_always.

Signed-off-by: Florian Hotze <[email protected]>
  • Loading branch information
florian-h05 authored Dec 27, 2023
1 parent b08a01c commit f5bd7f9
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public Response get(@Context SecurityContext securityContext, @Context Request r
.map(rule -> EnrichedRuleDTOMapper.map(rule, ruleManager, managedRuleProvider));

CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMustRevalidate(true);
cc.setPrivate(true);
rules = dtoMapper.limitToFields(rules, "uid,templateUID,name,visibility,description,tags,editable");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ public Response getItems(final @Context UriInfo uriInfo, final @Context HttpHead
"name,label,type,groupType,function,category,editable,groupNames,link,tags,metadata,commandDescription,stateDescription");

CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMustRevalidate(true);
cc.setPrivate(true);
return Response.ok(new Stream2JSONInputStream(itemStream)).lastModified(lastModifiedDate).cacheControl(cc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public Response getTags(final @Context Request request, final @Context UriInfo u
}

CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMustRevalidate(true);
cc.setPrivate(true);

Expand Down Expand Up @@ -165,6 +166,7 @@ public Response getTagAndSubTags(final @Context Request request,
}

CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMustRevalidate(true);
cc.setPrivate(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ public Response getAll(@Context Request request,
}

CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMustRevalidate(true);
cc.setPrivate(true);
thingStream = dtoMapper.limitToFields(thingStream, "UID,label,bridgeUID,thingTypeUID,location,editable");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public Response getAllComponents(@Context Request request, @PathParam("namespace
}

CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMustRevalidate(true);
cc.setPrivate(true);
return Response.ok(new Stream2JSONInputStream(components)).lastModified(lastModifiedDate).cacheControl(cc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public Response getUoMInfo(final @Context Request request, final @Context UriInf
}

CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMustRevalidate(true);
cc.setPrivate(true);

Expand Down

0 comments on commit f5bd7f9

Please sign in to comment.