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 loader script.
28    pub script_path: String,
29    /// Where the browser SDK posts envelopes, relayed verbatim to Sentry.
30    pub tunnel_path: String,
31    /// The Sentry `environment` tag.
32    pub environment: String,
33}
34
35/// What the social card image actually is.
36///
37/// Read from the image's header bytes at boot rather than declared, because a
38/// hand-typed dimension can drift from the file and a wrong `og:image:width`
39/// makes platforms render the card at the wrong aspect ratio — worse than
40/// omitting it.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize)]
42pub struct SocialImageMetadata {
43    pub width: Option<u32>,
44    pub height: Option<u32>,
45    pub mime_type: Option<&'static str>,
46}
47
48/// The smallest image X/Twitter will render as a `summary_large_image` card.
49pub const MINIMUM_SOCIAL_IMAGE_WIDTH: u32 = 600;
50/// The matching minimum height.
51pub const MINIMUM_SOCIAL_IMAGE_HEIGHT: u32 = 314;
52
53impl SocialImageMetadata {
54    /// Whether this image is large enough for the wide card the layout
55    /// declares. A smaller one silently degrades to a thumbnail.
56    #[must_use]
57    pub const fn is_large_enough(&self) -> bool {
58        match (self.width, self.height) {
59            (Some(width), Some(height)) => {
60                width >= MINIMUM_SOCIAL_IMAGE_WIDTH && height >= MINIMUM_SOCIAL_IMAGE_HEIGHT
61            }
62            // Unknown dimensions are not a failure; nothing is claimed.
63            _ => true,
64        }
65    }
66}
67
68/// Everything a frontend computes once and reuses on every render.
69#[derive(Debug, Clone, Serialize)]
70pub struct FrontendRuntime {
71    /// The inline pre-paint theme script.
72    pub theme_script: String,
73    /// The social image's real dimensions and MIME type.
74    pub social_image: SocialImageMetadata,
75    /// Whether an SVG icon exists to link. A project whose art is a photograph
76    /// authors a PNG instead, and the layout must not link a file that is not
77    /// there.
78    pub has_svg_icon: bool,
79    /// Analytics proxy paths.
80    pub analytics: AnalyticsPaths,
81    /// Browser error monitoring.
82    pub sentry_browser: SentryBrowser,
83}
84
85#[cfg(test)]
86mod tests {
87    use super::{MINIMUM_SOCIAL_IMAGE_HEIGHT, MINIMUM_SOCIAL_IMAGE_WIDTH, SocialImageMetadata};
88
89    #[test]
90    fn an_image_at_the_floor_is_large_enough() {
91        let image: SocialImageMetadata = SocialImageMetadata {
92            width: Some(MINIMUM_SOCIAL_IMAGE_WIDTH),
93            height: Some(MINIMUM_SOCIAL_IMAGE_HEIGHT),
94            mime_type: Some("image/webp"),
95        };
96
97        let expected: bool = true;
98        let actual: bool = image.is_large_enough();
99        assert_eq!(expected, actual);
100    }
101
102    #[test]
103    fn an_image_below_the_floor_would_degrade_to_a_thumbnail() {
104        let image: SocialImageMetadata = SocialImageMetadata {
105            width: Some(400),
106            height: Some(210),
107            mime_type: Some("image/webp"),
108        };
109
110        let expected: bool = false;
111        let actual: bool = image.is_large_enough();
112        assert_eq!(expected, actual);
113    }
114
115    #[test]
116    fn unknown_dimensions_are_not_treated_as_a_failure() {
117        let expected: bool = true;
118        let actual: bool = SocialImageMetadata::default().is_large_enough();
119        assert_eq!(expected, actual);
120    }
121}