Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve deprecation errors for new middleware API #30316

Merged
merged 6 commits into from Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions errors/manifest.json
Expand Up @@ -502,6 +502,10 @@
{
"title": "swc-minify-enabled",
"path": "/errors/swc-minify-enabled.md"
},
{
"title": "middleware-new-signature",
"path": "/errors/middleware-new-signature.md"
}
]
}
Expand Down
35 changes: 35 additions & 0 deletions errors/middleware-new-signature.md
@@ -0,0 +1,35 @@
# Middleware new signature
styfle marked this conversation as resolved.
Show resolved Hide resolved

#### Why This Error Occurred

Your application is using a Middleware function that is using parameters from the deprecated API.

```typescript
import { NextResponse } from 'next/server'
styfle marked this conversation as resolved.
Show resolved Hide resolved

export function middleware(event) {
if (event.request.nextUrl.pathname === '/blocked') {
event.respondWith(
new Response(null, {
status: 403,
})
)
}
}
```

#### Possible Ways to Fix It

Update to use the new API for Middleware:

```typescript
import { NextResponse } from 'next/server'
styfle marked this conversation as resolved.
Show resolved Hide resolved

export function middleware(request) {
if (request.nextUrl.pathname === '/blocked') {
return new Response(null, {
status: 403,
})
}
}
```
Expand Up @@ -26,7 +26,8 @@ export default function middlewareLoader(this: any) {
export default function (opts) {
return adapter({
...opts,
handler
page: ${JSON.stringify(page)},
handler,
})
}
`
Expand Down
38 changes: 25 additions & 13 deletions packages/next/server/web/adapter.ts
Expand Up @@ -8,22 +8,27 @@ import { waitUntilSymbol } from './spec-compliant/fetch-event'

export async function adapter(params: {
handler: (request: NextRequest, event: NextFetchEvent) => Promise<Response>
page: string
request: RequestData
}): Promise<FetchEventResult> {
const url = params.request.url.startsWith('/')
? `https://${params.request.headers.host}${params.request.url}`
: params.request.url

const request = new NextRequestHint(url, {
geo: params.request.geo,
headers: fromNodeHeaders(params.request.headers),
ip: params.request.ip,
method: params.request.method,
nextConfig: params.request.nextConfig,
page: params.request.page,
const request = new NextRequestHint({
page: params.page,
input: url,
init: {
geo: params.request.geo,
headers: fromNodeHeaders(params.request.headers),
ip: params.request.ip,
method: params.request.method,
nextConfig: params.request.nextConfig,
page: params.request.page,
},
})

const event = new NextFetchEvent(request)
const event = new NextFetchEvent({ request, page: params.page })
const original = await params.handler(request, event)

return {
Expand All @@ -33,19 +38,26 @@ export async function adapter(params: {
}

class NextRequestHint extends NextRequest {
constructor(input: Request | string, init: RequestInit = {}) {
super(input, init)
sourcePage: string

constructor(params: {
init: RequestInit
input: Request | string
page: string
}) {
super(params.input, params.init)
this.sourcePage = params.page
}

get request() {
throw new DeprecationError()
throw new DeprecationError({ page: this.sourcePage })
}

respondWith() {
throw new DeprecationError()
throw new DeprecationError({ page: this.sourcePage })
}

waitUntil() {
throw new DeprecationError()
throw new DeprecationError({ page: this.sourcePage })
}
}
6 changes: 4 additions & 2 deletions packages/next/server/web/error.ts
@@ -1,10 +1,12 @@
export class DeprecationError extends Error {
constructor() {
super(`Middleware now accepts an async API directly with the form:
constructor({ page }: { page: string }) {
super(`The middleware "${page}" accepts an async API directly with the form:

export function middleware(request, event) {
return new Response("Hello " + request.url)
}

More: https://nextjs.org/docs/messages/middleware-new-signature
styfle marked this conversation as resolved.
Show resolved Hide resolved
`)
}
}
20 changes: 15 additions & 5 deletions packages/next/server/web/spec-extension/fetch-event.ts
Expand Up @@ -3,13 +3,23 @@ import { FetchEvent } from '../spec-compliant/fetch-event'
import { NextRequest } from './request'

export class NextFetchEvent extends FetchEvent {
readonly request: NextRequest
constructor(request: NextRequest) {
super(request)
this.request = request
sourcePage: string

constructor(params: { request: NextRequest; page: string }) {
super(params.request)
this.sourcePage = params.page
}

// @ts-ignore
get request() {
throw new DeprecationError({
page: this.sourcePage,
})
}

respondWith() {
throw new DeprecationError()
throw new DeprecationError({
page: this.sourcePage,
})
}
}