Skip to content

Implement dynamic OpenGraph image url support for crate pages #10464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,8 @@ export GH_CLIENT_SECRET=
# Credentials for connecting to the Sentry error reporting service.
# export SENTRY_DSN_API=
export SENTRY_ENV_API=local

# Base URL for the service from which the OpenGraph images
# for crates are loaded. Make sure the URL ends
# with a `/`.
export OG_IMAGE_BASE_URL="http://localhost:3000/og/"
182 changes: 179 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ ipnetwork = "=0.21.1"
json-subscriber = "=0.2.4"
krata-tokio-tar = "=0.4.2"
lettre = { version = "=0.11.12", default-features = false, features = ["file-transport", "smtp-transport", "hostname", "builder", "tokio1", "tokio1-native-tls"] }
minijinja = "=2.7.0"
minijinja = { version = "=2.7.0", features = ["loader"] }
mockall = "=0.13.1"
moka = { version = "=0.12.10", default-features = false, features = ["future"] }
native-tls = "=0.2.13"
oauth2 = "=5.0.0"
object_store = { version = "=0.11.2", features = ["aws"] }
Expand Down
2 changes: 1 addition & 1 deletion app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

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

<meta property="og:image" content="https://crates.io/assets/og-image.png">
<meta property="og:image" content="{{og_image_url}}">
<meta name="twitter:card" content="summary_large_image">
</head>
<body>
Expand Down
13 changes: 13 additions & 0 deletions src/config/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::{anyhow, Context};
use ipnetwork::IpNetwork;
use oauth2::{ClientId, ClientSecret};
use url::Url;

use crate::rate_limiter::{LimitedAction, RateLimiterConfig};
use crate::Env;
Expand Down Expand Up @@ -75,6 +76,16 @@ pub struct Server {
/// non-API requests?
pub serve_html: bool,

/// Base URL for the service from which the OpenGraph images
/// for crates are loaded. Required if
/// [`Self::serve_html`] is set.
pub og_image_base_url: Option<Url>,

/// Maximum number of items that the HTML render
/// cache in `crate::middleware::ember_html::serve_html`
/// can hold. Defaults to 1024.
pub html_render_cache_max_capacity: u64,

pub content_security_policy: Option<HeaderValue>,
}

Expand Down Expand Up @@ -215,6 +226,8 @@ impl Server {
.unwrap_or(StatusCodeConfig::AdjustAll),
serve_dist: true,
serve_html: true,
og_image_base_url: var_parsed("OG_IMAGE_BASE_URL")?,
html_render_cache_max_capacity: var_parsed("HTML_RENDER_CACHE_CAP")?.unwrap_or(1024),
content_security_policy: Some(content_security_policy.parse()?),
})
}
Expand Down
Loading
Loading