Skip to content

Actions #588

Open

Description

@GauBen

Context / Problem to solve / Design

Actions are backend callbacks, exposed for asynchronous usage in the browser. They are a simpler alternative to GQL extensions, they don't live within a data graph.

Design

Here is the first design draft for actions written and used in JS. The design is inspired by SvelteKit remote functions.

// example.action.ts (Only the .action.ts extension matters, the file might be named anything and placed anywhere
export const getExchangeRate = async (...args) => {
//           ^^^^^^^^^^^^^^^ The export name is used to create an HTTP endpoint 
// (e.g. /modules/<name>/actions/<action>.do or equivalent)
// It must be unique across all .action.ts files

  // `args` is what the client sent over the wire, serialized on the client, parsed on the server 
  doStuff(args);
  return 123; // The return value is sent back to the client
}

Usage on the client:

// Rate.client.tsx
import { getExchangeRate } from "./example.action.ts";

export default function Rate({ initialValue }: { initialValue: number }) {
  const [rate, setRate] = useState(initialValue);

  return <button onClick={async () => {
    const newValue = await getExchangeRate(...args);
    setRate();
  }}>
    Rate: {rate}. Click to refresh.
  </button>
}

.action.ts files will be compiled twice: once for the server, once for the client. As far as TypeScript is concerned, we are calling a function on the client. The reality is different: we will be performing a network request. Thanks to React context we can hide the nasty implementation details (e.g. the context):

// example.action.ts gets compiled to something like this on the server:
defineAction("getExchangeRate", async (...args) => {
  doStuff(args);
  return 123;
});

// ...and to something like this on the client:
const getExchangeRate = async (...args) => {
  const ctx = use(IslandContext);
  const response = await fetch(ctx.base + "/getExchangeRate.do", {
    method: "POST",
    body: devalue.stringify(args)
  });
  const data = await response.text();
  return devalue.parse(data);
}

We'll call this implementation "raw actions", the base brick to build stuff, but we'll also expose "safe actions", i.e. actions with validation.

This will be done by a single function coming from @jahia/javascript-modules-library: action (how original)

// example.action.ts written as a safe action
import { action } from "@jahia/javascript-modules-library";

const InputSchema = /* any https://standardschema.dev/ compatible schema */ z.object({ foo: z.number() });

export const getExchangeRate = action(InputSchema, (args) => {
  // args was validated by InputSchema, this function is not called for invalid args
  // the args object type is inferred from the schema: `{ foo: number }`
});

Activity

  1. added theissue type on Dec 5, 2025
  2. added this to the javascript-modules 1.x milestone on Dec 5, 2025
  3. romain-pm commented on Jul 23, 2026

    @romain-pm

    Implemented on feature/js-server-extensions, under review in #687 (will close on merge).

    What shipped vs this design draft:

    • .action.ts files compiled twice exactly as sketched: server bundle registers every export; client bundle gets generated fetch stubs — importing an action from a client component is a plain typed await.
    • devalue wire format (args array in, result out); errors and validation issues travel back to the client Error.
    • Safe actions: action(schema, fn) accepting any Standard Schema v1 library (interface vendored, no dependency), input type inferred from the schema.
    • async handlers supported (microtask-only: the server runtime has no timers/async I/O; never-settling promises fail explicitly).
    • Endpoint v1 deviates from the draft's /modules/<name>/actions/<action>.do URL: a single engine-owned dispatcher (<pageUrl>.jsAction.do?name=<module>/<export>) riding the render servlet for auth/session handling, CSRF-hardened by a mandatory custom header. The module-scoped-servlet transport is the recorded intended evolution (Actions endpoint v2: dedicated servlet instead of riding org.jahia.bin.Action #690) — non-breaking, since both wire ends are platform-generated.
    • v1 limitation: only top-level export const/export function declarations are discovered (lexical extraction; AST-based discovery deferred).

    Design rationale and alternatives: ADR-0008 (plus ADR-0007 for why this feature holds the plain "action" name). Docs: guide 7-actions. Coverage: GraalJS promise-settling unit tests, vite-plugin snapshot fixture, Cypress wire + through-island specs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions