lgtm
This commit is contained in:
parent
084117cea8
commit
a8de0a614d
36 changed files with 1329 additions and 522 deletions
|
|
@ -7,6 +7,7 @@ use axum::middleware::Next;
|
|||
use axum::response::Response;
|
||||
use tracing::warn;
|
||||
|
||||
use crate::language::{language_from_accept_language, query_string_with_language};
|
||||
use crate::state::AppState;
|
||||
|
||||
const OG_PLACEHOLDER: &str =
|
||||
|
|
@ -140,19 +141,19 @@ fn seo_page_for_path(path: &str) -> Option<SeoPage> {
|
|||
"/saved" => Some(SeoPage {
|
||||
canonical_path: "/saved",
|
||||
title: "Perfect Postcode account",
|
||||
description: "Manage your Perfect Postcode account, saved searches, saved properties and invitations.",
|
||||
description: "Manage your Perfect Postcode account, saved searches, shared links and invitations.",
|
||||
indexable: false,
|
||||
}),
|
||||
"/invites" => Some(SeoPage {
|
||||
canonical_path: "/invites",
|
||||
title: "Perfect Postcode account",
|
||||
description: "Manage your Perfect Postcode account, saved searches, saved properties and invitations.",
|
||||
description: "Manage your Perfect Postcode account, saved searches, shared links and invitations.",
|
||||
indexable: false,
|
||||
}),
|
||||
"/account" => Some(SeoPage {
|
||||
canonical_path: "/account",
|
||||
title: "Perfect Postcode account",
|
||||
description: "Manage your Perfect Postcode account, saved searches, saved properties and invitations.",
|
||||
description: "Manage your Perfect Postcode account, saved searches, shared links and invitations.",
|
||||
indexable: false,
|
||||
}),
|
||||
_ if path.starts_with("/invite/") => Some(SeoPage {
|
||||
|
|
@ -223,9 +224,17 @@ fn not_found_response(public_url: &str, path: &str) -> Response {
|
|||
response
|
||||
}
|
||||
|
||||
fn route_seo_tags(page: &SeoPage, path: &str, query_string: &str, public_url: &str) -> String {
|
||||
fn route_seo_tags(
|
||||
page: &SeoPage,
|
||||
path: &str,
|
||||
query_string: &str,
|
||||
public_url: &str,
|
||||
language: &str,
|
||||
) -> String {
|
||||
let path_e = escape_attr(path);
|
||||
let query_e = escape_attr(query_string);
|
||||
let screenshot_query_string = query_string_with_language(query_string, language);
|
||||
let screenshot_query_e = escape_attr(&screenshot_query_string);
|
||||
let public_url_e = escape_attr(public_url.trim_end_matches('/'));
|
||||
let canonical_path_e = escape_attr(page.canonical_path);
|
||||
let title_e = escape_attr(page.title);
|
||||
|
|
@ -233,15 +242,15 @@ fn route_seo_tags(page: &SeoPage, path: &str, query_string: &str, public_url: &s
|
|||
|
||||
let is_invite = path.starts_with("/invite/");
|
||||
let og_image_url = if is_invite {
|
||||
if query_string.is_empty() {
|
||||
if screenshot_query_string.is_empty() {
|
||||
format!("{public_url_e}/api/screenshot?og=1&path={path_e}")
|
||||
} else {
|
||||
format!("{public_url_e}/api/screenshot?og=1&path={path_e}&{query_e}")
|
||||
format!("{public_url_e}/api/screenshot?og=1&path={path_e}&{screenshot_query_e}")
|
||||
}
|
||||
} else if query_string.is_empty() {
|
||||
} else if screenshot_query_string.is_empty() {
|
||||
format!("{public_url_e}/api/screenshot?og=1")
|
||||
} else {
|
||||
format!("{public_url_e}/api/screenshot?og=1&{query_e}")
|
||||
format!("{public_url_e}/api/screenshot?og=1&{screenshot_query_e}")
|
||||
};
|
||||
|
||||
let canonical_url = format!("{public_url_e}{canonical_path_e}");
|
||||
|
|
@ -313,6 +322,12 @@ 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
|
||||
let query_string = request.uri().query().unwrap_or("").to_string();
|
||||
let language = language_from_accept_language(
|
||||
request
|
||||
.headers()
|
||||
.get(header::ACCEPT_LANGUAGE)
|
||||
.and_then(|value| value.to_str().ok()),
|
||||
);
|
||||
|
||||
// Get state from extensions
|
||||
let state = request.extensions().get::<Arc<AppState>>().cloned();
|
||||
|
|
@ -362,7 +377,7 @@ 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);
|
||||
let tags = route_seo_tags(&page, &path, &query_string, &state.public_url, language);
|
||||
let html = inject_tags(html, &page, &tags);
|
||||
parts.headers.remove(header::CONTENT_LENGTH);
|
||||
Response::from_parts(parts, Body::from(html))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue