forge
Explore
Log in

Don't you dare go Hollow

im

Clone
★ 0

0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits

assets.rs75 lines, 3.2 KiBHistoryRawDownload
1//! The static files of the UI, compiled into the binary and served from `/assets/`.
2//!
3//! The CSS is compiled from `style/` by `build.rs`. The scripts and the offline page are in
4//! `crates/im-web/assets/`. Each link to a file that changes with a build carries the first 12
5//! hex digits of its SHA-256 as `?v=`, so a changed file gets a new URL and an unchanged one keeps
6//! its cache entry. Files whose URL does not change (the service worker and the offline page) are
7//! served with `no-cache`.
8
9use topcoat::Result;
10use topcoat::router::{HeaderMap, HeaderValue, StatusCode, header, route};
11
12/// Link to the stylesheet, with its content version.
13pub const MAIN_CSS_HREF: &str = concat!("/assets/main.css?v=", env!("IM_MAIN_CSS_V"));
14
15/// Link to the script that makes navigation soft and drives the live pages.
16pub const NAV_JS_HREF: &str = concat!("/assets/nav.js?v=", env!("IM_NAV_JS_V"));
17
18/// Link to the script of individual pages.
19pub const PAGES_JS_HREF: &str = concat!("/assets/pages.js?v=", env!("IM_PAGES_JS_V"));
20
21const MAIN_CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/main.css"));
22const NAV_JS: &str = include_str!("../assets/nav.js");
23const PAGES_JS: &str = include_str!("../assets/pages.js");
24const OFFLINE_HTML: &str = include_str!("../assets/offline.html");
25const SW_JS: &str = include_str!("../assets/sw.js");
26
27/// A content-versioned file. The URL changes with the bytes, so the browser may keep it for a year.
28const IMMUTABLE: &str = "public, max-age=31536000, immutable";
29
30/// A file whose URL stays the same across builds. The browser must ask again each time.
31const REVALIDATE: &str = "no-cache";
32
33/// The status, headers and body of an asset answer.
34type Asset = (StatusCode, HeaderMap, &'static str);
35
36/// Builds the answer for one asset: the content type and the cache policy.
37fn asset(content_type: &'static str, cache: &'static str, body: &'static str) -> Asset {
38 let mut headers = HeaderMap::new();
39 headers.insert(header::CONTENT_TYPE, HeaderValue::from_static(content_type));
40 headers.insert(header::CACHE_CONTROL, HeaderValue::from_static(cache));
41 (StatusCode::OK, headers, body)
42}
43
44/// `GET /assets/main.css`.
45#[route(GET "/assets/main.css")]
46async fn main_css() -> Result<Asset> {
47 Ok(asset("text/css; charset=utf-8", IMMUTABLE, MAIN_CSS))
48}
49
50/// `GET /assets/nav.js`.
51#[route(GET "/assets/nav.js")]
52async fn nav_js() -> Result<Asset> {
53 Ok(asset("text/javascript; charset=utf-8", IMMUTABLE, NAV_JS))
54}
55
56/// `GET /assets/pages.js`.
57#[route(GET "/assets/pages.js")]
58async fn pages_js() -> Result<Asset> {
59 Ok(asset("text/javascript; charset=utf-8", IMMUTABLE, PAGES_JS))
60}
61
62/// `GET /assets/offline.html`. Also precached by the service worker.
63#[route(GET "/assets/offline.html")]
64async fn offline_html() -> Result<Asset> {
65 Ok(asset("text/html; charset=utf-8", REVALIDATE, OFFLINE_HTML))
66}
67
68/// `GET /assets/sw.js`. It is served at the root scope, which needs the `Service-Worker-Allowed`
69/// header; without it the browser would limit the worker to `/assets/`.
70#[route(GET "/assets/sw.js")]
71async fn sw_js() -> Result<Asset> {
72 let (status, mut headers, body) = asset("text/javascript; charset=utf-8", REVALIDATE, SW_JS);
73 headers.insert("service-worker-allowed", HeaderValue::from_static("/"));
74 Ok((status, headers, body))
75}