Introduction
Welcome to our documentation! Here, you'll find important guidelines and best practices to ensure a smooth and successful experience while working on this project.
β Must Follow Guidelinesβ
π File Naming Conventionβ
- Use kebab-case for file names.
- In kebab case, words are separated by hyphens, and all letters are in lowercase.
- Example:
admin-login-form.tsx
π Variable Naming Conventionβ
- Use camelCase for variable names.
- In camel case, the first word is in lowercase, and each subsequent word starts with an uppercase letter, without spaces or underscores.
- Example:
const userName = "John Doe";
π Function Usageβ
- Do not use arrow functions for defining functions.
- Only use them for inline functions. Ex:
<form onSubmit={() => {}}> </form>
β‘ useEffect
Restrictionβ
While useEffect
is a powerful hook for managing side effects, it can lead to unnecessary complexity, performance issues, and bugs if misused. Here are key reasons to use it judiciously:
Reference: YouTube Link
-
State Update Loops:
Updating state withinuseEffect
can trigger re-renders and cause cascading effect executions, potentially leading to infinite loops if dependencies aren't carefully managed. -
Complexity for Data Transformations:
useEffect
isn't suited for synchronous tasks like updating derived state (e.g., recalculating cart totals). These should be handled directly in event handlers or utility functions instead. -
Dependency Array Risks:
Missing dependencies can result in stale data, while overly broad dependency arrays can cause unnecessary effect runs. Managing them correctly is error-prone in complex components. -
Double Execution in Strict Mode:
In React's Strict Mode (development),useEffect
runs twice to help detect unclean side effects, which can confuse developers if not understood properly.
π API Callsβ
- Use TanStack Query for API requests.
- β Do not use
fetch
oraxios
. Always prefer TanStack Query.
π€ AI Usage Restrictionβ
- β Do not use AI-generated code. Write everything manually for better control and quality.
π¨ UI Componentsβ
- Use ShadCN components for UI elements.
- Do not create custom components unless absolutely necessary.
π¨ Stylingβ
- Use Tailwind CSS for styling.
π Git Branching Strategyβ
We follow a structured branch naming convention:
feature/your-feature-name
β For new features.fix/your-fix-name
β For bug fixes.refactor/your-refactor-name
β For code refactoring.hotfix/your-hotfix-name
β For critical fixes when something is breaking in production (top priority).
π Git Commit Messagesβ
- Use CZ Commitizen for commit messages.
π Next.js 101β
- Refer to the official Next.js Documentation for detailed information.
π§ Limitations of Reactβ
- SEO Issues β Since React's client-side rendering sends minimal initial HTML, search engines may not see the content, leading to poor SEO..
- Waterfall Problem β React components often fetch data in a nested manner, where one request depends on another. This leads to multiple sequential API calls, increasing load time and slowing down performance.
π Why is SEO Challenging in React?β
React (with client-side rendering) sends minimal initial HTML:
<!DOCTYPE html>
<html>
<head>
<title>My React App</title>
</head>
<body>
<div id="root"></div>
<script src="bundle.js"></script>
</body>
</html>
The <div id="root"></div>
is empty until JavaScript runs. Search engines may not see content, leading to poor SEO.
Why is This a Problem?β
Search engines rank pages based on content. If content is loaded via JavaScript after the page loads, crawlers may miss it, impacting SEO rankings.
How to Fix Itβ
Ensure search engines can see content without executing JavaScript. Solutions:
1. Server-Side Rendering (SSR)β
Server generates full HTML before sending it to the browser.
Example (Next.js SSR):
export async function getServerSideProps() {
const res = await fetch('https://api.example.com/data');
const data = await res.json();
return { props: { data } };
}
export default function Page({ data }) {
return (
<div>
<h1>{data.title}</h1>
<p>{data.description}</p>
</div>
);
}
2. Static Site Generation (SSG)β
HTML is generated at build time for better SEO.
Example (Next.js SSG):
export async function getStaticProps() {
const res = await fetch('https://api.example.com/data');
const data = await res.json();
return { props: { data } };
}
export default function Page({ data }) {
return (
<div>
<h1>{data.title}</h1>
<p>{data.description}</p>
</div>
);
}
3. Pre-Renderingβ
Generate static HTML files at build time using tools like React Snapshot or React Static.
4. Using a Headless CMSβ
Fetch and render content on the server or via SSG, making it visible to search engines.
5. Meta Tags & Structured Dataβ
Include proper meta tags and structured data.
Example (Next.js Meta Tags):
import Head from 'next/head';
export default function Home() {
return (
<div>
<Head>
<title>My Page Title</title>
<meta name="description" content="This is a description of my page." />
</Head>
<h1>Welcome to My Page</h1>
</div>
);
}
Conclusionβ
React's client-side rendering can cause SEO issues due to empty initial HTML. Using SSR, SSG, pre-rendering, or proper meta tags ensures better search engine visibility and rankings.
β‘ Next.js Overviewβ
- Next.js is a framework built on top of React that makes building websites easier and faster. It helps improve SEO and performance by allowing pages to load quickly with features like server-side rendering (SSR) and static site generation (SSG). It also has built-in tools for handling images, routing, and fetching data, making development smoother and more efficient.
π Routing in Next.jsβ
- Previously, Next.js used Pages Routing with the /pages directory. Later it introduced App Routing in version 13+, using the
/app
directory for a more flexible and powerful routing system.
π Basic Routingβ
In the app/
directory, each folder represents a route, and the page.tsx
file inside defines the page content.
app/page.tsx
β/
(Home Page)app/about/page.tsx
β/about
π Catch-All Segmentsβ
- Catch-all segments let you create one dynamic route that can handle multiple URL parts. This is useful when you donβt know how many segments a URL might have.
π How It Works
- Use
[...slug]
inside theapp/
directory to capture one or more URL segments.
β Example
If you create this file:
app/blog/[...slug]/page.tsx
It can match different URLs like:
/blog/post-1
β["post-1"]
/blog/category/post-2
β["category", "post-2"]
π Dynamic Routingβ
Use square brackets [ ]
to create dynamic routes.
app/product/[id]/page.tsx
β Accessible as/product/1
,/product/2
, etc.
π Nested Layouts & Shared UIβ
Layouts help structure pages efficiently.
app/layout.tsx
applies a layout to all pages insideapp/
.app/dashboard/layout.tsx
β A layout specific to/dashboard/*
pages.
π API Routes in App Routerβ
Instead of pages/api/
, APIs are now created using route.tsx
files.
app/api/user/route.tsx
β Accessible at/api/user
.
π Loading & Error Handlingβ
loading.tsx
β Shows a loading state while the page loads.error.tsx
β Handles page-specific errors gracefully.
π Route Groupsβ
Route Groups in Next.js help organize your projectβs folder structure without affecting the URL. They are created by wrapping a folder name in parentheses ( ).
- Reference: Next.js Documentation
- Helps manage large projects with multiple sections (e.g., admin vs. public).
- Example:
app/(admin)/dashboard/page.tsx
does not include(admin)
in the URL.
β‘ Next.js 15 and await
in Parametersβ
- In Next.js 15, route handlers support
await
in parameters, simplifying data fetching. - Example:
export default async function Page({ params }: { params: { id: string } }) {
const data = await fetchData(params.id);
return <div>{data.title}</div>;
}