THE_DEVELOPER_GUIDE Telegram 5632
ู…ูู‡ูˆู… ุงู„ู€ Middleware ููŠ Next.js ๐Ÿ’ก
.
.
ุฃูƒูŠุฏ ู‚ุงุจู„ุช ุณูŠู†ุงุฑูŠูˆ ุฅู†ูƒ ู…ุญุชุงุฌ ุชุชุญูƒู… ููŠ ุงู„ู€ requests ุงู„ู„ูŠ ุฏุงุฎู„ุฉ ู„ู…ูˆู‚ุนูƒ ุฃูˆ ุชุนู…ู„ checks ู…ุนูŠู†ุฉ ู‚ุจู„ ู…ุง ุงู„ู€ user ูŠุดูˆู ุงู„ู€ page. ู…ุซู„ู‹ุง:

- ู…ุญุชุงุฌ ุชู…ู†ุน user ู…ุด ุนุงู…ู„ login ุฅู†ู‡ ูŠุฏุฎู„ ุตูุญุฉ ู…ุนูŠู†ุฉ.
- ุฃูˆ ุชุนู…ู„ redirect ู„ูˆ user ู…ุด ุนู†ุฏู‡ ุตู„ุงุญูŠุงุช.
- ุฃูˆ ุญุชู‰ ุชุถูŠู headers ุฃูˆ ุชุนู…ู„ logging ู„ูƒู„ request.

ุงู„ุญุชุฉ ุฏูŠ ููŠ ุฃูŠ app ุจุชุจู‚ู‰ critical ุฌุฏู‹ุงุŒ ู„ุฃู†ูƒ ุณุงุนุงุช ู…ุญุชุงุฌ layer ูƒุฏู‡ ููŠ ุงู„ู†ุต ุจูŠู† request ุงู„ู€ user ูˆุงู„ู€ response ุงู„ู„ูŠ ุฑุงุฌุน. ูˆู‡ู†ุง ูŠูŠุฌูŠ ุฏูˆุฑ ุงู„ู€ Middleware...

โ€”โ€”โ€”

๐Ÿ”ฅ ูŠุนู†ูŠ ุฅูŠู‡ MiddlewareุŸ

ุงู„ู€ Middleware ุจุจุณุงุทุฉ ุนุจุงุฑุฉ ุนู† function ุจุชุชู†ูุฐ ู‚ุจู„ ู…ุง ุงู„ู€ request ูŠูˆุตู„ ู„ู„ู€ route.

ูŠุนู†ูŠ ู‡ูˆ ูˆุงู‚ู ูƒู€ ุญุงุฑุณ ุงู„ุจูˆุงุจุฉ: ุฃูŠ request ู„ุงุฒู… ูŠุนุฏูŠ ู…ู†ู‡ ุงู„ุฃูˆู„ุŒ ูˆุณุงุนุชู‡ุง ุฃู†ุช ุชู‚ุฏุฑ:

- ุชุบูŠุฑ ููŠ ุงู„ู€ request ุฃูˆ ุงู„ู€ response.
- ุชุนู…ู„ redirect ุฃูˆ rewrite.
- ุชู…ู†ุน requests ู…ุนูŠู†ุฉ.
- ุฃูˆ ุญุชู‰ ุชุถูŠู logic ุฒูŠ ุงู„ู€ authentication, logging, A/B testing, rate limitingโ€ฆ ุฅู„ุฎ.

โ€”โ€”โ€”

๐Ÿ“Œ ุฅุฒุงูŠ ุงู„ู€ Middleware ุจูŠุดุชุบู„ ููŠ Next.jsุŸ

ููŠ Next.js 13 (ูˆุงู„ู€ App Router)ุŒ ู„ูˆ ุนุงูŠุฒ ุชุนู…ู„ Middleware ุจุชุถูŠู ู…ู„ู ุจุงุณู…:

middleware.ts or middleware.js

ููŠ ุงู„ู€ root ุจุชุงุน ุงู„ู€ project.

