forge
Explore
Log in

Don't you dare go Hollow

in

Clone
★ 0

0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits

assets.rs100 lines, 4.1 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/in-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!("IN_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!("IN_NAV_JS_V"));
17
18/// Link to the script of individual pages.
19pub const PAGES_JS_HREF: &str = concat!("/assets/pages.js?v=", env!("IN_PAGES_JS_V"));
20
21/// Link to the script of the viewer: the keyboard steps between files. Every page loads it; it does
22/// nothing unless a page has previous and next links.
23pub const VIEWER_JS_HREF: &str = concat!("/assets/viewer.js?v=", env!("IN_VIEWER_JS_V"));
24
25/// Link to the upload queue of the drive pages. Only those pages load it.
26pub const DRIVE_JS_HREF: &str = concat!("/assets/drive.js?v=", env!("IN_DRIVE_JS_V"));
27
28const MAIN_CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/main.css"));
29const NAV_JS: &str = include_str!("../assets/nav.js");
30const PAGES_JS: &str = include_str!("../assets/pages.js");
31const DRIVE_JS: &str = include_str!("../assets/drive.js");
32const VIEWER_JS: &str = include_str!("../assets/viewer.js");
33const OFFLINE_HTML: &str = include_str!("../assets/offline.html");
34const SW_JS: &str = include_str!("../assets/sw.js");
35
36/// A content-versioned file. The URL changes with the bytes, so the browser may keep it for a year.
37const IMMUTABLE: &str = "public, max-age=31536000, immutable";
38
39/// A file whose URL stays the same across builds. The browser must ask again each time.
40const REVALIDATE: &str = "no-cache";
41
42/// The status, headers and body of an asset answer.
43type Asset = (StatusCode, HeaderMap, &'static str);
44
45/// Builds the answer for one asset: the content type and the cache policy.
46fn asset(content_type: &'static str, cache: &'static str, body: &'static str) -> Asset {
47 let mut headers = HeaderMap::new();
48 headers.insert(header::CONTENT_TYPE, HeaderValue::from_static(content_type));
49 headers.insert(header::CACHE_CONTROL, HeaderValue::from_static(cache));
50 (StatusCode::OK, headers, body)
51}
52
53/// `GET /assets/main.css`.
54#[route(GET "/assets/main.css")]
55async fn main_css() -> Result<Asset> {
56 Ok(asset("text/css; charset=utf-8", IMMUTABLE, MAIN_CSS))
57}
58
59/// `GET /assets/nav.js`.
60#[route(GET "/assets/nav.js")]
61async fn nav_js() -> Result<Asset> {
62 Ok(asset("text/javascript; charset=utf-8", IMMUTABLE, NAV_JS))
63}
64
65/// `GET /assets/pages.js`.
66#[route(GET "/assets/pages.js")]
67async fn pages_js() -> Result<Asset> {
68 Ok(asset("text/javascript; charset=utf-8", IMMUTABLE, PAGES_JS))
69}
70
71/// `GET /assets/drive.js`.
72#[route(GET "/assets/drive.js")]
73async fn drive_js() -> Result<Asset> {
74 Ok(asset("text/javascript; charset=utf-8", IMMUTABLE, DRIVE_JS))
75}
76
77/// `GET /assets/viewer.js`.
78#[route(GET "/assets/viewer.js")]
79async fn viewer_js() -> Result<Asset> {
80 Ok(asset(
81 "text/javascript; charset=utf-8",
82 IMMUTABLE,
83 VIEWER_JS,
84 ))
85}
86
87/// `GET /assets/offline.html`. Also precached by the service worker.
88#[route(GET "/assets/offline.html")]
89async fn offline_html() -> Result<Asset> {
90 Ok(asset("text/html; charset=utf-8", REVALIDATE, OFFLINE_HTML))
91}
92
93/// `GET /assets/sw.js`. It is served at the root scope, which needs the `Service-Worker-Allowed`
94/// header; without it the browser would limit the worker to `/assets/`.
95#[route(GET "/assets/sw.js")]
96async fn sw_js() -> Result<Asset> {
97 let (status, mut headers, body) = asset("text/javascript; charset=utf-8", REVALIDATE, SW_JS);
98 headers.insert("service-worker-allowed", HeaderValue::from_static("/"));
99 Ok((status, headers, body))
100}