Reference · Browser

url

signature
url() -> map

What it does

The address as { path, query, hash }: of the request on the server, and of the address bar in the browser, where what reads it follows it when it changes. path is the path within the program (under a mount, without the mount's prefix), query a map of the query's keys and values, decoded, and hash the part after # without the #. Once a component has called it, clicks on same-site links inside components change the page without reloading, and Back and Forward work.

Examples

in a program
component App() {    state cart = []                               // survives moving between pages    let path = url().path    <div>        <nav><a href="/">Home</a> <a href="/products">Products</a> Cart: {len(cart)}</nav>        {match split(path, "/") | filter((s) => s != "") {            [] => <Home/>            ["products"] => <ProductList/>            ["products", id] => <ProductPage key={id} id={int(id)} on_add={(p) => { cart = cart + [p] }}/>            _ => <NotFound {path}/>        }}    </div>}route GET /*rest = <html><body><App/></body></html>       // every path renders on the server
in a program
component Pager() {    let page = int(url().query.page ?? "1") ?? 1    <p>Page {page} <a href={`?page=${page + 1}`}>Next</a></p>}

Edge cases

print(url())let u = url()print(u.path, len(u.query), u.query.page ?? "1", u.hash == "")
output
{ path: "/", query: {}, hash: "" }/ 0 1 true

Errors

url() never fails: with no request every field is empty. What is made from it can, so a query value is converted with a default (int(q.page) ?? 1).

print(int(url().query.page) ?? 1)
output
1

Chaining

The map is picked apart with fields or a pattern; path splits into its parts:

let { path, query } = url()print(path | split("/") | filter((s) => s != ""))print(query | keys)
output
[][]

See also

navigate, url_encode, split, redirect

Thread
8Useful? Like it, or add to its thread below.

Thread 0 posts

On url(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo