Convex + Next.js
Use this skill to
- Bootstrap Convex in a new or existing Next.js app.
- Add a feature end to end: schema, indexes, functions, UI hooks, auth, and deployment.
- Debug typical integration failures: missing provider, missing generated code, bad client/server boundaries, missing env vars.
- Review whether Convex is a good fit for a realtime or collaborative Next.js feature.
Do not use this skill when
- The task is plain Next.js UI work with no Convex dependency.
- The backend is definitely not Convex and the user is not considering migration.
- The problem is generic database theory with no Next.js/Convex implementation work.
Default posture
- Use
npx convex dev for development.
- Keep reactive hooks in Client Components.
- Prefer indexed queries over
.filter(...).
- Treat unbounded lists as paginated by default.
- Put external I/O in actions; add
"use node" only if Node APIs or unsupported packages are required.
- Require input validation on public functions; add return validation unless there is a good reason not to.
- Add explicit auth and ownership checks for user data.
- Prefer helper functions or custom wrappers when the same auth/tenant checks repeat.
Starting questions to answer from the repo
- Is this a new app, an existing Next.js app, or a migration?
- App Router, Pages Router, or both?
- Does the feature need reactivity, SSR, server actions, or all three?
- Is the dataset bounded or should it paginate?
- Is auth already present? If yes, is it client-only or needed on the server too?
- Would a Convex component make this feature more reusable or isolated?
Workflow 1 — Choose the right starting path
A. Brand new project
Prefer:
npm create convex@latest
If the user already has a Next.js app structure they want to keep, use path B instead.
B. Existing Next.js app
Install Convex and start dev sync:
npm install convex
npx convex dev
Expected outcomes:
convex/ exists, or the custom functions directory from convex.json
- generated files appear under
_generated/
- a dev deployment or local deployment is connected
NEXT_PUBLIC_CONVEX_URL is available for the frontend
See references/01-setup-and-decision-tree.md.
Workflow 2 — Model data for query patterns, not screen shapes
Before writing UI, define:
- the tables
- the ownership fields
- the indexes needed for the main reads
- whether lists are bounded or paginated
- whether files should live in Convex File Storage instead of large documents
Rules:
- Prefer flat relational-style documents over deep nested blobs.
- Use
v.id("table") for relationships.
- Add indexes for every repeated filter/sort path you know you need.
- If the query would scan an unbounded table, redesign the index or paginate it.
See references/02-schema-and-indexes.md.
Workflow 3 — Pick the correct Convex function shape
Query
Use for pure reads. Keep them small, indexed, and predictable.
Mutation
Use for writes and transactional read-write logic.
Action
Use for external APIs, long-running work, or non-transactional orchestration.
- Stay in the default Convex runtime if
fetch is enough.
- Add
"use node" only when you need Node-only APIs or unsupported packages.
- Files with
"use node" should contain actions only.
Detailed patterns: references/03-functions-and-safety.md
Workflow 4 — Enforce validation, auth, and ownership early
For public functions:
- define
args
- usually define
returns
- call
ctx.auth.getUserIdentity() when the function is protected
- check ownership or team membership, not just authentication
- move repeated checks into helpers or custom wrappers once duplication starts to spread
If auth or tenant checks repeat in many functions, consider:
convex/lib/auth.ts helpers
- thin wrappers/custom functions for
query, mutation, or action
- shared policy helpers for tenant/resource checks
See references/05-auth-and-access-control.md.
Workflow 5 — Respect Next.js boundaries
useQuery, useMutation, useAction, usePaginatedQuery, and usePreloadedQuery belong in Client Components.
- For reactive-first pages with good first paint, use
preloadQuery in a Server Component and usePreloadedQuery in a Client Component.
- For server-only reads, use
fetchQuery.
- For Server Actions or Route Handlers, use
fetchMutation or fetchAction.
Do not call React hooks from Server Components.
See references/04-nextjs-client-and-server-boundaries.md.
Workflow 6 — Treat large lists as a pagination problem
Use pagination by default when:
- the user says “all”, “feed”, “activity”, “history”, “messages”, “notifications”, “search results”, or “infinite scroll”
- the table can grow without a natural hard limit
- you would otherwise reach for
.collect() on a user-facing list
Pattern:
- backend query uses
.paginate(paginationOpts)
- React client uses
usePaginatedQuery
See references/06-pagination-performance-and-realtime.md.
Workflow 7 — Consider components when the feature wants isolation
A Convex component is often worth it when the feature:
- has its own schema, functions, and internal jobs
- should be reusable across apps
- would otherwise pollute the root
convex/ folder with tightly-coupled code
Use normal app code when the feature is small and specific to one app.
See references/07-components-migrations-and-reuse.md.
Workflow 8 — Choose the right development mode
- On your own machine or in a local coding agent, standard
npx convex dev is usually right.
- In remote or background agents that cannot log in, use Agent Mode.
- For isolated local-only development, use local deployments.
See references/08-local-dev-agent-mode-and-cloud-agents.md.
Workflow 9 — Validate before you stop
Run:
python {baseDir}/scripts/validate_project.py --root .
Useful flags:
python {baseDir}/scripts/validate_project.py --root . --strict
python {baseDir}/scripts/validate_project.py --root . --json
The validator checks for the common failures this skill is designed to catch:
- missing Convex installation or generated code
- missing provider or env wiring
- hook usage in non-client components
- implicit table access
.collect() or .filter() smells in queries
- missing validators on Convex functions
- risky
"use node" file mixes
- scheduler calls aimed at public functions
- missing TypeScript strictness or missing Convex ESLint plugin
Workflow 10 — Deploy cleanly
During normal development, keep using:
npx convex dev
For production or CI:
npx convex deploy
For Vercel builds, the common pattern is:
npx convex deploy --cmd "npm run build"
See references/09-deploy-ci-and-vercel.md.
What a strong final implementation usually includes
- updated
convex/schema.ts
- new or updated indexes
- public functions with
args and usually returns
- auth or ownership checks where needed
- UI wired through the generated
api
"use client" only where it is actually needed
- paginated lists instead of unbounded collects
- a note about required env vars
- commands the user should run to verify the change
Response shape to prefer when making code changes
- State the files to add or edit.
- Explain the architectural choice in one sentence.
- Apply the code changes.
- Run the validator or describe the exact checks to run.
- Call out any follow-up env vars, auth setup, deploy steps, or migration concerns.
Reference map
- Setup and choosing a path: references/01-setup-and-decision-tree.md
- Schema and index design: references/02-schema-and-indexes.md
- Functions, validation, Node actions, scheduler safety: references/03-functions-and-safety.md
- Next.js client/server boundaries and SSR: references/04-nextjs-client-and-server-boundaries.md
- Auth and access control: references/05-auth-and-access-control.md
- Pagination, performance, and realtime: references/06-pagination-performance-and-realtime.md
- Components, migrations, and reuse: references/07-components-migrations-and-reuse.md
- Local dev, Agent Mode, and local deployments: references/08-local-dev-agent-mode-and-cloud-agents.md
- Deploy and CI: references/09-deploy-ci-and-vercel.md
- Troubleshooting and smoke tests: references/10-troubleshooting-and-smoke-tests.md
1---2name: convex-nextjs3description: Build, refactor, debug, or review a Convex backend inside a Next.js app. Use when the user mentions Convex, `convex/nextjs`, `npx convex dev`, `NEXT_PUBLIC_CONVEX_URL`, `useQuery`, `useMutation`, `usePaginatedQuery`, schema/indexes, auth, App Router server components/actions, realtime data, chat, notifications, collaborative features, or deploying Convex with Vercel. Also use when deciding whether Convex is a good fit for a Next.js app that needs reactive shared state. Do not use for generic frontend-only Next.js work or non-Convex backends unless the task is specifically about adopting, migrating to, or evaluating Convex.4---5
6# Convex + Next.js
7
8## Use this skill to
9- Bootstrap Convex in a new or existing Next.js app.
10- Add a feature end to end: schema, indexes, functions, UI hooks, auth, and deployment.
11- Debug typical integration failures: missing provider, missing generated code, bad client/server boundaries, missing env vars.
12- Review whether Convex is a good fit for a realtime or collaborative Next.js feature.
13
14## Do not use this skill when
15- The task is plain Next.js UI work with no Convex dependency.
16- The backend is definitely not Convex and the user is not considering migration.
17- The problem is generic database theory with no Next.js/Convex implementation work.
18
19## Default posture
20- Use `npx convex dev` for development.
21- Keep reactive hooks in Client Components.
22- Prefer indexed queries over `.filter(...)`.
23- Treat unbounded lists as paginated by default.
24- Put external I/O in actions; add `"use node"` only if Node APIs or unsupported packages are required.
25- Require input validation on public functions; add return validation unless there is a good reason not to.
26- Add explicit auth and ownership checks for user data.
27- Prefer helper functions or custom wrappers when the same auth/tenant checks repeat.
28
29## Starting questions to answer from the repo
301. Is this a new app, an existing Next.js app, or a migration?
312. App Router, Pages Router, or both?
323. Does the feature need reactivity, SSR, server actions, or all three?
334. Is the dataset bounded or should it paginate?
345. Is auth already present? If yes, is it client-only or needed on the server too?
356. Would a Convex component make this feature more reusable or isolated?
36
37## Workflow 1 — Choose the right starting path
38
39### A. Brand new project
40Prefer:
41```bash
42npm create convex@latest
43```
44
45If the user already has a Next.js app structure they want to keep, use path B instead.
46
47### B. Existing Next.js app
48Install Convex and start dev sync:
49```bash
50npm install convex
51npx convex dev
52```
53
54Expected outcomes:
55- `convex/` exists, or the custom functions directory from `convex.json`
56- generated files appear under `_generated/`
57- a dev deployment or local deployment is connected
58- `NEXT_PUBLIC_CONVEX_URL` is available for the frontend
59
60See [references/01-setup-and-decision-tree.md](references/01-setup-and-decision-tree.md).
61
62## Workflow 2 — Model data for query patterns, not screen shapes
63Before writing UI, define:
64- the tables
65- the ownership fields
66- the indexes needed for the main reads
67- whether lists are bounded or paginated
68- whether files should live in Convex File Storage instead of large documents
69
70Rules:
71- Prefer flat relational-style documents over deep nested blobs.
72- Use `v.id("table")` for relationships.
73- Add indexes for every repeated filter/sort path you know you need.
74- If the query would scan an unbounded table, redesign the index or paginate it.
75
76See [references/02-schema-and-indexes.md](references/02-schema-and-indexes.md).
77
78## Workflow 3 — Pick the correct Convex function shape
79
80### Query
81Use for pure reads. Keep them small, indexed, and predictable.
82
83### Mutation
84Use for writes and transactional read-write logic.
85
86### Action
87Use for external APIs, long-running work, or non-transactional orchestration.
88- Stay in the default Convex runtime if `fetch` is enough.
89- Add `"use node"` only when you need Node-only APIs or unsupported packages.
90- Files with `"use node"` should contain actions only.
91
92Detailed patterns: [references/03-functions-and-safety.md](references/03-functions-and-safety.md)
93
94## Workflow 4 — Enforce validation, auth, and ownership early
95For public functions:
96- define `args`
97- usually define `returns`
98- call `ctx.auth.getUserIdentity()` when the function is protected
99- check ownership or team membership, not just authentication
100- move repeated checks into helpers or custom wrappers once duplication starts to spread
101
102If auth or tenant checks repeat in many functions, consider:
103- `convex/lib/auth.ts` helpers
104- thin wrappers/custom functions for `query`, `mutation`, or `action`
105- shared policy helpers for tenant/resource checks
106
107See [references/05-auth-and-access-control.md](references/05-auth-and-access-control.md).
108
109## Workflow 5 — Respect Next.js boundaries
110- `useQuery`, `useMutation`, `useAction`, `usePaginatedQuery`, and `usePreloadedQuery` belong in Client Components.
111- For reactive-first pages with good first paint, use `preloadQuery` in a Server Component and `usePreloadedQuery` in a Client Component.
112- For server-only reads, use `fetchQuery`.
113- For Server Actions or Route Handlers, use `fetchMutation` or `fetchAction`.
114
115Do not call React hooks from Server Components.
116
117See [references/04-nextjs-client-and-server-boundaries.md](references/04-nextjs-client-and-server-boundaries.md).
118
119## Workflow 6 — Treat large lists as a pagination problem
120Use pagination by default when:
121- the user says “all”, “feed”, “activity”, “history”, “messages”, “notifications”, “search results”, or “infinite scroll”
122- the table can grow without a natural hard limit
123- you would otherwise reach for `.collect()` on a user-facing list
124
125Pattern:
126- backend query uses `.paginate(paginationOpts)`
127- React client uses `usePaginatedQuery`
128
129See [references/06-pagination-performance-and-realtime.md](references/06-pagination-performance-and-realtime.md).
130
131## Workflow 7 — Consider components when the feature wants isolation
132A Convex component is often worth it when the feature:
133- has its own schema, functions, and internal jobs
134- should be reusable across apps
135- would otherwise pollute the root `convex/` folder with tightly-coupled code
136
137Use normal app code when the feature is small and specific to one app.
138
139See [references/07-components-migrations-and-reuse.md](references/07-components-migrations-and-reuse.md).
140
141## Workflow 8 — Choose the right development mode
142- On your own machine or in a local coding agent, standard `npx convex dev` is usually right.
143- In remote or background agents that cannot log in, use Agent Mode.
144- For isolated local-only development, use local deployments.
145
146See [references/08-local-dev-agent-mode-and-cloud-agents.md](references/08-local-dev-agent-mode-and-cloud-agents.md).
147
148## Workflow 9 — Validate before you stop
149Run:
150```bash
151python {baseDir}/scripts/validate_project.py --root .
152```
153
154Useful flags:
155```bash
156python {baseDir}/scripts/validate_project.py --root . --strict
157python {baseDir}/scripts/validate_project.py --root . --json
158```
159
160The validator checks for the common failures this skill is designed to catch:
161- missing Convex installation or generated code
162- missing provider or env wiring
163- hook usage in non-client components
164- implicit table access
165- `.collect()` or `.filter()` smells in queries
166- missing validators on Convex functions
167- risky `"use node"` file mixes
168- scheduler calls aimed at public functions
169- missing TypeScript strictness or missing Convex ESLint plugin
170
171## Workflow 10 — Deploy cleanly
172During normal development, keep using:
173```bash
174npx convex dev
175```
176
177For production or CI:
178```bash
179npx convex deploy
180```
181
182For Vercel builds, the common pattern is:
183```bash
184npx convex deploy --cmd "npm run build"
185```
186
187See [references/09-deploy-ci-and-vercel.md](references/09-deploy-ci-and-vercel.md).
188
189## What a strong final implementation usually includes
190- updated `convex/schema.ts`
191- new or updated indexes
192- public functions with `args` and usually `returns`
193- auth or ownership checks where needed
194- UI wired through the generated `api`
195- `"use client"` only where it is actually needed
196- paginated lists instead of unbounded collects
197- a note about required env vars
198- commands the user should run to verify the change
199
200## Response shape to prefer when making code changes
2011. State the files to add or edit.
2022. Explain the architectural choice in one sentence.
2033. Apply the code changes.
2044. Run the validator or describe the exact checks to run.
2055. Call out any follow-up env vars, auth setup, deploy steps, or migration concerns.
206
207## Reference map
208- Setup and choosing a path: [references/01-setup-and-decision-tree.md](references/01-setup-and-decision-tree.md)
209- Schema and index design: [references/02-schema-and-indexes.md](references/02-schema-and-indexes.md)
210- Functions, validation, Node actions, scheduler safety: [references/03-functions-and-safety.md](references/03-functions-and-safety.md)
211- Next.js client/server boundaries and SSR: [references/04-nextjs-client-and-server-boundaries.md](references/04-nextjs-client-and-server-boundaries.md)
212- Auth and access control: [references/05-auth-and-access-control.md](references/05-auth-and-access-control.md)
213- Pagination, performance, and realtime: [references/06-pagination-performance-and-realtime.md](references/06-pagination-performance-and-realtime.md)
214- Components, migrations, and reuse: [references/07-components-migrations-and-reuse.md](references/07-components-migrations-and-reuse.md)
215- Local dev, Agent Mode, and local deployments: [references/08-local-dev-agent-mode-and-cloud-agents.md](references/08-local-dev-agent-mode-and-cloud-agents.md)
216- Deploy and CI: [references/09-deploy-ci-and-vercel.md](references/09-deploy-ci-and-vercel.md)
217- Troubleshooting and smoke tests: [references/10-troubleshooting-and-smoke-tests.md](references/10-troubleshooting-and-smoke-tests.md)