@askrjs/askr

中文文档
0.5.1发布于 2026/11/02Apache-2.0TypeScriptES Module零依赖构建来源已验证近 30 天下载 2742

A TypeScript UI runtime with getter-based state, fine-grained bindings, transactional commits with rollback, and one route graph for SPA, SSR, and SSG.

原始使用文档
文档目录12 节
  1. How Askr works
  2. Quick Start
  3. What It Provides
  4. Runtime
  5. Getter-based state
  6. Routing and app startup
  7. Async resources
  8. Developer error boundaries
  9. Documentation
  10. Release Notes
  11. Install
  12. License

Askr

CI npm version

A TypeScript UI runtime with getter-based state, fine-grained bindings, transactional commits with rollback, and one route graph for SPA, SSR, and SSG.

How Askr works

  • Getter-based state with automatic tracking. state() gives you a getter and a setter. Calling the getter while a component renders subscribes that component; you do not list dependencies for state or derive().
  • Component re-render plus fine-grained bindings. A state change re-runs the components that read it. A function child or prop, such as {() => count()}, is a binding: it updates its own DOM node without re-running the component around it.
  • Transactional commits with rollback. Each render is prepared and applied as one transaction. If a component throws or the DOM commit fails, Askr restores the last committed DOM, and the listeners, refs, resources, and subscriptions created by the failed render never become live. The error goes to the nearest ErrorBoundary, or is rethrown when no boundary catches it. Bindings keep showing current state across a rollback.
  • One route graph. The registry from createRouteRegistry() drives browser navigation (createSPA), hydration (hydrateSPA), server rendering (renderRouteRequest), and static generation (createStaticGen).

Writes made in one event handler are batched into one scheduled render, and queued work runs in a fixed order. See Update ordering for exactly what Askr guarantees and what it does not.

Quick Start

import { state } from '@askrjs/askr';
import { createIsland } from '@askrjs/askr/boot';

function Counter() {
  const [count, setCount] = state(0);

  return (
    <button onClick={() => setCount((value) => value + 1)}>{count()}</button>
  );
}

createIsland({ root: document.body, component: Counter });

The tuple form above and the direct callable form below are equivalent; see State usage forms for when each is convenient.

What It Provides

Runtime

@askrjs/askr exports the core runtime primitives: state(), derive(), selector(), defineScope(), readScope(), getSignal(), and the JSX runtime exports.

Public APIs are mostly functions and closures. Error classes such as RouteDataLoadError and SSRDataMissingError live on their subpaths. Lexical ownership uses defineScope() and readScope(); there are no compatibility aliases for the clean-break vocabulary.

App startup, routing, async resources, data helpers, and error boundaries live on their own subpaths.

Getter-based state

State is read through getter functions and updated through setter functions. Reads are explicit calls, but dependency tracking is automatic: a component re-renders when state it read during its last committed render changes.

state(), derive(), selector(), resource(), and the other render-scoped primitives are hooks in the React sense. Each call is matched to its slot by call position, so call them unconditionally and in the same order on every render; the runtime throws when the order changes (see Runtime Enforcement). resource() takes a dependency array that decides when its loader re-runs.

State usage forms

state() returns one callable state cell that is also iterable. Destructuring it gives the cell as the getter and its .set() method as the setter. Call state() inside a component function:

const [count, setCount] = state(0);
console.log(count());
setCount(1);

The same state can be kept as one value and updated through .set():

const count = state(0);
console.log(count());
count.set(1);

Prefer destructuring when local getter and setter names make event handlers clear. Prefer the direct form when passing the state cell around as one value or when keeping reads and writes under one name. Both forms have the same tracking, scheduling, and update semantics.

Routing and app startup

Startup belongs in @askrjs/askr/boot. Routing helpers live in @askrjs/askr/router.

import { createSPA } from '@askrjs/askr/boot';
import { createRouteRegistry, route } from '@askrjs/askr/router';

const registry = createRouteRegistry(() => {
  route('/', Home);
  route('/about', About);
});

createSPA({
  root: document.body,
  registry,
});

Async resources

resource() manages async work with cancellation support.

import { resource } from '@askrjs/askr/resources';

function Data({ id }: { id: string }) {
  const data = resource(
    async ({ signal }) => {
      const response = await fetch(`/api/${id}`, { signal });
      return response.json();
    },
    [id]
  );

  if (data.pending) return <div>Loading...</div>;
  if (data.error) return <div>Failed to load</div>;
  return <div>{data.value?.name}</div>;
}

@askrjs/askr/data is the only entrypoint for query, mutation, invalidation, and data-runtime helpers; the root package does not re-export them.

Developer error boundaries

ErrorBoundary is the opt-in boundary primitive for render-time failures. It renders a visible fallback in every environment (with error details expanded by default in development), still logs the underlying error, and can reset via a resetKey tied to your app state. The boundary protects both initial mount and scheduled post-mount updates. Portal content follows its logical writer boundary and can also recover through a boundary around its host.

import { ErrorBoundary } from '@askrjs/askr/components';

function App() {
  return (
    <ErrorBoundary fallback={<div>Something went wrong</div>}>
      <FlakyView />
    </ErrorBoundary>
  );
}

Documentation

Release Notes

The published package is versioned with the repository package.json. Release workflows validate the version tag before publishing.

Install

npm install @askrjs/askr

License

Apache 2.0

版本与发布记录

@askrjs/askr 共发布 108 个版本,首次发布于 2025/12/22;过去 12 个月发布了 108 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.1latest2026/11/025 天前
0.5.02026/10/1424 天前
0.4.42026/10/0930 天前
0.4.32026/10/021 个月前
0.4.22026/10/021 个月前
0.4.12026/10/021 个月前

关于 @askrjs/askr 的常见问题

@askrjs/askr 是什么?

A TypeScript UI runtime with getter-based state, fine-grained bindings, transactional commits with rollback, and one route graph for SPA, SSR, and SSG.

如何安装 @askrjs/askr?

在项目目录运行 npm install @askrjs/askr,或使用 pnpm add / yarn add / bun add。

@askrjs/askr 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@askrjs/askr 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@askrjs/askr 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@askrjs/askr 可以商用吗?

@askrjs/askr 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@askrjs/askr 的下载量有多少?

根据 npm 官方下载统计,@askrjs/askr 在 2026/10/07 – 2026/11/05 这 30 天内被下载 2,742 次,日均约 91 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@askrjs/askr 还在维护吗?

最新版本 0.5.1 发布于 2026/11/02(5 天前);至今共发布 108 个版本,其中过去 12 个月发布了 108 个;首次发布于 2025/12/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。