Skip to content

Commit

Permalink
fix(ts): Middleware type tweaks (#38625)
Browse files Browse the repository at this point in the history
* 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
balazsorban44 committed Aug 8, 2022
1 parent 8ebd1a2 commit b292bdc
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 5 deletions.
4 changes: 4 additions & 0 deletions errors/manifest.json
Expand Up @@ -719,6 +719,10 @@
{
"title": "failed-to-fetch-devpagesmanifest",
"path": "/errors/failed-to-fetch-devpagesmanifest.md"
},
{
"title": "middleware-parse-user-agent",
"path": "/errors/middleware-parse-user-agent.md"
}
]
}
Expand Down
34 changes: 34 additions & 0 deletions errors/middleware-parse-user-agent.md
@@ -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)
}
```
2 changes: 1 addition & 1 deletion errors/middleware-request-page.md
Expand Up @@ -2,7 +2,7 @@

#### Why This Error Occurred

Your application is interacting with `request.page`, and it's being deprecated.
Your application is interacting with `request.page` which has been deprecated.

```typescript
// middleware.ts
Expand Down
Expand Up @@ -2,7 +2,6 @@ import type { NextConfig } from '../../../../server/config-shared'
import type { DocumentType, AppType } from '../../../../shared/lib/utils'
import type { BuildManifest } from '../../../../server/get-page-files'
import type { ReactLoadableManifest } from '../../../../server/load-components'
import type { NextRequest } from '../../../../server/web/spec-extension/request'

import WebServer from '../../../../server/web-server'
import {
Expand Down Expand Up @@ -104,7 +103,7 @@ export function getRender({
})
const requestHandler = server.getRequestHandler()

return async function render(request: NextRequest) {
return async function render(request: Request) {
const extendedReq = new WebNextRequest(request)
const extendedRes = new WebNextResponse()
requestHandler(extendedReq, extendedRes)
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/web/error.ts
Expand Up @@ -21,7 +21,7 @@ export class RemovedPageError extends Error {

export class RemovedUAError extends Error {
constructor() {
super(`The request.page has been removed in favour of \`userAgent\` function.
super(`The request.ua has been removed in favour of \`userAgent\` function.
Read more: https://nextjs.org/docs/messages/middleware-parse-user-agent
`)
}
Expand Down
10 changes: 10 additions & 0 deletions packages/next/server/web/spec-extension/request.ts
Expand Up @@ -71,10 +71,20 @@ export class NextRequest extends Request {
return this[INTERNALS].url
}

/**
* @deprecated
* `page` has been deprecated in favour of `URLPattern`.
* Read more: https://nextjs.org/docs/messages/middleware-request-page
*/
public get page() {
throw new RemovedPageError()
}

/**
* @deprecated
* `ua` has been removed in favour of \`userAgent\` function.
* Read more: https://nextjs.org/docs/messages/middleware-parse-user-agent
*/
public get ua() {
throw new RemovedUAError()
}
Expand Down
7 changes: 6 additions & 1 deletion packages/next/server/web/types.ts
Expand Up @@ -52,7 +52,12 @@ export interface FetchEventResult {
waitUntil: Promise<any>
}

export type NextMiddlewareResult = NextResponse | Response | null | undefined
export type NextMiddlewareResult =
| NextResponse
| Response
| null
| undefined
| void

export type NextMiddleware = (
request: NextRequest,
Expand Down

0 comments on commit b292bdc

Please sign in to comment.