This commit is contained in:
Andras Schmelczer 2026-05-17 10:16:30 +01:00
parent 47d89f6fad
commit 017902b8e6
82 changed files with 331466 additions and 54841 deletions

View file

@ -12,6 +12,7 @@ use crate::state::AppState;
const OG_PLACEHOLDER: &str =
r#"<meta name="x-og-placeholder" content="__PERFECT_POSTCODE_OG_TAGS__"/>"#;
const BUGSINK_CONFIG_PLACEHOLDER: &str = "__PERFECT_POSTCODE_BUGSINK_CONFIG__";
const HTML_BODY_LIMIT: usize = 5 * 1024 * 1024;
@ -318,6 +319,26 @@ fn inject_tags(mut html: String, page: &SeoPage, tags: &str) -> String {
html
}
fn escape_json_for_script_tag(json: &str) -> String {
json.replace('&', "\\u0026")
.replace('<', "\\u003c")
.replace('>', "\\u003e")
}
fn inject_bugsink_config(html: String, config: Option<&crate::bugsink::FrontendConfig>) -> String {
if !html.contains(BUGSINK_CONFIG_PLACEHOLDER) {
return html;
}
let json = config
.and_then(|config| serde_json::to_string(config).ok())
.unwrap_or_else(|| "{}".to_string());
html.replace(
BUGSINK_CONFIG_PLACEHOLDER,
&escape_json_for_script_tag(&json),
)
}
pub async fn og_middleware(request: Request, next: Next) -> Response {
let path = request.uri().path().to_string();
// Capture the query string before passing the request through
@ -360,10 +381,10 @@ pub async fn og_middleware(request: Request, next: Next) -> Response {
None => return response,
};
let page = match seo_page_for_path(&path) {
Some(page) => page,
None => return response,
};
let page = seo_page_for_path(&path);
if page.is_none() && state.bugsink_frontend_config.is_none() {
return response;
}
let (mut parts, body) = response.into_parts();
let bytes = match to_bytes(body, HTML_BODY_LIMIT).await {
@ -377,8 +398,11 @@ pub async fn og_middleware(request: Request, next: Next) -> Response {
};
let html = String::from_utf8_lossy(&bytes).into_owned();
let tags = route_seo_tags(&page, &path, &query_string, &state.public_url, language);
let html = inject_tags(html, &page, &tags);
let mut html = inject_bugsink_config(html, state.bugsink_frontend_config.as_ref());
if let Some(page) = page {
let tags = route_seo_tags(&page, &path, &query_string, &state.public_url, language);
html = inject_tags(html, &page, &tags);
}
parts.headers.remove(header::CONTENT_LENGTH);
Response::from_parts(parts, Body::from(html))
}