Skip to main content

larust_spa/
lib.rs

1//! `@spa`'s client runtime - hotloaded, SPA-style page navigation (History
2//! API fetch-and-swap instead of a full reload). See `docs/MACROS.md`'s
3//! `@spa` section for the full directive design; this crate is
4//! deliberately tiny, and deliberately its own crate rather than folded
5//! into `larust-live`.
6//!
7//! **Why its own crate, not `larust-live`**: `@spa` shares no machinery at
8//! all with `larust-live`'s reactive-component (`@wire`) or server-push
9//! (`@live`) features - no component registry, no session-backed mount, no
10//! WebSocket. Every request `@spa` navigates to is rendered exactly the
11//! same full HTML page `view!(...)` already produces for a hard reload;
12//! the client extracts what changed via `DOMParser` and swaps it in. There
13//! is no server-side rendering path for this feature at all - this crate
14//! is, in full, one vendored static JS asset and the handler that serves
15//! it, the same "thin sibling crate" shape `larust-reverb` already
16//! establishes for a broadcast/socket variant of `larust-live::push` that
17//! likewise didn't belong folded into that crate's own identity.
18//!
19//! **No feature flag** - re-exported unconditionally through
20//! `larust_support::spa`, unlike the optional Tier-1 shim crates
21//! (`permissions`/`reverb`/`sanctum`/`sitemap`/`socialite`, each a
22//! Laravel-third-party-package equivalent an app opts into via a Cargo
23//! feature). `@spa` is core template-directive surface, the same tier as
24//! `wire`/`push`.
25//!
26//! **Route registration is the app's own job**, not automatic - same
27//! explicit convention `/__larust_wire/*`/`/__larust_push/*` already use
28//! (see `demo/routes/web.rs`). An app that uses `@spa ... @endspa`
29//! anywhere registers exactly one route, most easily via [`SpaPlugin`]:
30//!
31//! ```ignore
32//! .plugin(larust_support::spa::SpaPlugin)
33//! ```
34//!
35//! Unlike `wire`/`push`, there is no second route - no `/__larust_spa/{id}`
36//! endpoint of any kind - because there is no server-side counterpart to
37//! this feature at all; ordinary page routes already serve everything
38//! `@spa` needs.
39
40use axum::http::header::CONTENT_TYPE;
41use axum::response::IntoResponse;
42
43const RUNTIME_JS: &str = include_str!("../assets/spa-runtime.js");
44
45/// Serves the vendored `spa-runtime.js` client script - see this crate's
46/// own doc comment for the route the app must register this under.
47pub async fn runtime_js() -> impl IntoResponse {
48    (
49        [(CONTENT_TYPE, "text/javascript; charset=utf-8")],
50        RUNTIME_JS,
51    )
52}
53
54/// `@spa`'s one route, bundled for [`larust_http::Router::plugin`] - sugar
55/// for the single `.get(...)` call this crate's own doc comment shows an
56/// app writing by hand today.
57pub struct SpaPlugin;
58
59impl larust_http::Plugin for SpaPlugin {
60    fn routes(&self) -> larust_http::Router {
61        larust_http::Router::new().get("/__larust_spa/runtime.js", runtime_js)
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn spa_plugin_contributes_exactly_the_one_route_an_app_used_to_hand_write() {
71        let routes = larust_http::Router::new().plugin(SpaPlugin).routes();
72
73        assert_eq!(routes.len(), 1);
74        assert_eq!(routes[0].method, "GET");
75        assert_eq!(routes[0].path, "/__larust_spa/runtime.js");
76    }
77}