Fix ogimages

This commit is contained in:
Andras Schmelczer 2026-03-24 22:53:34 +00:00
parent da1bf49524
commit 160283f1a1
2 changed files with 35 additions and 4 deletions

View file

@ -22,7 +22,7 @@ pub async fn og_middleware(request: Request, next: Next) -> Response {
let response = next.run(request).await;
// Only inject OG tags into SPA HTML responses, not proxied PocketBase responses
if path.starts_with("/pb/") || path.starts_with("/api/") {
if path.starts_with("/pb/") || path.starts_with("/api/") || path.starts_with("/s/") {
return response;
}

View file

@ -1,8 +1,8 @@
use std::sync::Arc;
use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Redirect, Response};
use axum::http::{header, StatusCode};
use axum::response::{Html, IntoResponse, Response};
use axum::Json;
use rand::Rng;
use serde::{Deserialize, Serialize};
@ -139,7 +139,38 @@ pub async fn get_short_url(
match params {
Some(params) => {
Redirect::temporary(&format!("/dashboard?{params}")).into_response()
let redirect_url = format!("/dashboard?{params}");
let og_image_url = format!(
"{}/api/screenshot?og=1&{params}",
state.public_url
);
let og_url = format!("{}/s/{code}", state.public_url);
let og_title = "Perfect Postcode \u{2014} Every neighbourhood in England";
let og_description = "Explore property prices, energy ratings, crime stats, school ratings, and more across England on one interactive map.";
let html = format!(
r#"<!DOCTYPE html>
<html><head>
<meta charset="utf-8" />
<meta property="og:title" content="{og_title}" />
<meta property="og:description" content="{og_description}" />
<meta property="og:type" content="website" />
<meta property="og:url" content="{og_url}" />
<meta property="og:image" content="{og_image_url}" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="{og_title}" />
<meta name="twitter:description" content="{og_description}" />
<meta http-equiv="refresh" content="0;url={redirect_url}" />
<title>{og_title}</title>
</head><body></body></html>"#
);
(
[(header::CACHE_CONTROL, "public, max-age=86400")],
Html(html),
)
.into_response()
}
None => StatusCode::NOT_FOUND.into_response(),
}