Crates.io | inertia_rs |
lib.rs | inertia_rs |
version | 0.2.0 |
source | src |
created_at | 2021-08-04 20:19:49.724568 |
updated_at | 2021-08-07 19:43:45.749548 |
description | Inertia.js for Rust |
homepage | https://github.com/stuarth/inertia-rs |
repository | https://github.com/stuarth/inertia-rs |
max_upload_size | |
id | 431707 |
size | 14,534 |
Inertia.js implementations for Rust. Currently supports Rocket.
From inertiajs.com
Inertia is a new approach to building classic server-driven web apps. We call it the modern monolith.
Inertia allows you to create fully client-side rendered, single-page apps, without much of the complexity that comes with modern SPAs. It does this by leveraging existing server-side frameworks.
Inertia has no client-side routing, nor does it require an API. Simply build controllers and page views like you've always done!
Inertia.rs brings a straightforward integration to Rust.
Add the following line to your Cargo.toml
inertia_rs = { version = "0.2.0", features = ["rocket"] }
inertia_rs
defines a succinct interface for creating Inertia.js apps in Rocket. It's comprised of two elements,
Inertia<T>
a Responder that's generic over T
, the Inertia component's properties
VersionFairing
Responsible for asset version checks. Constructed via VersionFairing::new
, which is given the asset version and a closure responsible for generating the Inertia's HTML template.
#[macro_use]
extern crate rocket;
use inertia_rs::rocket::{Inertia, VersionFairing};
use rocket::response::Responder;
use rocket_dyn_templates::Template;
#[derive(serde::Serialize)]
struct Hello {
some_property: String,
}
#[get("/hello")]
fn hello() -> Inertia<Hello> {
Inertia::response(
// the component to render
"hello",
// the props to pass our component
Hello { some_property: "hello world!".into() },
)
}
#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/", routes![hello])
.attach(Template::fairing())
// Version fairing is configured with current asset version, and a
// closure to generate the html template response
// `ctx` contains `data_page`, a json-serialized string of
// the inertia props
.attach(VersionFairing::new("1", |request, ctx| {
Template::render("app", ctx).respond_to(request)
}))
}