//! The static files of the UI, compiled into the binary and served from `/assets/`. //! //! The CSS is compiled from `style/` by `build.rs`. The scripts and the offline page are in //! `crates/im-web/assets/`. Each link to a file that changes with a build carries the first 12 //! hex digits of its SHA-256 as `?v=`, so a changed file gets a new URL and an unchanged one keeps //! its cache entry. Files whose URL does not change (the service worker and the offline page) are //! served with `no-cache`. use topcoat::Result; use topcoat::router::{HeaderMap, HeaderValue, StatusCode, header, route}; /// Link to the stylesheet, with its content version. pub const MAIN_CSS_HREF: &str = concat!("/assets/main.css?v=", env!("IM_MAIN_CSS_V")); /// Link to the script that makes navigation soft and drives the live pages. pub const NAV_JS_HREF: &str = concat!("/assets/nav.js?v=", env!("IM_NAV_JS_V")); /// Link to the script of individual pages. pub const PAGES_JS_HREF: &str = concat!("/assets/pages.js?v=", env!("IM_PAGES_JS_V")); const MAIN_CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/main.css")); const NAV_JS: &str = include_str!("../assets/nav.js"); const PAGES_JS: &str = include_str!("../assets/pages.js"); const OFFLINE_HTML: &str = include_str!("../assets/offline.html"); const SW_JS: &str = include_str!("../assets/sw.js"); /// A content-versioned file. The URL changes with the bytes, so the browser may keep it for a year. const IMMUTABLE: &str = "public, max-age=31536000, immutable"; /// A file whose URL stays the same across builds. The browser must ask again each time. const REVALIDATE: &str = "no-cache"; /// The status, headers and body of an asset answer. type Asset = (StatusCode, HeaderMap, &'static str); /// Builds the answer for one asset: the content type and the cache policy. fn asset(content_type: &'static str, cache: &'static str, body: &'static str) -> Asset { let mut headers = HeaderMap::new(); headers.insert(header::CONTENT_TYPE, HeaderValue::from_static(content_type)); headers.insert(header::CACHE_CONTROL, HeaderValue::from_static(cache)); (StatusCode::OK, headers, body) } /// `GET /assets/main.css`. #[route(GET "/assets/main.css")] async fn main_css() -> Result { Ok(asset("text/css; charset=utf-8", IMMUTABLE, MAIN_CSS)) } /// `GET /assets/nav.js`. #[route(GET "/assets/nav.js")] async fn nav_js() -> Result { Ok(asset("text/javascript; charset=utf-8", IMMUTABLE, NAV_JS)) } /// `GET /assets/pages.js`. #[route(GET "/assets/pages.js")] async fn pages_js() -> Result { Ok(asset("text/javascript; charset=utf-8", IMMUTABLE, PAGES_JS)) } /// `GET /assets/offline.html`. Also precached by the service worker. #[route(GET "/assets/offline.html")] async fn offline_html() -> Result { Ok(asset("text/html; charset=utf-8", REVALIDATE, OFFLINE_HTML)) } /// `GET /assets/sw.js`. It is served at the root scope, which needs the `Service-Worker-Allowed` /// header; without it the browser would limit the worker to `/assets/`. #[route(GET "/assets/sw.js")] async fn sw_js() -> Result { let (status, mut headers, body) = asset("text/javascript; charset=utf-8", REVALIDATE, SW_JS); headers.insert("service-worker-allowed", HeaderValue::from_static("/")); Ok((status, headers, body)) }