ุงู„ู€ function ุงู„ุฃุณุงุณูŠุฉ ุจุชูƒูˆู† ูƒุฏู‡:

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function middleware(req: NextRequest) {
// Example: check if user is authenticated
const token = req.cookies.get('token')

if (!token) {
return NextResponse.redirect(new URL('/login', req.url))
}

return NextResponse.next()
}

export const config = {
matcher: ['/dashboard/:path*'],
}


โœ… ุงู„ู„ูŠ ุจูŠุญุตู„ ู‡ู†ุง:

- ุฃูŠ request ุฏุงุฎู„ ุนู„ู‰ /dashboard/* ู„ุงุฒู… ูŠุนุฏูŠ ู…ู† ุงู„ู€ middleware.
- ู„ูˆ ู…ููŠุด token: ูŠุชุนู…ู„ redirect ุนู„ู‰ /login.
- ู„ูˆ ููŠู‡ token: ูŠูƒู…ู„ ุนุงุฏูŠ.

โ€”โ€”โ€”

โšก๏ธ ุฃู‡ู… ู…ู…ูŠุฒุงุช ุงู„ู€ Middleware ููŠ Next.js:

1- ุงู„ู€ Edge Runtime:
ุจูŠุชู†ูุฐ ุนู„ู‰ ุงู„ู€ Edge (Cloudflare Workers/Akamaiโ€ฆ ุฅู„ุฎ) ูŠุนู†ูŠ ultra fast ูˆู…ุด ู…ุญุชุงุฌ server ุชู‚ูŠู„.

2- ุงู„ู€ Lightweight:
ู…ุด ู…ุนู…ูˆู„ ุฅู†ู‡ ูŠุดูŠู„ logic ุชู‚ูŠู„ ุฌุฏู‹ุงุŒ ู‡ูˆ ุจุณ layer ุณุฑูŠุนุฉ ู„ู„ู€ requests.

3- ุงู„ู€ Selective Matching:
ุชู‚ุฏุฑ ุชุญุฏุฏ routes ู…ุนูŠู†ุฉ ุจุณ ุงู„ู„ูŠ ุชุนุฏูŠ ู…ู† ุงู„ู€ middleware ุนู† ุทุฑูŠู‚ ุงู„ู€ matcher.

4- ุนู†ุฏู‡ Use Cases ู…ุชู†ูˆุนุฉ:

- Authentication & Authorization.
- Redirects & Rewrites.
- A/B Testing
- Bot Protection.
- Internationalization

โ€”โ€”โ€”

ุฎู„ูŠ ุจุงู„ูƒ ุฅู† ุงู„ู€ Middleware ู…ุด ู…ูƒุงู† ุชุญุท ููŠู‡ ูƒู„ ุงู„ู€ business logic ุจุชุงุนูƒุŒ ู‡ูˆ ู…ุนู…ูˆู„ ู„ู„ุญุงุฌุงุช ุงู„ู„ูŠ ู…ุญุชุงุฌุฉ ุชุชู†ูุฐ ุจุณุฑุนุฉ ูˆู‚ุจู„ ู…ุง ุงู„ู€ request ูŠุฑูˆุญ ู„ู„ู€ route.

ูŠุนู†ูŠ authentication, headers, redirectsโ€ฆ ุญุงุฌุงุช ูƒุฏู‡. ู„ูƒู† ุงู„ู€ heavy logic ู…ูƒุงู†ู‡ุง ู…ุด ู‡ู†ุง.

โ€”โ€”โ€”

ูˆูู‚ูƒู… ุงู„ู„ู‡ ู„ูƒู„ ุฎูŠุฑ ๐ŸŒฟ
โค8



tgoop.com/the_developer_guide/5632
Create:
Last Update:

ู…ูู‡ูˆู… ุงู„ู€ Middleware ููŠ Next.js ๐Ÿ’ก
.
.
ุฃูƒูŠุฏ ู‚ุงุจู„ุช ุณูŠู†ุงุฑูŠูˆ ุฅู†ูƒ ู…ุญุชุงุฌ ุชุชุญูƒู… ููŠ ุงู„ู€ requests ุงู„ู„ูŠ ุฏุงุฎู„ุฉ ู„ู…ูˆู‚ุนูƒ ุฃูˆ ุชุนู…ู„ checks ู…ุนูŠู†ุฉ ู‚ุจู„ ู…ุง ุงู„ู€ user ูŠุดูˆู ุงู„ู€ page. ู…ุซู„ู‹ุง:

- ู…ุญุชุงุฌ ุชู…ู†ุน user ู…ุด ุนุงู…ู„ login ุฅู†ู‡ ูŠุฏุฎู„ ุตูุญุฉ ู…ุนูŠู†ุฉ.
- ุฃูˆ ุชุนู…ู„ redirect ู„ูˆ user ู…ุด ุนู†ุฏู‡ ุตู„ุงุญูŠุงุช.
- ุฃูˆ ุญุชู‰ ุชุถูŠู headers ุฃูˆ ุชุนู…ู„ logging ู„ูƒู„ request.

ุงู„ุญุชุฉ ุฏูŠ ููŠ ุฃูŠ app ุจุชุจู‚ู‰ critical ุฌุฏู‹ุงุŒ ู„ุฃู†ูƒ ุณุงุนุงุช ู…ุญุชุงุฌ layer ูƒุฏู‡ ููŠ ุงู„ู†ุต ุจูŠู† request ุงู„ู€ user ูˆุงู„ู€ response ุงู„ู„ูŠ ุฑุงุฌุน. ูˆู‡ู†ุง ูŠูŠุฌูŠ ุฏูˆุฑ ุงู„ู€ Middleware...

โ€”โ€”โ€”

๐Ÿ”ฅ ูŠุนู†ูŠ ุฅูŠู‡ MiddlewareุŸ

ุงู„ู€ Middleware ุจุจุณุงุทุฉ ุนุจุงุฑุฉ ุนู† function ุจุชุชู†ูุฐ ู‚ุจู„ ู…ุง ุงู„ู€ request ูŠูˆุตู„ ู„ู„ู€ route.

ูŠุนู†ูŠ ู‡ูˆ ูˆุงู‚ู ูƒู€ ุญุงุฑุณ ุงู„ุจูˆุงุจุฉ: ุฃูŠ request ู„ุงุฒู… ูŠุนุฏูŠ ู…ู†ู‡ ุงู„ุฃูˆู„ุŒ ูˆุณุงุนุชู‡ุง ุฃู†ุช ุชู‚ุฏุฑ:

- ุชุบูŠุฑ ููŠ ุงู„ู€ request ุฃูˆ ุงู„ู€ response.
- ุชุนู…ู„ redirect ุฃูˆ rewrite.
- ุชู…ู†ุน requests ู…ุนูŠู†ุฉ.
- ุฃูˆ ุญุชู‰ ุชุถูŠู logic ุฒูŠ ุงู„ู€ authentication, logging, A/B testing, rate limitingโ€ฆ ุฅู„ุฎ.

โ€”โ€”โ€”

๐Ÿ“Œ ุฅุฒุงูŠ ุงู„ู€ Middleware ุจูŠุดุชุบู„ ููŠ Next.jsุŸ

ููŠ Next.js 13 (ูˆุงู„ู€ App Router)ุŒ ู„ูˆ ุนุงูŠุฒ ุชุนู…ู„ Middleware ุจุชุถูŠู ู…ู„ู ุจุงุณู…:

middleware.ts or middleware.js

ููŠ ุงู„ู€ root ุจุชุงุน ุงู„ู€ project.

ุงู„ู€ function ุงู„ุฃุณุงุณูŠุฉ ุจุชูƒูˆู† ูƒุฏู‡:

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function middleware(req: NextRequest) {
// Example: check if user is authenticated
const token = req.cookies.get('token')

if (!token) {
return NextResponse.redirect(new URL('/login', req.url))
}

return NextResponse.next()
}

export const config = {
matcher: ['/dashboard/:path*'],
}


โœ… ุงู„ู„ูŠ ุจูŠุญุตู„ ู‡ู†ุง:

- ุฃูŠ request ุฏุงุฎู„ ุนู„ู‰ /dashboard/* ู„ุงุฒู… ูŠุนุฏูŠ ู…ู† ุงู„ู€ middleware.
- ู„ูˆ ู…ููŠุด token: ูŠุชุนู…ู„ redirect ุนู„ู‰ /login.
- ู„ูˆ ููŠู‡ token: ูŠูƒู…ู„ ุนุงุฏูŠ.

โ€”โ€”โ€”

โšก๏ธ ุฃู‡ู… ู…ู…ูŠุฒุงุช ุงู„ู€ Middleware ููŠ Next.js:

1- ุงู„ู€ Edge Runtime:
ุจูŠุชู†ูุฐ ุนู„ู‰ ุงู„ู€ Edge (Cloudflare Workers/Akamaiโ€ฆ ุฅู„ุฎ) ูŠุนู†ูŠ ultra fast ูˆู…ุด ู…ุญุชุงุฌ server ุชู‚ูŠู„.

2- ุงู„ู€ Lightweight:
ู…ุด ู…ุนู…ูˆู„ ุฅู†ู‡ ูŠุดูŠู„ logic ุชู‚ูŠู„ ุฌุฏู‹ุงุŒ ู‡ูˆ ุจุณ layer ุณุฑูŠุนุฉ ู„ู„ู€ requests.

3- ุงู„ู€ Selective Matching:
ุชู‚ุฏุฑ ุชุญุฏุฏ routes ู…ุนูŠู†ุฉ ุจุณ ุงู„ู„ูŠ ุชุนุฏูŠ ู…ู† ุงู„ู€ middleware ุนู† ุทุฑูŠู‚ ุงู„ู€ matcher.

4- ุนู†ุฏู‡ Use Cases ู…ุชู†ูˆุนุฉ:

- Authentication & Authorization.
- Redirects & Rewrites.
- A/B Testing
- Bot Protection.
- Internationalization

โ€”โ€”โ€”

ุฎู„ูŠ ุจุงู„ูƒ ุฅู† ุงู„ู€ Middleware ู…ุด ู…ูƒุงู† ุชุญุท ููŠู‡ ูƒู„ ุงู„ู€ business logic ุจุชุงุนูƒุŒ ู‡ูˆ ู…ุนู…ูˆู„ ู„ู„ุญุงุฌุงุช ุงู„ู„ูŠ ู…ุญุชุงุฌุฉ ุชุชู†ูุฐ ุจุณุฑุนุฉ ูˆู‚ุจู„ ู…ุง ุงู„ู€ request ูŠุฑูˆุญ ู„ู„ู€ route.

ูŠุนู†ูŠ authentication, headers, redirectsโ€ฆ ุญุงุฌุงุช ูƒุฏู‡. ู„ูƒู† ุงู„ู€ heavy logic ู…ูƒุงู†ู‡ุง ู…ุด ู‡ู†ุง.

โ€”โ€”โ€”

ูˆูู‚ูƒู… ุงู„ู„ู‡ ู„ูƒู„ ุฎูŠุฑ ๐ŸŒฟ

BY DevGuide ๐Ÿ‡ต๐Ÿ‡ธ


Share with your friend now:
tgoop.com/the_developer_guide/5632

View MORE
Open in Telegram


Telegram News

Date: |

1What is Telegram Channels? Just as the Bitcoin turmoil continues, crypto traders have taken to Telegram to voice their feelings. Crypto investors can reduce their anxiety about losses by joining the โ€œBear Market Screaming Therapy Groupโ€ on Telegram. Public channels are public to the internet, regardless of whether or not they are subscribed. A public channel is displayed in search results and has a short address (link). How to create a business channel on Telegram? (Tutorial) There have been several contributions to the group with members posting voice notes of screaming, yelling, groaning, and wailing in different rhythms and pitches. Calling out the โ€œdegenerateโ€ community or the crypto obsessives that engage in high-risk trading, Co-founder of NFT renting protocol Rentable World emiliano.eth shared this group on his Twitter. He wrote: โ€œhey degen, are you stressed? Just let it out all out. Voice only tg channel for screamingโ€.
from us


Telegram DevGuide ๐Ÿ‡ต๐Ÿ‡ธ
FROM American