@bablic-io/sdk-core

中文文档
1.4.0发布于 2026/11/06MITTypeScriptESM + CJS近 30 天下载 2018

Engine behind Bablic server-side rendering: translates pages on the server with your Bablic project.

原始使用文档
文档目录6 节
  1. Install
  2. Translate one page
  3. Serve a host: createPageHandler
  4. Other exports
  5. Links
  6. License

@bablic-io/sdk-core

The engine behind server-side rendering for Bablic. It translates a page on the server with your Bablic project's translations and returns finished HTML in the visitor's language.

You need a Bablic account and project. New to Bablic? Sign up.

Most sites want an adapter instead:

Use this package directly to support another host. Requires Node.js 22.

Install

npm install @bablic-io/sdk-core

Translate one page

import { MemoryCache, translateDocument } from '@bablic-io/sdk-core';

const cache = new MemoryCache();

const result = await translateDocument({
  html, // original-language HTML, including the Bablic script tag
  url: 'https://example.com/fr-fr/pricing', // the translated URL being served (example)
  cache,
});

if (result.status === 'complete') {
  // result.html: translated page
  // result.textMap: original → translated strings, for framework data
  // result.links: your project's link rules and the links that changed
} else {
  // 'skipped': not translated on the server (plan without server-side rendering, or the
  // project doesn't translate this page); serve the original.
  // 'failed': never serve or cache the result; see result.reason.
}

translateDocument reads your project from the page's Bablic script tag, translates the page on the server, and waits until translation is complete.

Serve a host: createPageHandler

The request flow the adapters share: routing, fetching the original page from the same site, translating, caching, the editor bypass and the failure rule.

import { createPageHandler, MemoryCache } from '@bablic-io/sdk-core';

const pages = createPageHandler({ project: 'P0000', cache: new MemoryCache() });

// In your request handler:
const response = await pages.handle(request); // undefined: not a translated URL

A proxy that already fetched the original page passes it in instead, and gets a response back for every request:

const response = await pages.translateHtml({ request, html, status, headers });

Same routing, cache, editor bypass and failure rule as handle; the original is never fetched again, not even for background refreshes. A URL that isn't translated, or a page already translated on the server (x-moxie header, or <html data-moxie-mode="ssr">), comes back unchanged with x-moxie: pass and the reason.

For a URL that isn't translated, await pages.explain(request) says why (for example, which language folders the project has), for an x-moxie-reason header or a log line.

project is your Bablic project key. Translated URLs come from your Bablic dashboard settings (language folders or subdomains); projectResolver and routeFromConfig expose that routing on its own.

Options match the adapters (project, resolve, pageTtl, timeoutMs, cache, moxie, editorCookie, onResult, onUnsupported, host, fetch), plus host hooks: buildId, sourceHeaders, ignoreParams, dropRequestHeaders, transformHtml and absoluteRedirects. See the @bablic-io/sdk-vercel README for behavior, response headers and hosting rules.

Other exports

  • CacheStore: the cache interface (get, set with ttl and tags, expireTag). MemoryCache implements it.
  • createRevalidateHandler({ secret, cache }): signed purge endpoint that clears cached pages, or the cached project config with {"config": true}.
  • rewriteHref(href, rules), localizeHref(href, links): apply your project's link rules to a URL, as Bablic does in the page.
  • projectResolver({ project, cache }), routeFromConfig(config, url): your project's translated URLs.
  • isCrawler(userAgent), isStagingUrl(url).

License

MIT

版本与发布记录

@bablic-io/sdk-core 共发布 13 个版本,首次发布于 2026/10/14;过去 12 个月发布了 13 个版本。

版本发布日期距今
1.4.0latest2026/11/062 天前
1.3.02026/11/053 天前
1.2.02026/10/2910 天前
1.1.02026/10/2118 天前
1.0.02026/10/2118 天前
0.2.02026/10/1722 天前

关于 @bablic-io/sdk-core 的常见问题

@bablic-io/sdk-core 是什么?

Engine behind Bablic server-side rendering: translates pages on the server with your Bablic project.

如何安装 @bablic-io/sdk-core?

在项目目录运行 npm install @bablic-io/sdk-core,或使用 pnpm add / yarn add / bun add。

@bablic-io/sdk-core 支持 TypeScript 吗?

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

@bablic-io/sdk-core 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bablic-io/sdk-core 可以商用吗?

@bablic-io/sdk-core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bablic-io/sdk-core 的下载量有多少?

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

@bablic-io/sdk-core 还在维护吗?

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