Links and redirects
Links, redirects, and navigation from an action or a statement are built from the route, so a URL with a parameter of the wrong type or in the wrong number does not compile.
Links
A Link<S> points at a page the router serves, for an element on the page whose signals are S. Route::link(&self) -> Link<S> builds one for a route without parameters. Route::link_with(&self, args) -> Link<S> builds one for a route with them, where args are expressions of the route's types, as the arguments of an action call are. A route with search parameters takes (P, Q), or Q alone where P is ().
El::href takes the link. It also takes a Url written on the server, and a SafeUrl for an address the router does not serve.
use hypersig::html::a;
use hypersig::{Page, PathSegment, Route};
#[derive(Signals, Serialize, Deserialize)]
struct Search {
team: PathSegment,
}
hypersig::routes! {
fn pages(Router<()>);
const MEMBER: Route<(PathSegment, i32)> = "/teams/{team}/members/{id}" => member;
}
// Literal parameters give a fixed href, percent-encoded on the server.
let fixed = a::<Search>().href(MEMBER.link_with((PathSegment::new("red team"), 7)));
assert_eq!(fixed.render(), r#"<a href="/teams/red%20team/members/7"></a>"#);
// A parameter that reads a signal is kept up to date in the browser.
let bound = a().href(MEMBER.link_with((Search::team(), 7)));
let page = Page::new("Team", Search { team: PathSegment::new("red") }).child(bound);
// The page's first HTML holds the URL of the signal's first value.
assert!(page.render().contains(r#"<a href="/teams/red/members/7""#));What a link writes
Literals give a fixed href. A parameter that reads a signal or a list item is evaluated in the browser and kept up to date: Page::render writes the URL of the signals' first values into href, and the browser rewrites href each time a signal the link reads changes. Both sides percent-encode a value as encodeURIComponent does, so the first HTML and the browser write the same URL. In the sample above, the page's first HTML holds <a href="/teams/red/members/7">.
What the compiler checks
El::href does not take a string, so a URL cannot be written by hand:
use hypersig::Route;
use hypersig::html::a;
hypersig::routes! {
fn pages(Router<()>);
const MEMBER: Route<i32> = "/members/{id}" => member;
}
// error[E0277]: `&str` is not a URL an `href` or a `src` takes on the page
// whose signals are `Team`
let _link = a::<Team>().href("/members/7");linkexists only onRoute<(), ()>, so calling it on a route with parameters is error E0599, "no method namedlink".- A parameter of the wrong type, such as
MEMBER.link_with("seven")for aRoute<i32>, is error E0277, "&stris not an expression of typei32".
Redirects and navigation
The server writes a page's URL with Route::url(&self, args) -> Url. It takes the route's parameter values themselves, in the shape link_with takes, and encodes them as a link does. Url::as_str and Display give the text.
- A page handler that sends the browser elsewhere returns a
PageResponse, built withPageResponse::redirect(url: Url), which answers303 See Other. Its usual page converts with.into(). - An action does the same with
Patches::navigate(url: Url). The browser follows it once the patches before it are applied, and the patches after it in the same answer are not applied. - In the browser,
Link::navigate(self) -> Stmt<S>is a statement that loads the linked page, as following the link would. It is for a handler that should leave the page after it runs other statements.
use hypersig::html::button;
use hypersig::{Action, PageResponse, Patches, Route};
#[derive(Signals, Serialize, Deserialize)]
struct Draft {
saving: bool,
}
hypersig::routes! {
fn pages(Router<()>);
const POST: Route<i32> = "/posts/{id}" => post;
const OLD_POST: Route<i32> = "/p/{id}" => old_post;
action PUBLISH: Action<Draft> = post "/publish" => publish;
}
// A page handler answers an old address with a redirect.
async fn old_post((): (), id: i32) -> PageResponse {
PageResponse::redirect(POST.url(id))
}
// An action sends the browser to the page it created.
async fn publish((): (), (): ()) -> Patches<Draft> {
let id = 7;
Patches::from(Draft::saving().patch(false)).navigate(POST.url(id))
}
// A test reads the page an action's answer sends the browser to.
let sent = Patches::<Draft>::new().navigate(POST.url(7));
assert_eq!(sent.navigates_to().map(|url| url.as_str()), Some("/posts/7"));
// A button leaves the page after setting a signal.
let _back = button::<Draft>().on_click(Draft::saving().set(false).then(POST.link_with(&1).navigate()));What links leave unchecked
- A link is a plain
<a href>, so following it loads the next page from the server, and the signals of the page you leave are gone. - A link to another site or to a
#fragmentis aSafeUrl, which Values the types do not check covers. It checks the URL's scheme, and nothing checks that the address it names exists.
Next
- Pages under a page links to a page whose path continues another's.
- Search parameters changes the URL in the address bar without loading a page.
- Error pages answers with a status of your choice.