Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ts): Middleware type tweaks (#38625)
* fix internal type * allow `void` to be returned from Middleware * mark deprecated APIs in JSDoc * fix typo * add missing error page * remove unused import
- Loading branch information
1 parent
8ebd1a2
commit b292bdc
Showing
7 changed files
with
57 additions
and
5 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,34 @@ | ||
# Removed parsed User Agent from Middleware API | ||
|
||
#### Why This Error Occurred | ||
|
||
Your application is interacting with `req.ua` which has been deprecated. | ||
|
||
```ts | ||
// middleware.ts | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
export function middleware(request: NextRequest) { | ||
const viewport = request.ua.device.type === 'mobile' ? 'mobile' : 'desktop' | ||
|
||
request.nextUrl.searchParams.set('viewport', viewport) | ||
return NextResponse.rewrites(request.nextUrl) | ||
} | ||
``` | ||
|
||
#### Possible Ways to Fix It | ||
|
||
The internal logic has been moved into a separate `userAgent` function that you can import from `next/server` and wrap your request instead. | ||
|
||
```ts | ||
// middleware.ts | ||
import { NextRequest, NextResponse, userAgent } from 'next/server' | ||
|
||
export function middleware(request: NextRequest) { | ||
const { device } = userAgent(request) | ||
const viewport = device.type === 'mobile' ? 'mobile' : 'desktop' | ||
|
||
request.nextUrl.searchParams.set('viewport', viewport) | ||
return NextResponse.rewrites(request.nextUrl) | ||
} | ||
``` |
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