@basenative/share

中文文档
3.0.0发布于 2026/10/31Apache-2.0TypeScriptES Module零依赖构建来源已验证

Native Web Share + clipboard fallback, share-card mint client+server, and OG-redirect landing page generator for BaseNative apps

原始使用文档
文档目录7 节
  1. Install
  2. Client — sharing from the browser
  3. Server — minting + landing
  4. Hooking up @basenative/og-image
  5. Custom domains, custom roots
  6. Links
  7. License

@basenative/share

Three things every shareable BaseNative app needs, packaged together:

  1. Native share + clipboard fallback for the browser.
  2. Share-card mint — server-side store + POST handler that turns a game/post/result into a stable short URL.
  3. OG-redirect landing page — crawler-facing HTML at /s/{id} so iMessage / Discord / Twitter / LinkedIn render the right card image, while real visitors get JS-redirected onward.

Composes with @basenative/og-image for the per-share PNG.

Install

pnpm add @basenative/share
# Optional — server-side PNG rendering
pnpm add @basenative/og-image

Client — sharing from the browser

import { nativeShare, mintShareCard, composeShareText } from '@basenative/share/client';

const { id, url } = await mintShareCard({
  sessionId, category, score, won, grid,
});

const text = composeShareText(
  'T4BS · ${category} · ${score}pts · ${verdict}\n\n${grid}',
  { category, score, verdict: won ? 'Solved' : 'Busted', grid }
);

const result = await nativeShare({ text, url });
// result.status: 'shared' | 'copied' | 'failed'

nativeShare uses the Web Share API where available, falls back to navigator.clipboard.writeText with the URL appended, and respects user cancel (AbortError) so we don't ambush them by silently copying instead.

Server — minting + landing

In functions/api/share-cards.js:

import { defineShareCards, mintHandler } from '@basenative/share/server';

export const onRequestPost = async (ctx) => {
  const store = defineShareCards({ db: ctx.env.DB });
  return mintHandler({
    store,
    validate: (b) => {
      if (typeof b.category !== 'string' || !b.category) return 'bad-category';
      if (typeof b.score !== 'number') return 'bad-score';
      return true;
    },
  })(ctx);
};

In functions/s/[id].js:

import { defineShareCards, landingHandler } from '@basenative/share/server';

export const onRequestGet = async (ctx) => {
  const store = defineShareCards({ db: ctx.env.DB });
  return landingHandler({
    store,
    ogImage: (_card, { origin, id }) => `${origin}/og/score/${id}.png`,
    buildMeta: (card, { origin, id, ogImage }) => ({
      title: `T4BS — ${card.won ? 'Solved' : 'Busted'} ${card.category} for ${card.score}pts`,
      description: `Five-letter battle of bullshit — ${card.category}.`,
      imageUrl: ogImage,
      imageAlt: `T4BS score card`,
      canonicalUrl: `${origin}/s/${id}`,
      siteName: 'T4BS',
      themeColor: '#0C0B09',
    }),
    redirectTo: '/',
  })(ctx);
};

Apply migrations/0001_share_cards.sql to set up the share_cards table.

Hooking up @basenative/og-image

In functions/og/score/[id].png.js:

import { defineShareCards } from '@basenative/share/server';
import { renderPng, defaultTheme } from '@basenative/og-image';
import { scoreCard } from '@basenative/og-image/presets';

export const onRequestGet = async ({ env, params }) => {
  const store = defineShareCards({ db: env.DB });
  const card = await store.get(params.id);
  if (!card) return new Response('not found', { status: 404 });

  const png = await renderPng(scoreCard({
    category: card.category,
    score: card.score,
    won: !!card.won,
    grid: card.grid,
    theme: defaultTheme,
  }));
  return new Response(png, {
    headers: {
      'Content-Type': 'image/png',
      'Cache-Control': 'public, max-age=31536000, immutable',
    },
  });
};

The landingHandler references this URL, the crawler fetches it, and the preview renders.

Custom domains, custom roots

  • origin accepts a string or (env) => string.
  • path defaults to /s/.
  • idPattern defaults to /^[a-z0-9]{4,16}$/i.
  • validate lets you reject malformed mint requests inline.

License

Apache-2.0.

版本与发布记录

@basenative/share 共发布 11 个版本,首次发布于 2026/10/18;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/10/312 天前
2.4.02026/10/268 天前
2.3.22026/10/2212 天前
2.3.12026/10/2212 天前
2.3.02026/10/2212 天前
2.2.02026/10/2113 天前

关于 @basenative/share 的常见问题

@basenative/share 是什么?

本机Web共享+剪贴板回退,共享卡薄荷客户端+服务器,和og重定向登陆页面生成器的BaseNative应用程序

如何安装 @basenative/share?

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

@basenative/share 支持 TypeScript 吗?

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

@basenative/share 支持 ESM 还是 CommonJS?

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

@basenative/share 可以商用吗?

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

@basenative/share 还在维护吗?

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