Skip to content

Commit 223ccc1

Browse files
authored
Merge pull request #10464 from hdoordt/main
Implement dynamic OpenGraph image url support for crate pages
2 parents 2b757c3 + 3bd430c commit 223ccc1

File tree

8 files changed

+371
-17
lines changed

8 files changed

+371
-17
lines changed

.env.sample

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,8 @@ export GH_CLIENT_SECRET=
8686
# Credentials for connecting to the Sentry error reporting service.
8787
# export SENTRY_DSN_API=
8888
export SENTRY_ENV_API=local
89+
90+
# Base URL for the service from which the OpenGraph images
91+
# for crates are loaded. Make sure the URL ends
92+
# with a `/`.
93+
export OG_IMAGE_BASE_URL="http://localhost:3000/og/"

Cargo.lock

Lines changed: 179 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ ipnetwork = "=0.21.1"
9797
json-subscriber = "=0.2.4"
9898
krata-tokio-tar = "=0.4.2"
9999
lettre = { version = "=0.11.12", default-features = false, features = ["file-transport", "smtp-transport", "hostname", "builder", "tokio1", "tokio1-native-tls"] }
100-
minijinja = "=2.7.0"
100+
minijinja = { version = "=2.7.0", features = ["loader"] }
101101
mockall = "=0.13.1"
102+
moka = { version = "=0.12.10", default-features = false, features = ["future"] }
102103
native-tls = "=0.2.13"
103104
oauth2 = "=5.0.0"
104105
object_store = { version = "=0.11.2", features = ["aws"] }

app/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<meta name="google" content="notranslate" />
2525

26-
<meta property="og:image" content="https://crates.io/assets/og-image.png">
26+
<meta property="og:image" content="{{og_image_url}}">
2727
<meta name="twitter:card" content="summary_large_image">
2828
</head>
2929
<body>

src/config/server.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use anyhow::{anyhow, Context};
22
use ipnetwork::IpNetwork;
33
use oauth2::{ClientId, ClientSecret};
4+
use url::Url;
45

56
use crate::rate_limiter::{LimitedAction, RateLimiterConfig};
67
use crate::Env;
@@ -75,6 +76,16 @@ pub struct Server {
7576
/// non-API requests?
7677
pub serve_html: bool,
7778

79+
/// Base URL for the service from which the OpenGraph images
80+
/// for crates are loaded. Required if
81+
/// [`Self::serve_html`] is set.
82+
pub og_image_base_url: Option<Url>,
83+
84+
/// Maximum number of items that the HTML render
85+
/// cache in `crate::middleware::ember_html::serve_html`
86+
/// can hold. Defaults to 1024.
87+
pub html_render_cache_max_capacity: u64,
88+
7889
pub content_security_policy: Option<HeaderValue>,
7990
}
8091

@@ -215,6 +226,8 @@ impl Server {
215226
.unwrap_or(StatusCodeConfig::AdjustAll),
216227
serve_dist: true,
217228
serve_html: true,
229+
og_image_base_url: var_parsed("OG_IMAGE_BASE_URL")?,
230+
html_render_cache_max_capacity: var_parsed("HTML_RENDER_CACHE_CAP")?.unwrap_or(1024),
218231
content_security_policy: Some(content_security_policy.parse()?),
219232
})
220233
}

0 commit comments

Comments
 (0)