I'll create a simple CRUD (Create, Read, Update, Delete) application backed by Supabase using Next.js and React. We'll create a basic task management app where users can add, view, edit, and delete tasks.
Let's start by creating the necessary files for our CRUD app:
This CRUD app allows users to create, read, update, and delete tasks using Supabase as the backend. Here's a breakdown of the components and their functions:
lib/supabase.ts
: Sets up the Supabase client using environment variables.types/task.ts
: Defines the Task interface.app/page.tsx
: The main page component that renders the TaskList.app/components/TaskList.tsx
: The core component that handles displaying tasks and user interactions.app/actions/taskActions.ts
: Server Actions for handling database operations.To use this app, you'll need to:
Add your Supabase URL and anon key to your environment variables:
This app provides a simple interface for managing tasks, with the ability to add new tasks, mark them as completed, and delete them. The UI is styled using Tailwind CSS and shadcn/ui components for a clean, modern look.
You can further enhance this app by adding features like:
Let me know if you want to expand on any part of this CRUD app or if you have any questions!
I'll modify our CRUD app to move the database access to server components and actions, and use non-public environment variables for better security. This approach aligns with Next.js best practices for handling sensitive operations server-side.
Let's update our CRUD app:
These changes move our database access to server components and actions, improving security and performance. Here's a summary of the modifications:
lib/supabase.ts
to use non-public environment variables (SUPABASE_URL
and SUPABASE_SERVICE_ROLE_KEY
).TaskList
to a server component that fetches tasks server-side.TaskItem
as a client component to handle individual task interactions.CreateTaskForm
as a client component for adding new tasks.revalidatePath
for cache invalidation.TaskList
.To use this updated app, you'll need to:
Add your Supabase URL and service role key to your environment variables (not prefixed with NEXT_PUBLIC):
This updated version provides better security by keeping sensitive operations server-side and using non-public environment variables. It also leverages Next.js 13+ features like Server Components and Server Actions for improved performance and developer experience.
Remember to handle any errors appropriately in a production environment and consider adding user authentication for a more secure application.