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(header::CONTENT_TYPE, "application/javascript; charset=utf-8")
15                .header(header::CACHE_CONTROL, "no-cache, no-store, must-revalidate")
16                .header("service-worker-allowed", "/")
17                .body(axum::body::Body::from(data))
18                .unwrap();
19            response.into_response()
20        }
21        None => axum::http::StatusCode::NOT_FOUND.into_response(),
22    }
23}