# Quick start: Next.js

> Add Editlet to a Next.js App Router site and edit your first entry with ?editlet.

Follow these steps to add Editlet to a Next.js App Router site (`next >=16`, `react >=19`) and edit an entry in
`next dev`.

## Install with your agent

Give Claude Code, Codex or another coding agent this prompt in your project:

```text
Install Editlet in this project: add the package `editlet` with the project's package manager, then read
node_modules/editlet/skills/editlet-setup/SKILL.md and follow it.
```

## Install by hand

1. Install the package and link the [agent skills](https://editlet.gunkin.dev/docs/agent-skills.md):

   ```bash
   npm install editlet
   node node_modules/editlet/skills/editlet/scripts/link.mjs --hooks
   ```

2. Wrap your Next config. In `next dev` and `next build`, `withEditlet` writes the types to `.editlet/content.d.ts`
   and the editor to `public/editlet/`. In `next dev` it also serves the dev file API at `/__editlet`.

   ```ts
   // next.config.ts
   import { withEditlet } from 'editlet/next/config';

   export default withEditlet({ reactStrictMode: true });
   ```

3. Add the generated folders to `.gitignore`:

   ```text
   /.editlet/
   /public/editlet/
   ```

4. Render `<EditletLoader />` last in `<body>` of `app/layout.tsx` (or `src/app/layout.tsx`). It renders nothing
   until the URL has `?editlet`.

   ```tsx
   import { EditletLoader } from 'editlet/next';

   // inside <body>:
   {children}
   <EditletLoader />
   ```

5. Create `cms/schema.json`. Set `git.baseBranch` to the branch your site deploys from. See
   [Content model](https://editlet.gunkin.dev/docs/content-model.md).

   ```json
   {
     "contentDir": "cms/content",
     "git": { "baseBranch": "main" },
     "collections": {
       "post": {
         "label": "Post",
         "titleField": "title",
         "slugFrom": "title",
         "fields": {
           "title": { "type": "string", "label": "Title", "required": true },
           "body": { "type": "markdown", "label": "Body" }
         }
       }
     }
   }
   ```

6. Create one entry, `cms/content/post/hello-world.md`:

   ```md
   ---
   title: Hello world
   ---

   The first post.
   ```

7. Run `npm run dev` and open `http://localhost:3000/?editlet`. In dev, a save changes the local file.

## Show content on a page

Create the reader once, then call it in Server Components, `generateStaticParams` and `generateMetadata`:

```ts
// src/lib/cms.ts
import { createReader } from 'editlet/next';
import type { EntryMap } from '../../.editlet/content';

export const { getEntries, getEntry, getRef, getReferrers } = createReader<EntryMap>();
```

```ts
const posts = await getEntries('post');                  // sorted by slug
const post = await getEntry('post', 'hello-world');      // undefined when missing
const home = await getEntry('home');                     // a singleton: the slug is 'index'
const author = await getRef(post.data.author);           // 'author/jane' → that entry
const full = await getEntry('post', 'hello-world', { depth: 1 }); // references → entries, 0–10 levels
const byJane = await getReferrers('author/jane', { from: 'post' }); // entries that reference author/jane
```

An entry is `{ collection, slug, data, body }`, and `body` holds the `markdown` field. Production builds leave out
`draft: true` entries. An invalid entry throws an error with every issue, so `next build` fails.

Image fields hold a public URL in `src`. Pass images in a Markdown body through `assetUrl`:

```tsx
import Markdown, { defaultUrlTransform } from 'react-markdown';
import { assetUrl } from 'editlet/next';

<Markdown urlTransform={(url, key) => defaultUrlTransform(key === 'src' ? assetUrl(post, url) : url)}>
  {post.body}
</Markdown>;
```

Mark the element that shows an entry with `data-editlet-entry="post/hello-world"`. The editor can then point at it,
and you can select words in it and press **Edit**.

Tell the schema which page shows an entry: `"url": "/blog/{slug}"` on the collection, matching your route
`src/app/blog/[slug]/page.tsx`. The editor then shows the page's address in *New entry* and *Details*, and **⋯ → Open
on the page** goes there. Add `"pluralLabel": "Posts"` for the list heading. See [Content model](https://editlet.gunkin.dev/docs/content-model.md#page-urls).

## Limits

- `basePath` is not supported. The editor is always at `/editlet/` and the dev API at `/__editlet`.
- With `cacheComponents: true`, wrap reads in `'use cache'` with `cacheLife('max')`. In dev, a content edit then
  needs a restart.

To edit the live site through GitHub, read [Dev mode and production](https://editlet.gunkin.dev/docs/dev-and-production.md).
