HtmxMiddleware: add Vary header to prevent caching issues

See https://htmx.org/docs/#caching
This commit is contained in:
Emanuel Guével 2025-04-20 16:06:55 +02:00
parent dee5701b4a
commit 95d3f10bc9

View File

@ -19,17 +19,21 @@ pub fn afterRequest(request: *jetzig.http.Request) !void {
/// If a redirect was issued during request processing, reset any response data, set response /// If a redirect was issued during request processing, reset any response data, set response
/// status to `200 OK` and replace the `Location` header with a `HX-Redirect` header. /// status to `200 OK` and replace the `Location` header with a `HX-Redirect` header.
/// Add Vary response header to prevent caching the page without layout for requests not coming
/// from htmx.
pub fn beforeResponse(request: *jetzig.http.Request, response: *jetzig.http.Response) !void { pub fn beforeResponse(request: *jetzig.http.Request, response: *jetzig.http.Response) !void {
switch (response.status_code) {
.moved_permanently, .found => {},
else => return,
}
if (request.headers.get("HX-Request") == null) return; if (request.headers.get("HX-Request") == null) return;
switch (response.status_code) {
.moved_permanently, .found => {
if (response.headers.get("Location")) |location| { if (response.headers.get("Location")) |location| {
response.status_code = .ok; response.status_code = .ok;
request.response_data.reset(); request.response_data.reset();
try response.headers.append("HX-Redirect", location); try response.headers.append("HX-Redirect", location);
} }
},
else => {
try response.headers.append("Vary", "HX-Request");
},
}
} }