feat: make htmx check more specific

image-uploads
Jef Roosens 2024-12-29 16:09:07 +01:00
parent 4c84c944d4
commit f980115d45
No known key found for this signature in database
GPG Key ID: 21FD3D77D56BAF49
2 changed files with 11 additions and 4 deletions

View File

@ -13,9 +13,16 @@ use tower_http::set_header::SetResponseHeaderLayer;
use crate::db;
const HX_REQUEST_HEADER: &str = "HX-Request";
const HX_HISTORY_RESTORE_HEADER: &str = "HX-History-Restore-Request";
pub fn is_htmx_req(headers: &HeaderMap) -> bool {
headers.get(HX_REQUEST_HEADER).is_some()
pub fn render_partial(headers: &HeaderMap) -> bool {
let is_htmx_req = headers.get(HX_REQUEST_HEADER).is_some();
let is_hist_restore_req = headers
.get(HX_HISTORY_RESTORE_HEADER)
.map(|val| val == HeaderValue::from_static("true"))
.unwrap_or(false);
is_htmx_req && !is_hist_restore_req
}
pub fn app(ctx: crate::Context) -> axum::Router {

View File

@ -9,7 +9,7 @@ use tera::Context;
use crate::db;
use super::is_htmx_req;
use super::render_partial;
pub fn app(ctx: crate::Context) -> axum::Router {
Router::new()
@ -31,7 +31,7 @@ async fn get_plant_page(
let mut context = Context::new();
context.insert("plant", &plant);
let tmpl = if is_htmx_req(&headers) {
let tmpl = if render_partial(&headers) {
"partials/plant_info.html"
} else {
"plant_page.html"