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: |

While some crypto traders move toward screaming as a coping mechanism, many mental health experts have argued that โ€œscream therapyโ€ is pseudoscience. Scientific research or no, it obviously feels good. While the character limit is 255, try to fit into 200 characters. This way, users will be able to take in your text fast and efficiently. Reveal the essence of your channel and provide contact information. For example, you can add a bot name, link to your pricing plans, etc. 2How to set up a Telegram channel? (A step-by-step tutorial) Just at this time, Bitcoin and the broader crypto market have dropped to new 2022 lows. The Bitcoin price has tanked 10 percent dropping to $20,000. On the other hand, the altcoin space is witnessing even more brutal correction. Bitcoin has dropped nearly 60 percent year-to-date and more than 70 percent since its all-time high in November 2021. On June 7, Perekopsky met with Brazilian President Jair Bolsonaro, an avid user of the platform. According to the firm's VP, the main subject of the meeting was "freedom of expression."
from us


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