A modern, full-featured blog application built with Next.js 12, MongoDB, GraphQL, and TailwindCSS. Features include server-side rendering, static site generation, optimized images, and a custom GraphQL API.
- β Server-Side Rendering (SSR) - Fast page loads with pre-rendered content
- β Static Site Generation (SSG) - Build-time optimization with ISR
- β GraphQL API - Custom Apollo Server integration
- β MongoDB Database - Scalable NoSQL database with connection pooling
- β Image Optimization - Next.js Image component with WebP/AVIF support
- β Responsive Design - Mobile-first approach with TailwindCSS
- β Category Filtering - Browse posts by category
- β Related Posts - Smart content recommendations
- β SEO Optimized - Meta tags and semantic HTML
- β Fast Build Times - SWC compiler for 7x faster builds
- β Comments System - User engagement features
- β React Query - Smart data fetching and caching
- Prerequisites
- Installation
- Environment Variables
- Database Setup
- Running the Application
- Project Structure
- API Routes
- GraphQL Schema
- Available Scripts
- Performance Optimizations
- Deployment
- Technologies Used
- Contributing
- License
Before you begin, ensure you have the following installed:
- Node.js (v14.x or higher)
- npm or yarn
- MongoDB Atlas Account (or local MongoDB installation)
- Git
- Clone the repository
git clone https://github.com/yourusername/abek-blog.git
cd abek-blog- Install dependencies
npm install
# or
yarn install- Create environment file
cp .env.example .env.localCreate a .env.local file in the root directory:
# MongoDB Connection
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/blog?retryWrites=true&w=majority
# GraphQL Endpoint
NEXT_PUBLIC_GRAPHCMS_ENDPOINT=http://localhost:3000/api/graphql
# Disable Telemetry (Optional)
NEXT_TELEMETRY_DISABLED=1- Go to MongoDB Atlas
- Create a new cluster (free tier available)
- Create a database user
- Whitelist your IP address
- Click "Connect" β "Connect your application"
- Copy the connection string and replace
<password>with your password
Populate your database with initial data:
npm run seedThis will create:
- 6 sample blog posts
- 5 categories (Technology, Design, Programming, Web Development, AI & Machine Learning)
- 3 authors
Optimize query performance:
npm run create-indexesThis creates indexes on:
- Post slugs (unique)
- Created dates
- Category slugs
- Author names
npm run devOpen http://localhost:3000 in your browser.
npm run build
npm run startnpm run analyzeabek-blog/
βββ components/ # React components
β βββ Author.jsx
β βββ Categories.jsx
β βββ Comments.jsx
β βββ CommentsForm.jsx
β βββ Header.jsx
β βββ Layout.jsx
β βββ Loader.jsx
β βββ PostCard.jsx
β βββ PostDetail.jsx
β βββ PostWidget.jsx
β βββ index.js
βββ lib/ # Utility functions
β βββ mongodb.js # MongoDB connection
β βββ posts.js # Database queries
β βββ queryClient.js # React Query config
βββ pages/ # Next.js pages
β βββ api/ # API routes
β β βββ graphql.js # GraphQL endpoint
β β βββ popular-categories.js
β βββ category/ # Category pages
β β βββ [slug].js
β βββ post/ # Post detail pages
β β βββ [slug].js
β βββ _app.js # App wrapper
β βββ index.js # Homepage
βββ public/ # Static assets
βββ scripts/ # Utility scripts
β βββ create-indexes.js
β βββ seed.js
βββ services/ # API service layer
β βββ index.js
βββ styles/ # Global styles
β βββ globals.scss
βββ .env.local # Environment variables
βββ next.config.js # Next.js configuration
βββ package.json
βββ tailwind.config.js # Tailwind configuration
βββ README.md
Endpoint: /api/graphql
Access the GraphQL Playground at http://localhost:3000/api/graphql
POST /api/seed- Seed database with sample dataGET /api/popular-categories?limit=3- Get most popular categories
type Query {
# Get all posts with pagination
postsConnection: PostsConnection!
# Get posts with filters
posts(orderBy: String, last: Int, where: PostWhereInput): [Post!]!
# Get single post by slug
post(slug: String!): Post
# Get all categories
categories: [Category!]!
}type Mutation {
# Create a new post
createPost(title: String!, slug: String!, shortPost: String): Post!
}type Post {
id: ID!
title: String!
slug: String!
shortPost: String
createdAt: String!
image: Photo
author: Author!
categories: [Category!]!
}
type Author {
id: ID!
name: String!
bio: String
photo: Photo
}
type Category {
id: ID!
name: String!
slug: String!
}
type Photo {
url: String!
}Get all posts:
query GetAllPosts {
postsConnection {
edges {
node {
id
title
slug
shortPost
createdAt
image {
url
}
author {
name
bio
photo {
url
}
}
categories {
name
slug
}
}
}
}
}Get post by slug:
query GetPost($slug: String!) {
post(slug: $slug) {
title
shortPost
createdAt
image {
url
}
author {
name
bio
photo {
url
}
}
categories {
name
slug
}
}
}Get posts by category:
query GetPostsByCategory($categorySlug: [String!]) {
posts(where: { categories_some: { slug_in: $categorySlug } }) {
title
slug
shortPost
createdAt
}
}| Command | Description |
|---|---|
npm run dev |
Start development server |
npm run build |
Build production bundle |
npm run start |
Start production server |
npm run lint |
Run ESLint |
npm run seed |
Seed database with sample data |
npm run create-indexes |
Create database indexes |
npm run test-db |
Test MongoDB connection |
npm run analyze |
Analyze bundle size |
- Using Next.js Image component
- WebP/AVIF format support
- Lazy loading by default
- Responsive images
- SWC Minification - 7x faster than Babel
- Tree Shaking - Remove unused code
- Code Splitting - Automatic per-route
- Moment.js Optimization - Removes unused locales
- Connection Pooling - Reuse database connections
- Indexes - Fast queries on slug, date, categories
- Pagination - Limit data fetched per request
- Static Generation - Pre-render pages at build time
- ISR (Incremental Static Regeneration) - Revalidate every 60 seconds
- React Query - Client-side data caching (5-minute stale time)
- HTTP Headers - Cache static assets for 1 year
After optimizations:
- β‘ Build Time: 30-50% faster
- π Page Load: 40-60% faster
- π¦ Bundle Size: 20-30% smaller
- πΎ Database Queries: 3-5x faster
- Push your code to GitHub
- Go to Vercel
- Import your repository
- Add environment variables
- Deploy!
# Or use Vercel CLI
npm install -g vercel
vercel- Build the project:
npm run build - Deploy the
.nextfolder - Configure environment variables
- Set build command:
npm run build - Set publish directory:
.next
Make sure to set these in your deployment platform:
MONGODB_URI=your_production_mongodb_uri
NEXT_PUBLIC_GRAPHCMS_ENDPOINT=https://yourdomain.com/api/graphql- Next.js 12.1.6 - React framework
- React 18.1.0 - UI library
- TailwindCSS 3.0.24 - Utility-first CSS
- Moment.js - Date formatting
- React Query - Data fetching and caching
- MongoDB 7.0.0 - NoSQL database
- Apollo Server - GraphQL server
- GraphQL - Query language
- ESLint - Code linting
- PostCSS - CSS processing
- Autoprefixer - CSS vendor prefixes
- Bundle Analyzer - Analyze bundle size
Posts and categories use Next.js dynamic routes:
// /post/[slug].js - Individual post pages
// /category/[slug].js - Category filter pagesPages are pre-rendered at build time for optimal performance:
export async function getStaticProps({ params }) {
const post = await getPostBySlug(params.slug);
return {
props: { post },
revalidate: 60, // ISR - Regenerate every 60 seconds
};
}Custom Apollo Server provides a flexible API:
// /pages/api/graphql.js
const server = new ApolloServer({
typeDefs,
resolvers,
});Efficient database connections:
// Reuses connections in development
// Creates new pool in production
const clientPromise = global._mongoClientPromise || client.connect();Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature - Commit your changes:
git commit -m 'Add some feature' - Push to the branch:
git push origin feature/your-feature - Open a Pull Request
- Follow the existing code style
- Write meaningful commit messages
- Add comments for complex logic
- Test your changes thoroughly
- Update documentation as needed
This project is licensed under the MIT License - see the LICENSE file for details.
Abbosbek Sulaymonov
- Full-stack developer specializing in React, Next.js, TypeScript, and MongoDB
- GitHub: @Abbosbek
- Website: abbosbek.uz
- Next.js team for the amazing framework
- MongoDB for the database solution
- Vercel for hosting platform
- TailwindCSS for the utility-first CSS framework
- The open-source community
If you have any questions or need help, please:
- Check the documentation
- Open an issue
- Contact me directly
Future enhancements planned:
- User authentication system
- Admin dashboard
- Rich text editor for posts
- Search functionality
- Tags system
- Newsletter subscription
- Social media sharing
- Dark mode support
- Multi-language support
- Analytics integration
Built with β€οΈ using Next.js and MongoDB