Skip to content

Action governance ​

The server doesn't send JS; it sends { "fn": "dom.scrollTo", "args": {...}, "on": "then" }, and the browser looks it up in a table and runs it. That table is the complete answer to "the most the server can make the browser do", so manage it like permissions.

Two tables ​

SideWhereContents
RustBUILTIN_ACTIONS in crates/cg-component/src/runtime/linker/response.rsAction names + argument specs
JSresources/js/cg/linker/actions.js (synced from PHP) + your own registered with registerLinkerActionAction names + implementations
rust
pub const BUILTIN_ACTIONS: &[(&str, &[(&str, &str)])] = &[
    ("dom.scrollTo", &[("target", "string")]),
    ("dom.class", &[("target", "string"), ("class", "string"), ("on", "bool?")]),
    ("state.set", &[("scope", "string"), ("key", "string"), ("value", "mixed")]),
    ("render.fragment", &[("target", "string"), ("name", "string"), ("params", "array?"), ("fresh", "bool?")]),
    ("ui.toast", &[("level", "string"), ("message", "string")]),
    // …
];
js
// actions.js (illustrative)
register('dom.scrollTo', (args, context) => {
    within(context, args.target)?.scrollIntoView({ behavior: 'smooth', block: 'start' });
});

The full built-in list and the Rust shortcuts are in Responses and actions.

ui.test

BUILTIN_ACTIONS contains ui.test (copied from PHP's LinkerServiceProvider), but in PHP's actions.js it's written after the // make:linker-action marker, as the PHP project's own demo action. The sync script only copies the built-in actions before the marker, so the browser has no ui.test. Don't use it; using it warns in the browser and jumps to catch.

Three checks ​

mermaid
flowchart LR
    A["BUILTIN_ACTIONS<br/>compile time"] -->|"the table is a constant"| B["Action::new<br/>when the response is built"]
    B -->|"name format, missing argument,<br/>extra argument, wrong type → Err"| C["when the browser runs it"]
    C -->|"no such JS action → warning,<br/>treated as failure, jumps to catch"| D[done]
  1. Types: argument types can only be string int number bool array (array or object) mixed (a ? suffix means optional; null counts as omitted).
  2. When the response is built: Action::new checks the arguments of built-in names and returns Err(String) when they're wrong; in call, .map_err(laravel::Error::msg)? turns it into fatal. Names must match ^[a-z][a-z0-9]*\.[a-z][a-zA-Z0-9]*$. The shortcuts (Action::toast etc.) have typed arguments and can't fail.
  3. When it runs: if JS has no such action → a console warning, the pipeline is treated as failed, and only catch / always run after that.

Where this differs from PHP

PHP's ->call('ui.toats', …) throws on the server. Rust's Action::new lets every name not in the built-in table through (as a custom action you registered in JS), so a typo is only discovered in the browser. Prefer the shortcuts for built-in actions.

Adding an action ​

Rust has no make:linker-action. For your application's own actions, don't edit BUILTIN_ACTIONS or actions.js (the next sync overwrites the latter):

1. The JS implementation ​

js
// resources/js/site.js (or a file it imports)
import { registerLinkerAction, within } from './cg/runtime.js';

registerLinkerAction('app.spinner', (args, context) => {
    const el = within(context, args.target);
    if (!el) throw new Error('app.spinner: not found ' + args.target);
    el.toggleAttribute('aria-busy', args.on ?? true);
});

2. Calling it from Rust ​

rust
let spinner = Action::new("app.spinner", json!({ "target": "#report", "on": false }))
    .map_err(laravel::Error::msg)?;

Ok(LinkerResponse::ok().then(spinner))

Custom actions get no argument checks, so wrap them so callers can't pass the wrong thing:

rust
// src/cg/actions.rs
use cg_component::runtime::linker::Action;
use serde_json::json;

pub fn spinner(target: &str, on: bool) -> Action {
    Action::new("app.spinner", json!({ "target": target, "on": on })).expect("app.spinner arguments")
}

Use your own prefix such as app., separate from the built-in dom. form. state. render. ui., so it's obvious which ones the application added.

Rules for a good action ​

RuleWhy
Arguments are data, not codeNo JS strings, HTML strings or function names. target is only used as a selector
Small and generaldom.class beats app.highlightInvoiceRow; the list should be small enough for one person to read
Write to the DOM with textContent / setAttributeArguments come from the server, but are always treated as data
Find selectors with within(context, …)Looks only inside the mount point, never elsewhere on the page
Throw when it can failA throw → the pipeline jumps to catch; silently skipping makes debugging hard
Return a Promise when asyncThe pipeline awaits it, and the next step waits for it
Never add eval-like actionsOnce one exists, the whole table loses its meaning

What an action receives ​

The action function's signature is (args, context):

contextContents
elementThe element that triggered this call
hostThe Linker mount point
callLinker(method, args)Calls another method on the same mount point (it already holds the queue, so it runs directly)

Limits ​

  • A pipeline has at most 20 steps; beyond that only the first 20 run, with a warning.
  • A nonexistent action name = failure; there's no guessing.
  • Action JS is code inside the bundle, so changing it needs a rebuild. That's exactly what guarantees "the server can't add capabilities on the fly".

Review ​

PRs that add or change actions should at least check:

  • [ ] JS has registerLinkerAction, and Rust has a matching wrapper function
  • [ ] The action audit passes: every name Rust uses exists in JS
  • [ ] Nothing in the arguments can be executed as code
  • [ ] Writes to the DOM don't go through innerHTML
  • [ ] It really can't be built from existing actions