Project structure
The frontend follows Next.js 13 App Router conventions:Core components
Layout component
The root layout defines metadata and wraps the entire application:The layout uses React Server Components by default, providing automatic code splitting and reduced client-side JavaScript.
Header component
A server component that renders the application header:The header uses Next.js
Image component for automatic image optimization and Link for client-side navigation.PromptInput component
A client component that handles user input and AI prompt suggestions:Key features of PromptInput
Key features of PromptInput
- AI suggestions: Uses ChatGPT to generate creative prompt ideas
- Optimistic updates: Shows loading states while waiting for API responses
- Toast notifications: Provides real-time feedback on generation status
- Dual submit modes: Users can submit their own prompt or use AI suggestion
Images component
A client component that displays the generated image gallery:The first image in the grid spans 2 columns and 2 rows on medium+ screens, creating an engaging Pinterest-style layout.
State management with SWR
VisionaryAI uses SWR for efficient data fetching and state management:Fetcher functions
SWR provides automatic revalidation, caching, and optimistic updates without complex state management libraries.
SWR configuration
All SWR hooks userevalidateOnFocus: false to prevent unnecessary refetching when users switch browser tabs:
Styling with Tailwind CSS
The application uses Tailwind CSS for styling with responsive design patterns:- Responsive grid
- Hover effects
- Conditional styling
- Mobile: 1 column
- Tablet: 2 columns
- Desktop: 3 columns
- Large desktop: 4 columns
Client-side notifications
TheClientProvider component wraps the app with react-hot-toast:
Toast notifications provide non-intrusive feedback for image generation status, errors, and success messages.
API routes (middleware)
Next.js API routes act as a secure proxy layer:Generate image route
Get images route
Suggestion route
API routes use
cache: "no-store" to ensure fresh data on every request, critical for real-time image generation.Performance optimizations
React Server Components
React Server Components
The
Header component is a Server Component, reducing client-side JavaScript and improving initial page load.Code splitting
Code splitting
Next.js 13 App Router automatically code-splits routes and components, loading only necessary JavaScript.
SWR caching
SWR caching
SWR caches API responses, reducing redundant network requests and improving perceived performance.
Optimistic updates
Optimistic updates
The Images component uses optimistic updates when refreshing, maintaining UI responsiveness.
Next steps
Backend development
Learn about Azure Functions and OpenAI integration
Local setup
Set up the development environment on your machine