Next

作者 vercel-labsfc2a696a50a3无许可证18K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库昨天更新

Next.js renderer for json-render that turns JSON specs into full Next.js applications with routes, layouts, SSR, and metadata. Use when working with @json-render/next, building Next.js apps from JSON specs, or creating AI-generated multi-page applications.

AI 生成的概览

将 JSON 规格渲染为包含路由、布局、SSR 和元数据的完整 Next.js 应用。

功能
该技能介绍 @json-render/next,这是一个把 NextAppSpec JSON 对象转换为完整 Next.js 应用的渲染器。内容涵盖如何定义包含元数据、布局、路由和状态的规格,再通过 createNextApp 创建应用并接入路由文件。它还说明了内置组件、动作、服务端数据加载器、SSR 行为以及服务端与客户端入口。
适用场景
适用于使用 @json-render/next、根据 JSON 规格构建 Next.js 应用,或生成 AI 驱动的多页面应用的场景。适合需要由规格驱动路由、布局、元数据和服务端渲染的任务。
运行要求
需要一个 Next.js 项目,并安装 npm 包 @json-render/core、@json-render/react 和 @json-render/next。该技能不包含脚本,仅为说明文档。服务端数据加载器可能需要访问数据库等数据源。

@json-render/next

Next.js renderer that converts JSON specs into full Next.js applications with routes, pages, layouts, metadata, and SSR support.

Quick Start

bash
npm install @json-render/core @json-render/react @json-render/next

1. Define Your Spec

typescript
// lib/spec.tsimport type { NextAppSpec } from "@json-render/next";
export const spec: NextAppSpec = {  metadata: {    title: { default: "My App", template: "%s | My App" },    description: "A json-render Next.js application",  },  layouts: {    main: {      root: "shell",      elements: {        shell: { type: "Container", props: {}, children: ["nav", "slot"] },        nav: { type: "NavBar", props: { links: [          { href: "/", label: "Home" },          { href: "/about", label: "About" },        ]}, children: [] },        slot: { type: "Slot", props: {}, children: [] },      },    },  },  routes: {    "/": {      layout: "main",      metadata: { title: "Home" },      page: {        root: "hero",        elements: {          hero: { type: "Card", props: { title: "Welcome" }, children: [] },        },      },    },    "/about": {      layout: "main",      metadata: { title: "About" },      page: {        root: "content",        elements: {          content: { type: "Card", props: { title: "About Us" }, children: [] },        },      },    },  },};

2. Create the App

typescript
// lib/app.tsimport { createNextApp } from "@json-render/next/server";import { spec } from "./spec";
export const { Page, generateMetadata, generateStaticParams } = createNextApp({  spec,  loaders: {    // Server-side data loaders (optional)    loadPost: async ({ slug }) => {      const post = await getPost(slug as string);      return { post };    },  },});

3. Wire Up Route Files

tsx
// app/[[...slug]]/page.tsxexport { Page as default, generateMetadata, generateStaticParams } from "@/lib/app";
tsx
// app/[[...slug]]/layout.tsximport { NextAppProvider } from "@json-render/next";import { registry, handlers } from "@/lib/registry";
export default function Layout({ children }: { children: React.ReactNode }) {  return (    <html lang="en">      <body>        <NextAppProvider registry={registry} handlers={handlers}>          {children}        </NextAppProvider>      </body>    </html>  );}

Key Concepts

NextAppSpec

The top-level spec defines an entire Next.js application:

  • metadata: Root-level SEO metadata (title template, description, OpenGraph)
  • layouts: Reusable layout element trees (each must include a Slot component)
  • routes: Route definitions keyed by URL pattern
  • state: Global initial state shared across all routes

Route Patterns

Routes use Next.js URL conventions:

  • "/" -- home page
  • "/about" -- static route
  • "/blog/[slug]" -- dynamic segment
  • "/docs/[...path]" -- catch-all segment
  • "/settings/[[...path]]" -- optional catch-all segment

Layouts

Layouts wrap page content. Every layout MUST include a Slot component where page content will be rendered. Layouts are defined once in spec.layouts and referenced by routes via the layout field.

Built-in Components

  • Slot: Placeholder in layouts where page content is rendered
  • Link: Client-side navigation link (wraps next/link)

Built-in Actions

  • setState: Update state value. Params: { statePath, value }
  • pushState: Append to array. Params: { statePath, value, clearStatePath? }
  • removeState: Remove from array by index. Params: { statePath, index }
  • navigate: Client-side navigation. Params: { href }

Data Loaders

Server-side async functions that run in the Server Component before rendering. Results are merged into the page's initial state.

typescript
createNextApp({  spec,  loaders: {    loadPost: async ({ slug }) => {      const post = await db.post.findUnique({ where: { slug } });      return { post };    },  },});

SSR

Pages are server-rendered automatically. The createNextApp Page component is an async Server Component that:

  1. Matches the route from the spec
  2. Runs server-side data loaders
  3. Generates metadata
  4. Passes the resolved spec to the client renderer for hydration

Entry Points

  • @json-render/next -- Client components (NextAppProvider, PageRenderer, Link)
  • @json-render/next/server -- Server utilities (createNextApp, matchRoute, schema)

API Reference

Server Exports (@json-render/next/server)

  • createNextApp(options) -- Create Page, generateMetadata, generateStaticParams
  • schema -- Custom schema for Next.js apps (for AI catalog generation)
  • matchRoute(spec, pathname) -- Match a URL to a route spec
  • resolveMetadata(spec, route) -- Resolve metadata for a route
  • slugToPath(slug) -- Convert catch-all slug array to pathname
  • collectStaticParams(spec) -- Collect static params for all routes

Client Exports (@json-render/next)

  • NextAppProvider -- Context provider for registry and handlers
  • PageRenderer -- Renders a page spec with optional layout
  • NextErrorBoundary -- Error boundary component
  • NextLoading -- Loading state component
  • NextNotFound -- Not-found component
  • Link -- Built-in navigation component (wraps next/link)

来源与署名

来源:vercel-labs/json-render位于skills/next提交fc2a696

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架