[rest] Add no-cache directive to cached REST responses (#3970)

Fixes https://github.com/openhab/openhab-webui/issues/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 <florianh_dev@icloud.com>
This commit is contained in:
Florian Hotze 2023-12-27 17:52:49 +01:00 committed by GitHub
parent b08a01c93f
commit f5bd7f94f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 7 additions and 0 deletions

View File

@ -188,6 +188,7 @@ public class RuleResource implements RESTResource {
.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");

View File

@ -271,6 +271,7 @@ public class ItemResource implements RESTResource {
"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)

View File

@ -133,6 +133,7 @@ public class TagResource implements RESTResource {
}
CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMustRevalidate(true);
cc.setPrivate(true);
@ -165,6 +166,7 @@ public class TagResource implements RESTResource {
}
CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMustRevalidate(true);
cc.setPrivate(true);

View File

@ -328,6 +328,7 @@ public class ThingResource implements RESTResource {
}
CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMustRevalidate(true);
cc.setPrivate(true);
thingStream = dtoMapper.limitToFields(thingStream, "UID,label,bridgeUID,thingTypeUID,location,editable");

View File

@ -168,6 +168,7 @@ public class UIResource implements RESTResource {
}
CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMustRevalidate(true);
cc.setPrivate(true);
return Response.ok(new Stream2JSONInputStream(components)).lastModified(lastModifiedDate).cacheControl(cc)

View File

@ -119,6 +119,7 @@ public class SystemInfoResource implements RESTResource, ConfigurationListener {
}
CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMustRevalidate(true);
cc.setPrivate(true);