webserver_base/templates/frontend.rs
1//! Per-server frontend data, computed once at boot.
2//!
3//! Everything here is true of every page but owned by neither
4//! [`BaseTemplateData`](super::BaseTemplateData) (which is what the *author*
5//! declares) nor [`PageTemplateData`](super::PageTemplateData) (which is what
6//! *this render* is): it is what the server worked out for itself at startup.
7
8use serde::Serialize;
9
10/// Where the first-party Plausible proxy lives on this origin.
11///
12/// Both paths are derived from the project name rather than configured. Two
13/// reasons: Plausible's own guidance is to avoid their default paths because
14/// blocklists target them, and a single library-wide constant shared across
15/// every site would be one filter rule away from breaking all of them at once.
16#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
17pub struct AnalyticsPaths {
18 /// The proxied script, e.g. `/script/boggledygook-a3f2c1d8.js`.
19 pub script_path: String,
20 /// The proxied event endpoint, e.g. `/api/v1/boggledygook-a3f2c1d8`.
21 pub event_path: String,
22}
23
24/// Browser-side error monitoring.
25#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
26pub struct SentryBrowser {
27 /// The proxied Sentry browser bundle.
28 pub script_path: String,
29 /// The browser DSN, public by design.
30 pub dsn: String,
31 /// The share of page loads that send a trace, as a script literal
32 /// (`0.2`); `None` when tracing is off, so the layout loads and starts
33 /// nothing for it.
34 pub traces_sample_rate: Option<String>,
35 /// Where the browser SDK posts envelopes, relayed verbatim to Sentry.
36 pub tunnel_path: String,
37 /// The Sentry `environment` tag.
38 pub environment: String,
39}
40
41/// What the social card image actually is.
42///
43/// Read from the image's header bytes at boot rather than declared, because a
44/// hand-typed dimension can drift from the file and a wrong `og:image:width`
45/// makes platforms render the card at the wrong aspect ratio — worse than
46/// omitting it.
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize)]
48pub struct SocialImageMetadata {
49 pub width: Option<u32>,
50 pub height: Option<u32>,
51 pub mime_type: Option<&'static str>,
52}
53
54/// The smallest image X/Twitter will render as a `summary_large_image` card.
55pub const MINIMUM_SOCIAL_IMAGE_WIDTH: u32 = 600;
56/// The matching minimum height.
57pub const MINIMUM_SOCIAL_IMAGE_HEIGHT: u32 = 314;
58
59impl SocialImageMetadata {
60 /// Whether this image is large enough for the wide card the layout
61 /// declares. A smaller one silently degrades to a thumbnail.
62 #[must_use]
63 pub const fn is_large_enough(&self) -> bool {
64 match (self.width, self.height) {
65 (Some(width), Some(height)) => {
66 width >= MINIMUM_SOCIAL_IMAGE_WIDTH && height >= MINIMUM_SOCIAL_IMAGE_HEIGHT
67 }
68 // Unknown dimensions are not a failure; nothing is claimed.
69 _ => true,
70 }
71 }
72}
73
74/// The autodiscovery links a reader uses to find the feed.
75///
76/// Emitted on every page, not only on the one the feed mirrors: a reader handed
77/// any URL on the site should still find the stream. Ordered RSS, Atom, JSON in
78/// the layout, because a reader that takes the first `rel="alternate"` rather
79/// than offering a choice is far more likely to be RSS-only than Atom-only.
80///
81/// Plain strings rather than a dependency on the `feed` kit: the layout needs
82/// three URLs and a name, and nothing here should force a templates-only
83/// consumer to compile a feed serializer.
84#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
85pub struct FeedLinks {
86 /// The feed's name, shown by a reader that presents a picker.
87 pub title: String,
88 /// Absolute URL of the RSS 2.0 document.
89 pub rss: String,
90 /// Absolute URL of the Atom 1.0 document.
91 pub atom: String,
92 /// Absolute URL of the JSON Feed document.
93 pub json: String,
94}
95
96/// Everything a frontend computes once and reuses on every render.
97#[derive(Debug, Clone, Serialize)]
98pub struct FrontendRuntime {
99 /// The inline pre-paint theme script.
100 pub theme_script: String,
101 /// The social image's real dimensions and MIME type.
102 pub social_image: SocialImageMetadata,
103 /// Whether an SVG icon exists to link. A project whose art is a photograph
104 /// authors a PNG instead, and the layout must not link a file that is not
105 /// there.
106 pub has_svg_icon: bool,
107 /// Analytics proxy paths; `None` when analytics is disabled, so the layout
108 /// renders no script tag.
109 pub analytics: Option<AnalyticsPaths>,
110 /// Browser error monitoring.
111 pub sentry_browser: SentryBrowser,
112 /// Feed autodiscovery, when the site declared a feed.
113 pub feed: Option<FeedLinks>,
114}
115
116#[cfg(test)]
117mod tests {
118 use super::{MINIMUM_SOCIAL_IMAGE_HEIGHT, MINIMUM_SOCIAL_IMAGE_WIDTH, SocialImageMetadata};
119
120 #[test]
121 fn an_image_at_the_floor_is_large_enough() {
122 let image: SocialImageMetadata = SocialImageMetadata {
123 width: Some(MINIMUM_SOCIAL_IMAGE_WIDTH),
124 height: Some(MINIMUM_SOCIAL_IMAGE_HEIGHT),
125 mime_type: Some("image/webp"),
126 };
127
128 let expected: bool = true;
129 let actual: bool = image.is_large_enough();
130 assert_eq!(expected, actual);
131 }
132
133 #[test]
134 fn an_image_below_the_floor_would_degrade_to_a_thumbnail() {
135 let image: SocialImageMetadata = SocialImageMetadata {
136 width: Some(400),
137 height: Some(210),
138 mime_type: Some("image/webp"),
139 };
140
141 let expected: bool = false;
142 let actual: bool = image.is_large_enough();
143 assert_eq!(expected, actual);
144 }
145
146 #[test]
147 fn unknown_dimensions_are_not_treated_as_a_failure() {
148 let expected: bool = true;
149 let actual: bool = SocialImageMetadata::default().is_large_enough();
150 assert_eq!(expected, actual);
151 }
152}