Prerequisites:
- Next.js 14.0 or higher (for Server Actions)
- TypeScript enabled in your Next.js project
- Strapi v4 or v5 backend
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
- findMany
- findAll
- findOne
- findBySlug
- create
- update
- delete
- count
Fetch multiple items with pagination:
Single Types
find(options)— Fetch single type dataupdate(data)— Update single typedelete()— Delete single type
Advanced Patterns
Data Fetching Strategies
- SSG (Static)
- SSR (Dynamic)
- ISR (Revalidate)
- Client-Side
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.
Best Practices
Troubleshooting
Fetch Errors
If you encounter fetch errors, check that yourNEXT_PUBLIC_STRAPI_URL is correct and accessible.
Type Errors
Runnpx strapi2front sync to regenerate types after schema changes.
Environment Variables
Remember that only variables prefixed withNEXT_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