grainlet-adapters

中文文档
1.0.1发布于 2026/11/04MITTypeScriptES Module零依赖

Deploy Grainlet SSR apps to Vercel, Cloudflare, and other Web-standard runtimes

原始使用文档
文档目录6 节
  1. Runtime
  2. Vercel
  3. Cloudflare Workers
  4. Vite build output
  5. Vercel
  6. Cloudflare

grainlet-adapters

Deploy Grainlet SSR apps to Vercel and Cloudflare using the Web Request/Response handler from grainlet/ssr. This package does not put platform APIs into grainlet core.

npm install grainlet-adapters

Peer: grainlet. Optional peer: vite (for grainlet-adapters/vite).

Runtime

Use the same App + routes object as the Node SSR template. Keep /client.js as the production script name.

Vercel

import { createVercelHandler } from 'grainlet-adapters/vercel';
import { App, routes } from './App.jsx';

export default createVercelHandler({
  App,
  routes,
  document: { scripts: ['/client.js'] },
});

Export that module as the function entry. The handler accepts a Web Request and optional Vercel context.

Cloudflare Workers

import { createCloudflareHandler } from 'grainlet-adapters/cloudflare';
import { App, routes } from './App.jsx';

export default createCloudflareHandler({
  App,
  routes,
  document: { scripts: ['/client.js'] },
});

fetch(request, env, ctx) passes { env, ctx } as platformContext. If env.ASSETS.fetch exists, static files are served first; 404s fall through to SSR. npx wrangler deploy from the generated Cloudflare output directory.

createGrainletHandler is the shared Web factory used by both adapters. Pass assetPrefix (or GRAINLET_ASSET_PREFIX) when scripts are not same-origin. streaming: false buffers HTML for runtimes that do not stream.

Vite build output

Keep grainJsx() from grainlet-vite. Add grainletPlatform() to the SSR Vite config so it runs after the server bundle:

import { defineConfig } from 'vite';
import { grainJsx } from 'grainlet-vite';
import { grainletPlatform } from 'grainlet-adapters/vite';

export default defineConfig({
  plugins: [
    grainJsx(),
    grainletPlatform({
      target: 'vercel', // or 'cloudflare'
      clientOutDir: 'dist/client',
      prerender: true, // or ['/', '/about']
      App, // required when prerender is enabled
      routes,
    }),
  ],
  esbuild: { jsx: 'automatic', jsxImportSource: 'grainlet' },
  build: {
    outDir: 'dist/server',
    rollupOptions: {
      input: 'src/vercel.js',
      output: { entryFileNames: 'server.js' },
    },
    ssr: true,
  },
});

Build order: vite build --config vite.client.config.js then the SSR config.

Vercel

Emits Build Output API v3 under .vercel/output/:

  • static/ — client assets and prerendered HTML
  • functions/ssr.func/ — Edge function wrapping the SSR bundle
  • config.json — filesystem routes first, then catch-all /ssr

Deploy with the Vercel CLI from the project root (vercel deploy --prebuilt).

Cloudflare

Emits dist/cloudflare/:

  • assets/ — client files and prerendered HTML
  • worker.js — Workers fetch export
  • wrangler.json — main + assets binding ASSETS
npx wrangler deploy --config dist/cloudflare/wrangler.json

Dynamic routes stay on the SSR function. Prerendered paths are static files and win over the catch-all.

The Node createNodeHandler template remains the default create-grainlet --ssr starter. Point the SSR Vite input at src/vercel.js or src/cloudflare.js when targeting a cloud.

版本与发布记录

grainlet-adapters 共发布 2 个版本,首次发布于 2026/08/22;过去 12 个月发布了 2 个版本。

版本发布日期距今
1.0.1latest2026/11/04今天
1.0.02026/08/222 个月前

关于 grainlet-adapters 的常见问题

grainlet-adapters 是什么?

将Grainlet SSR应用部署到Vercel、Cloudflare和其他web标准运行时

如何安装 grainlet-adapters?

在项目目录运行 npm install grainlet-adapters,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:grainlet(^1.1.11)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

grainlet-adapters 支持 TypeScript 吗?

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

grainlet-adapters 支持 ESM 还是 CommonJS?

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

grainlet-adapters 需要什么版本的 Node.js?

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

grainlet-adapters 可以商用吗?

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

grainlet-adapters 还在维护吗?

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