This Twitter Clone is a full-stack application built with Next.js, React, and Clerk for authentication. It provides a social media platform where users can create posts, like posts, and interact with other users, mimicking core functionalities of Twitter. 4. Application Architecture 5. Authentication Flow 6. Main Application Flow 7. API Endpoints 8. Database Schema 9. State Management 10. UI Components 11. Styling 12. Error Handling 13. Performance Optimizations 14. Getting Started 15. Deployment 16. Future Enhancements 17. Contributing 18. License
This Twitter Clone is a full-stack application built with Next.js, React, and Clerk for authentication. It provides a social media platform where users can create posts, like posts, and interact with other users, mimicking core functionalities of Twitter.
- User authentication (sign up, login, logout)
- Create, read, update, and delete posts
- Like/unlike posts
- Reply to posts
- User profiles
- Feed of recent posts
- Pagination for posts
- Responsive design
- Frontend: React, Next.js
- Backend: Next.js API routes
- Database: Vercel Postgres
- Authentication: Clerk
- Styling: Tailwind CSS
- Deployment: Vercel
The application follows a typical Next.js structure:
app/
: Contains the main application codecomponents/
: Reusable React componentsapi/
: API routes(routes)/
: Page components for different routes
public/
: Static assetsstyles/
: Global styles
The app uses the App Router feature of Next.js for routing.
- User signs up or logs in using Clerk's UI components.
- Clerk handles the authentication process and provides a session token.
- The app uses Clerk's
useUser
hook to access user information. - When a user is created or updated, a webhook is triggered
- The webhook updates the user information in the Vercel Postgres database.
- User lands on the home page (
app/page.tsx
). - The page fetches recent posts from the
/api/posts
endpoint. - Posts are displayed in a list.
- Authenticated users can create new posts using a form.
- User enters post content in the form.
handlePostSubmit
function is called on form submission.- A POST request is sent to
/api/posts
. - The new post is added to the database and returned in the response.
- The UI is updated to show the new post.
- Users can like posts by clicking the like button.
- A POST request is sent to
/api/posts/[id]/like
. - The like status is toggled in the database.
- The UI is updated to reflect the new like status and count.
- Users can reply to posts by using the reply form below each post.
- When a reply is submitted, a new post is created with a
reply_to_id
referencing the original post. - Replies are displayed beneath the original post, creating a threaded conversation.
- Users can view profiles by navigating to
/users/[id]
. - The profile page fetches user information and posts from the respective API endpoints.
- The page displays the user's posts and liked posts.
-
POST /api/posts
- Creates a new post or reply
- Request body:
{ content: string, reply_to_id?: string }
- Response: The created post object
-
GET /api/posts
- Fetches posts, with optional filtering
- Query parameters:
userId
: Fetch posts by a specific userlikedBy
: Fetch posts liked by a specific userpage
: Pagination
- Response: An array of post objects, current page, and total pages
-
PUT /api/posts/[id]
- Updates an existing post
- Request body:
{ content: string }
- Response: The updated post object
-
DELETE /api/posts/[id]
- Deletes a post
- Response: Success message
-
POST /api/posts/[id]/like
- Likes or unlikes a post
- Response: Updated like status and count
-
GET /api/users/[id]
- Fetches user information
- Response: User object with username and display name
-
POST /api/clerk_webhook
- Handles Clerk webhook events, specifically user creation
- Creates or updates user in the database
The application uses Vercel Postgres with the following main tables:
-
users
- id (primary key)
- username
- first_name
- last_name
- image_url
- created_at
- updated_at
- subscription_tier
-
posts
- id (primary key)
- user_id (foreign key to users)
- content
- created_at
- updated_at
- reply_to_id (self-referencing foreign key, nullable)
-
likes
- user_id (foreign key to users)
- post_id (foreign key to posts)
- created_at
The application primarily uses React's built-in state management with useState
and useEffect
hooks. For more complex state management, consider implementing a solution like Redux or Zustand in future iterations.
The app uses a combination of custom components and Clerk's authentication components. Key components include:
Sidebar
: Navigation sidebarPostForm
: Form for creating new postsPostList
: Displays a list of postsPost
: Individual post componentReplyForm
: Form for submitting replies to postsReplyList
: Displays replies to a postUserButton
(from Clerk): User authentication button
The application uses Tailwind CSS for styling, providing a responsive and customizable design. Global styles are defined in app/globals.css
.
Error handling is implemented throughout the application:
- API routes use try-catch blocks to handle errors and return appropriate status codes.
- Client-side error handling is done using try-catch blocks in async functions.
- Error messages are displayed to users when operations fail.
- Server-side rendering for initial page loads
- Client-side navigation for faster subsequent page transitions
- Pagination for posts to limit data fetching
- Image optimization using Next.js Image component
- Clone the repository
- Install dependencies:
npm install
- Set up environment variables (see
.env.example
) - Run the development server:
npm run dev
- Open http://localhost:3000 in your browser
The app is designed to be deployed on Vercel:
- Push your code to a GitHub repository
- Connect your repository to Vercel
- Configure environment variables in Vercel project settings
- Deploy the application
- Implement real-time updates using WebSockets
- Add direct messaging functionality
- Implement hashtags and search functionality
- Add media upload capabilities for posts
- Enhance reply functionality with nested replies and threading
- Implement notifications for replies and mentions
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.