-
Notifications
You must be signed in to change notification settings - Fork 26.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(middleware): deprecate request.page
- Loading branch information
Showing
9 changed files
with
140 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Deprecated page into Middleware API | ||
|
||
#### Why This Error Occurred | ||
|
||
Your application is interacting with `request.page`, and it's being deprecated. | ||
|
||
```typescript | ||
// middleware.ts | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
export function middleware(request: NextRequest) { | ||
const { params } = event.request.page | ||
const { locale, slug } = params | ||
|
||
if (locale && slug) { | ||
const { search, protocol, host } = request.nextUrl | ||
const url = new URL(`${protocol}//${locale}.${host}/${slug}${search}`) | ||
return NextResponse.redirect(url) | ||
} | ||
} | ||
``` | ||
|
||
#### Possible Ways to Fix It | ||
|
||
You can use [URLPattern](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern) instead to have the same behavior: | ||
|
||
```typescript | ||
// middleware.ts | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
const PATTERNS = [ | ||
[ | ||
new URLPattern({ pathname: '/:locale/:slug' }), | ||
({ pathname }) => pathname.groups, | ||
], | ||
] | ||
|
||
const params = (url) => { | ||
const input = url.split('?')[0] | ||
let result = {} | ||
|
||
for (const [pattern, handler] of PATTERNS) { | ||
const patternResult = pattern.exec(input) | ||
if (patternResult !== null && 'pathname' in patternResult) { | ||
result = handler(patternResult) | ||
break | ||
} | ||
} | ||
return result | ||
} | ||
|
||
export function middleware(request: NextRequest) { | ||
const { locale, slug } = params(request.url) | ||
|
||
if (locale && slug) { | ||
const { search, protocol, host } = request.nextUrl | ||
const url = new URL(`${protocol}//${locale}.${host}/${slug}${search}`) | ||
return NextResponse.redirect(url) | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters