sveltekit-remote-functions

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

SvelteKit Remote Functions

SvelteKit 远程函数

Quick Start

快速开始

File naming:
*.remote.ts
for remote function files
Which function? One-time action →
command()
| Repeated reads →
query()
| Forms →
form()
文件命名规则: 远程函数文件使用
*.remote.ts
命名
如何选择函数? 一次性操作 →
command()
| 重复读取操作 →
query()
| 表单处理 →
form()

Example

示例

typescript
// actions.remote.ts
import { command } from "$app/server";
import * as v from "valibot";

export const delete_user = command(
  v.object({ id: v.string() }),
  async ({ id }) => {
    await db.users.delete(id);
    return { success: true };
  }
);

// Call from client: await delete_user({ id: '123' });
typescript
// actions.remote.ts
import { command } from "$app/server";
import * as v from "valibot";

export const delete_user = command(
  v.object({ id: v.string() }),
  async ({ id }) => {
    await db.users.delete(id);
    return { success: true };
  }
);

// 从客户端调用:await delete_user({ id: '123' });

Reference Files

参考文档

  • references/remote-functions.md - Complete guide with all patterns
  • references/remote-functions.md - 包含所有使用模式的完整指南

Notes

注意事项

  • Remote functions execute on server when called from browser
  • Args/returns must be JSON-serializable
  • Schema validation via StandardSchemaV1 (Valibot/Zod)
  • getRequestEvent()
    available for cookies/headers access
  • In components: Use
    <svelte:boundary>
    +
    {@const await}
    (no flicker)
  • Refresh queries: Call
    query().refresh()
    - updates without flicker
  • Last verified: 2025-12-24
<!-- PROGRESSIVE DISCLOSURE GUIDELINES: - Keep this file ~50 lines total (max ~150 lines) - Use 1-2 code blocks only (recommend 1) - Keep description <200 chars for Level 1 efficiency - Move detailed docs to references/ for Level 3 loading - This is Level 2 - quick reference ONLY, not a manual LLM WORKFLOW (when editing this file): 1. Write/edit SKILL.md 2. Format (if formatter available) 3. Run: claude-skills-cli validate <path> 4. If multi-line description warning: run claude-skills-cli doctor <path> 5. Validate again to confirm -->
  • 远程函数在从浏览器调用时会在服务器端执行
  • 参数/返回值必须是可JSON序列化的
  • 可通过StandardSchemaV1(Valibot/Zod)进行 Schema 验证
  • 可使用
    getRequestEvent()
    访问Cookie/请求头
  • 在组件中使用: 结合
    <svelte:boundary>
    +
    {@const await}
    使用(避免页面闪烁)
  • 刷新查询: 调用
    query().refresh()
    - 无闪烁更新数据
  • 最后验证时间: 2025-12-24
<!-- 渐进式披露指南: - 保持文件总长度约50行(最多约150行) - 仅使用1-2个代码块(推荐1个) - 描述部分控制在200字符以内,以实现一级效率 - 将详细文档移至references/目录,用于三级加载 - 本文件为二级内容 - 仅作为快速参考,而非完整手册 LLM 编辑工作流: 1. 编写/编辑SKILL.md 2. 格式化(如果有格式化工具) 3. 运行:claude-skills-cli validate <path> 4. 如果出现多行描述警告:运行claude-skills-cli doctor <path> 5. 再次验证以确认 -->