- It Uses React for building user interface
- Next.js is a React framework for building full-stack web applications. You use React Components to build user interfaces, and Next.js for additional features and optimizations.
- Provides additional features that enable you to build production-ready applications
- Next.js simplifies the process of building a web application for production
- Routing
- API Routes
- Rendering (support both rendering server side and client side)
- Data fetching
- Styling
- Optimization
- Dev and prod build system
- HTML, CSS, JavaScript fundamentals
- ES6 + features
- React fundamental
- Node.js
- VS code
npx create-next-app@latest
- cd file-location
- npm run dev
React Server Component (RSC)
- React Server Components is a new architecture introduced by the React team in version 18 which was quickly embraced by Next.js
- The architecture introduces a new way of creating React components, splitting them into two types.
- Server components
- client components
- In Next.js, all components are server components by default
- They have the ability to run tasks like reading files or fetching data from a database
- However, they don't have the ability to use hooks or handle user interactions
- To create a client component, it's necessary to add "use client" at the end of the component file.
- Client components can't perform tasks like reading files, but they have the ability to use hooks and manage interactions.
- Next.js has a file-system based routing mechanism
- URL paths that users can access in the browser are defined by files and folders in your codebase
- All routes must be placed inside the app folder
- Every file that corresponds to a route must be named page.js or page.tsx
- Every folder corresponds to a path segment in the browser URL
- Scenario 1:
- -> src/app(page.tsx)
- Scenario 2:
- -> src/app/about(page.tsx)
- -> src/app/profile(page.tsx)
- Scenario 3:
- -> src/app(page.tsx)/blog(page.tsx)
- Scenario 4:
- -> src/app(page.tsx)/blog(page.tsx)/first(page.tsx)
- Scenario 5:
- -> src/app(page.tsx)/blog(page.tsx)/second(page.tsx)
-
Scenario 1:
- -> src/app(page.tsx)/products(page.tsx)/[productId]
const DetailProducts = ({ params }: { params: { productId: string } }) => { return <div>DetailProducts {params.productId}</div>; }; export default DetailProducts;
-
Scenario 1:
-
-> src/app(page.tsx)/products(page.tsx)/[productId]/reviews/[reviewId] - http://localhost:3000/products/7/reviews/1
``` const ReviewDeatails = ({ params, }: { params: {productId: string; reviewId: string }; }) => { return (
Review Details {params.reviewId} for product {params.productId}); };export default ReviewDeatails; ```
-
-
Scenario 1:
- -> src/app(page.tsx)/docs/[...slug]
const Docs = () => { return <div>Docs Home Page</div>; }; export default Docs; -
http://localhost:3000/docs/feature1/concept1
Feature 1
- concept 1
- concept 2
- concept 3 Feature 2 Feature 3 Feature 4
-
-> src/app(page.tsx)/docs/[...slug]
-
http://localhost:3000/docs/feature2/segment
const Docs = ({ params, }: { params: { slug: string[]; }; }) => { if (params.slug.length === 2) { return ( <h1> {" "} Viewing docs for feature {params.slug[0]} and concept {params.slug[1]} </h1> ); } else if (params.slug.length === 1) { return <h1> Viewing docs for feature {params.slug[0]} </h1>; } return <div>Docs Home Page</div>; }; export default Docs;
- app
- docs
- [[...slugs]]
- page.tsx
- [[...slugs]]
- layout.tsx
- page.tsx
- docs
- -> src/app/not-found.tsx
- -> src/app/dashboard/line-chart.tsx
- GET not found page because of use line-chart.tsx instead of page.tsx
-
-> src/app/_lib/private_folder.tsx
-
-> src/app/_lib/page.tsx
-
A private folder indicates that it is a private implementation detail and should not be considered by the routing system.
-
The folder and all its sub-folders are excluded from routing
-
Prefix the folder name with an underscore
- For separating UI logic from routing logic
- For consistently organizing internal files across a project
- For sorting and grouping files in code editors
- And finally, for avoiding potential naming conflicts with future next.js file conventions
-
Allows us to logically group our routes and project files without affecting the URL path structure.
-
Let's implement authentication routes
-
Register
-
Login
-
Forgot Password
- -> src/app/login/page.tsx
- -> src/app/register/page.tsx
- -> src/app/forgot-password/page.tsx
- -> src/app/auth/login/page.tsx
- -> src/app/auth/register/page.tsx
- -> src/app/auth/forgot-password/page.tsx
- app
- auth
- login (page.tsx)
- register (page.tsx)
- forgot-password (page.tsx)
- auth
-
app
- (auth)
- login (page.tsx)
- register (page.tsx)
- forgot-password (page.tsx)
- (auth)
-
app
- (routeg)
- ro (page.tsx)
- rotwo (page.tsx)
- (routeg)
-
- A page is UI that is unique to a route
- A layout is UI that is shared between multiple pages in the app
| Header |
|---|
| Content |
| Footer |
-
You can define a layout by default exporting a React component from a layout.js or layout.tsx file
-
That component should accept a children prop that will be populated with a child page during rendering
-
-> src/app/layout.tsx is automatic
- app
- layout.tsx
- products
- [productId]
- layout.tsx
- page.tsx
- page.tsx
-page.tsx
- Route Group uses:
- To organize your project in a manner that doesn't affect the URL
- To selectively apply a layout to certain segments while leaving others unchanged
- Ensuring proper search engine optimization (SEO) is crucial for increasing visibility and attracting users.
- Next.js introduced the Metadata API which allows you to define metadata for each page
- Metadata ensures accurate and relevant information is displayed when your pages are shared or indexed
- Export a static metadata object
- Export a dynamic generateMetadata function
-
Both layout.tsx and page.tsx files can export metadata If defined in a layout, it applies to all pages in that layout, but if defined in a page, it applies only to that page.
-
Metadata is read in order, from the root level down to the final page level.
export const metadata = { title: "About Next.js", };- dynamic metadata
- -> src/app/product/[[productId]]/page.tsx
export const generateMetadata = ({ params }: Props): Metadata => { return { title: `Product ${params.productId} `, }; };
-
The title field's primary purpose is to define the document title
-
It can be either a string or an object
export const metadata: Metadata = { title: { absolute: "", default: "Next.js Tutorial - TH Raju", template: "%s | TH-Next.js", }, description: "Generated by create next app", };export const metadata: Metadata = { title: "Blog", }; -
Now if i visit blog route on browser then i see --- Blog | TH-Next.js
-
To enable client-side navigation Next.js provides us with the link component
-
The
<Link>component is a React component that extends the HTML<a>element, and it's the primary way to navigate between routes in Next.js -
To use it, we need to import it from "next/link"
<Link href="/blog" > Blog </Link> <Link href="/blog" target="__blank"> Blog </Link> // it will open on new window <Link href="/blog" replace> Blog </Link> // it will relocate to home page because it will clear the route history
-
import {usePathname} from 'next/navigation';
-
add on function
-
const pathname = usePathname();
-
const isActive = pathname.startsWith(link.href);
<Link href="/blog" className = {isActive ? "font-bold mr-4": "text-blue-500 mr-4"} > Blog </Link>
(.) to match segments on the same level (..) to match segments one level above (..)(..) to match segments two levels above