Skip to main content

forma_server/
sw.rs

1use axum::{
2    http::header,
3    response::{IntoResponse, Response},
4};
5use rust_embed::Embed;
6
7/// Axum handler: serve the service worker with no-cache headers.
8///
9/// Mount at `/sw.js` in your router. Returns 404 if sw.js is not embedded.
10pub async fn serve_sw<A: Embed>() -> Response {
11    match crate::assets::asset_bytes::<A>("sw.js") {
12        Some(data) => {
13            let response = Response::builder()
14                .header(
15                    header::CONTENT_TYPE,
16                    "application/javascript; charset=utf-8",
17                )
18                .header(header::CACHE_CONTROL, "no-cache, no-store, must-revalidate")
19                .header("service-worker-allowed", "/")
20                .body(axum::body::Body::from(data))
21                .unwrap();
22            response.into_response()
23        }
24        None => axum::http::StatusCode::NOT_FOUND.into_response(),
25    }
26}