Agent Wranglers

APP-31Recommended

Keep screen state in the address

Put every choice that changes what a screen shows in the address, such as filters, sort, page, tab and the open record's random ID. Keep free-text search out of it when it may hold a name, email or phone number. Reloading or sharing the link shows the same screen.

How to do it

Read the state from the query string when the screen loads, and write it back when it changes. Use replace for typing and push for steps people expect Back to undo, such as opening a record or moving to another page of results. Validate each parameter and fall back to a default for bad values. Leave defaults out, so links stay short. Never put personal data or tokens in the address. A search box over people, such as customers or staff, keeps its text in history.state or a POST body, not the query string. The privacy reason is in keep personal data out of addresses.

Examples

TypeScript example

const SORTS = ['newest', 'oldest', 'amount'] as const;
type Sort = (typeof SORTS)[number];

export function readOrderFilters(url = new URL(location.href)) {
  const sort = url.searchParams.get('sort');
  return {
    status: url.searchParams.get('status') ?? 'any',
    sort: (SORTS as readonly string[]).includes(sort ?? '') ? (sort as Sort) : 'newest',
    cursor: url.searchParams.get('cursor'),
  };
}

export function writeOrderFilters(filters: Record<string, string | null>, mode: 'push' | 'replace') {
  const url = new URL(location.href);
  for (const [name, value] of Object.entries(filters)) value ? url.searchParams.set(name, value) : url.searchParams.delete(name);
  history[mode === 'push' ? 'pushState' : 'replaceState'](null, '', url);
}

Python example

SORTS = {"newest", "oldest", "amount"}

@app.get("/orders")
def order_list():
    sort = request.args.get("sort", "newest")
    filters = OrderFilters(
        status=request.args.get("status", "any"),
        sort=sort if sort in SORTS else "newest",
        cursor=request.args.get("cursor"),
    )
    return render_template("orders/list.html", page=orders.list(current_user().account_id, filters), filters=filters)

How to check

A browser test sets a filter, a sort and page 3, copies the address and opens it in a new context. The same rows show. Back returns to the previous page of results. Searching a list of people for a test email leaves 0 matches in the address bar, the access log and the browser history.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/app-ui/client-and-server-state/state-in-url">Agent Wranglers web app interface playbook</a>. In code you write, add a comment next to it with the same link.