Skip to main content

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    /// The Sentry `release` as a ready-to-emit JavaScript string literal
40    /// (quotes included, `<` escaped so it cannot close the inline script);
41    /// `None` omits `release` from the init, as before.
42    pub release_js: Option<String>,
43}
44
45/// What the social card image actually is.
46///
47/// Read from the image's header bytes at boot rather than declared, because a
48/// hand-typed dimension can drift from the file and a wrong `og:image:width`
49/// makes platforms render the card at the wrong aspect ratio — worse than
50/// omitting it.
51#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize)]
52pub struct SocialImageMetadata {
53    pub width: Option<u32>,
54    pub height: Option<u32>,
55    pub mime_type: Option<&'static str>,
56}
57
58/// The smallest image X/Twitter will render as a `summary_large_image` card.
59pub const MINIMUM_SOCIAL_IMAGE_WIDTH: u32 = 600;
60/// The matching minimum height.
61pub const MINIMUM_SOCIAL_IMAGE_HEIGHT: u32 = 314;
62
63impl SocialImageMetadata {
64    /// Whether this image is large enough for the wide card the layout
65    /// declares. A smaller one silently degrades to a thumbnail.
66    #[must_use]
67    pub const fn is_large_enough(&self) -> bool {
68        match (self.width, self.height) {
69            (Some(width), Some(height)) => {
70                width >= MINIMUM_SOCIAL_IMAGE_WIDTH && height >= MINIMUM_SOCIAL_IMAGE_HEIGHT
71            }
72            // Unknown dimensions are not a failure; nothing is claimed.
73            _ => true,
74        }
75    }
76}
77
78/// The autodiscovery links a reader uses to find the feed.
79///
80/// Emitted on every page, not only on the one the feed mirrors: a reader handed
81/// any URL on the site should still find the stream. Ordered RSS, Atom, JSON in
82/// the layout, because a reader that takes the first `rel="alternate"` rather
83/// than offering a choice is far more likely to be RSS-only than Atom-only.
84///
85/// Plain strings rather than a dependency on the `feed` kit: the layout needs
86/// three URLs and a name, and nothing here should force a templates-only
87/// consumer to compile a feed serializer.
88#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
89pub struct FeedLinks {
90    /// The feed's name, shown by a reader that presents a picker.
91    pub title: String,
92    /// Absolute URL of the RSS 2.0 document.
93    pub rss: String,
94    /// Absolute URL of the Atom 1.0 document.
95    pub atom: String,
96    /// Absolute URL of the JSON Feed document.
97    pub json: String,
98}
99
100/// Everything a frontend computes once and reuses on every render.
101#[derive(Debug, Clone, Serialize)]
102pub struct FrontendRuntime {
103    /// The inline pre-paint theme script.
104    pub theme_script: String,
105    /// The social image's real dimensions and MIME type.
106    pub social_image: SocialImageMetadata,
107    /// Whether an SVG icon exists to link. A project whose art is a photograph
108    /// authors a PNG instead, and the layout must not link a file that is not
109    /// there.
110    pub has_svg_icon: bool,
111    /// Analytics proxy paths; `None` when analytics is disabled, so the layout
112    /// renders no script tag.
113    pub analytics: Option<AnalyticsPaths>,
114    /// Browser error monitoring.
115    pub sentry_browser: SentryBrowser,
116    /// Feed autodiscovery, when the site declared a feed.
117    pub feed: Option<FeedLinks>,
118}
119
120#[cfg(test)]
121mod tests {
122    use super::{MINIMUM_SOCIAL_IMAGE_HEIGHT, MINIMUM_SOCIAL_IMAGE_WIDTH, SocialImageMetadata};
123
124    #[test]
125    fn an_image_at_the_floor_is_large_enough() {
126        let image: SocialImageMetadata = SocialImageMetadata {
127            width: Some(MINIMUM_SOCIAL_IMAGE_WIDTH),
128            height: Some(MINIMUM_SOCIAL_IMAGE_HEIGHT),
129            mime_type: Some("image/webp"),
130        };
131
132        let expected: bool = true;
133        let actual: bool = image.is_large_enough();
134        assert_eq!(expected, actual);
135    }
136
137    #[test]
138    fn an_image_below_the_floor_would_degrade_to_a_thumbnail() {
139        let image: SocialImageMetadata = SocialImageMetadata {
140            width: Some(400),
141            height: Some(210),
142            mime_type: Some("image/webp"),
143        };
144
145        let expected: bool = false;
146        let actual: bool = image.is_large_enough();
147        assert_eq!(expected, actual);
148    }
149
150    #[test]
151    fn unknown_dimensions_are_not_treated_as_a_failure() {
152        let expected: bool = true;
153        let actual: bool = SocialImageMetadata::default().is_large_enough();
154        assert_eq!(expected, actual);
155    }
156}