Documentation
API

Define Server Action (Optional)

Optional server-side actions for SSR-friendly data loading.

Server actions are optional. They are useful when you want to load data during SSR or trigger server mutations directly from server components.

Create:

src/features/web/todo/actions/todo.server.ts

Copy-paste scaffold

import { os } from '@orpc/server';

import {
  CreateTodoInputSchema,
  ListTodosOutputSchema,
  TodoSchema,
} from '@/features/web/todo/model/todo.schema';

import {
  createTodo,
  listTodos,
} from '@/features/web/todo/service/todo.service';

export const getTodos = os
  .output(ListTodosOutputSchema)
  .handler(async () => {
    return listTodos();
  })
  .callable();

export const createTodoAction = os
  .input(CreateTodoInputSchema)
  .output(TodoSchema)
  .handler(async ({ input }) => {
    return createTodo(input);
  })
  .actionable();

When to use this

  • SSR page needs initial data before rendering.
  • You want a simple server mutation flow from forms/actions.
  • You want to keep logic in one service layer and reuse it from both router and server action.

On this page