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
| Side | Where | Contents |
|---|---|---|
| Rust | BUILTIN_ACTIONS in crates/cg-component/src/runtime/linker/response.rs | Action names + argument specs |
| JS | resources/js/cg/linker/actions.js (synced from PHP) + your own registered with registerLinkerAction | Action names + implementations |
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")]),
// …
];// 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
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]- Types: argument types can only be
stringintnumberboolarray(array or object)mixed(a?suffix means optional;nullcounts as omitted). - When the response is built:
Action::newchecks the arguments of built-in names and returnsErr(String)when they're wrong; incall,.map_err(laravel::Error::msg)?turns it intofatal. Names must match^[a-z][a-z0-9]*\.[a-z][a-zA-Z0-9]*$. The shortcuts (Action::toastetc.) have typed arguments and can't fail. - When it runs: if JS has no such action → a console warning, the pipeline is treated as failed, and only
catch/alwaysrun 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
// 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
let spinner = Action::new("app.spinner", json!({ "target": "#report", "on": false }))
.map_err(laravel::Error::msg)?;
Ok(LinkerResponse::ok().then(spinner))3. Wrap it in a typed function (recommended)
Custom actions get no argument checks, so wrap them so callers can't pass the wrong thing:
// 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
| Rule | Why |
|---|---|
| Arguments are data, not code | No JS strings, HTML strings or function names. target is only used as a selector |
| Small and general | dom.class beats app.highlightInvoiceRow; the list should be small enough for one person to read |
Write to the DOM with textContent / setAttribute | Arguments 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 fail | A throw → the pipeline jumps to catch; silently skipping makes debugging hard |
| Return a Promise when async | The pipeline awaits it, and the next step waits for it |
Never add eval-like actions | Once one exists, the whole table loses its meaning |
What an action receives
The action function's signature is (args, context):
context | Contents |
|---|---|
element | The element that triggered this call |
host | The 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