Skip to main content
Prerequisites:
  • Next.js 14.0 or higher (for Server Actions)
  • TypeScript enabled in your Next.js project
  • Strapi v4 or v5 backend
Strapi2Front generates TypeScript types, services, and Zod schemas for your Next.js applications. Server Actions generation for Next.js is coming soon.

Current Support

TypeScript Types

Fully typed interfaces for all content types

Service Functions

Ready-to-use data fetching functions

Zod Schemas

Validation schemas for forms and mutations

Server Actions

Coming soon in a future release

Setup

1

Install Strapi2Front

Install the package in your Next.js project:
2

Initialize Configuration

Run the init command to create a configuration file:
3

Configure for Next.js

Update your strapi2front.config.ts:
strapi2front.config.ts
4

Add Environment Variables

Create a .env.local file:
.env.local
5

Generate Code

Run the sync command:
This generates:

Usage

Server Components

Fetch data in React Server Components using the generated services:

Client Components with SWR

Use SWR for client-side data fetching:

Route Handlers

Create API routes using the generated services:
app/api/contact/route.ts

Form Handling with React Hook Form

Use Zod schemas with React Hook Form:
components/ContactForm.tsx

Service Functions Reference

All generated services include these methods:

Collection Types

Fetch multiple items with pagination:

Single Types

  • find(options) — Fetch single type data
  • update(data) — Update single type
  • delete() — Delete single type

Advanced Patterns

Data Fetching Strategies

Generate static pages at build time:

Caching with Next.js

Leverage Next.js caching for optimal performance:

Parallel Data Fetching

Fetch multiple resources in parallel:

Server Actions (Coming Soon)

Server Actions generation for Next.js is in development and will be available in a future release.
When available, you’ll be able to use Server Actions like this:

Best Practices

Use Server Components by default and only mark components as 'use client' when you need interactivity.
Never expose your STRAPI_API_TOKEN to the client. Always use it in Server Components or API routes.
Use the findAll() method sparingly in production, as it fetches all items. Prefer findMany() with pagination for better performance.

Troubleshooting

Fetch Errors

If you encounter fetch errors, check that your NEXT_PUBLIC_STRAPI_URL is correct and accessible.

Type Errors

Run npx strapi2front sync to regenerate types after schema changes.

Environment Variables

Remember that only variables prefixed with NEXT_PUBLIC_ are available in client components.

Next Steps

Services

Learn more about service functions

Schemas

Use Zod schemas for validation

Relations

Work with Strapi relations

Types

Understand generated TypeScript types