forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Middleware API signature (vercel#30282)
Co-authored-by: Steven <[email protected]>
- Loading branch information
1 parent
4782cac
commit 0910e8b
Showing
14 changed files
with
190 additions
and
114 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
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 |
---|---|---|
@@ -1,36 +1,51 @@ | ||
import type { RequestData, FetchEventResult } from './types' | ||
|
||
import { DeprecationError } from './error' | ||
import { fromNodeHeaders } from './utils' | ||
import { NextFetchEvent } from './spec-extension/fetch-event' | ||
import { NextRequest } from './spec-extension/request' | ||
import { NextRequest, RequestInit } from './spec-extension/request' | ||
import { NextResponse } from './spec-extension/response' | ||
import { waitUntilSymbol, responseSymbol } from './spec-compliant/fetch-event' | ||
import { waitUntilSymbol } from './spec-compliant/fetch-event' | ||
|
||
export async function adapter(params: { | ||
handler: (event: NextFetchEvent) => void | Promise<void> | ||
handler: (request: NextRequest, event: NextFetchEvent) => Promise<Response> | ||
request: RequestData | ||
}): Promise<FetchEventResult> { | ||
const url = params.request.url.startsWith('/') | ||
? `https://${params.request.headers.host}${params.request.url}` | ||
: params.request.url | ||
|
||
const event = new NextFetchEvent( | ||
new NextRequest(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(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 handled = params.handler(event) | ||
const original = await event[responseSymbol] | ||
const event = new NextFetchEvent(request) | ||
const original = await params.handler(request, event) | ||
|
||
return { | ||
promise: Promise.resolve(handled), | ||
response: original || NextResponse.next(), | ||
waitUntil: Promise.all(event[waitUntilSymbol]), | ||
} | ||
} | ||
|
||
class NextRequestHint extends NextRequest { | ||
constructor(input: Request | string, init: RequestInit = {}) { | ||
super(input, init) | ||
} | ||
|
||
get request() { | ||
throw new DeprecationError() | ||
} | ||
|
||
respondWith() { | ||
throw new DeprecationError() | ||
} | ||
|
||
waitUntil() { | ||
throw new DeprecationError() | ||
} | ||
} |
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,10 @@ | ||
export class DeprecationError extends Error { | ||
constructor() { | ||
super(`Middleware now accepts an async API directly with the form: | ||
export function middleware(request, event) { | ||
return new Response("Hello " + request.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { NextResponse } from 'next/server' | ||
|
||
export function middleware(event) { | ||
const url = event.request.nextUrl | ||
export async function middleware(request) { | ||
const url = request.nextUrl | ||
if (url.pathname === '/redirect-with-basepath' && !url.basePath) { | ||
url.basePath = '/root' | ||
event.respondWith(NextResponse.redirect(url)) | ||
return NextResponse.redirect(url) | ||
} | ||
|
||
if (url.pathname === '/redirect-with-basepath') { | ||
url.pathname = '/about' | ||
event.respondWith(NextResponse.rewrite(url)) | ||
return NextResponse.rewrite(url) | ||
} | ||
} |
24 changes: 11 additions & 13 deletions
24
test/integration/middleware-core/pages/interface/_middleware.js
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 |
---|---|---|
@@ -1,14 +1,12 @@ | ||
export function middleware(event) { | ||
event.respondWith( | ||
new Response(null, { | ||
headers: { | ||
'req-url-basepath': event.request.nextUrl.basePath, | ||
'req-url-pathname': event.request.nextUrl.pathname, | ||
'req-url-params': JSON.stringify(event.request.page.params), | ||
'req-url-page': event.request.page.name, | ||
'req-url-query': event.request.nextUrl.searchParams.get('foo'), | ||
'req-url-locale': event.request.nextUrl.locale, | ||
}, | ||
}) | ||
) | ||
export function middleware(request) { | ||
return new Response(null, { | ||
headers: { | ||
'req-url-basepath': request.nextUrl.basePath, | ||
'req-url-pathname': request.nextUrl.pathname, | ||
'req-url-params': JSON.stringify(request.page.params), | ||
'req-url-page': request.page.name, | ||
'req-url-query': request.nextUrl.searchParams.get('foo'), | ||
'req-url-locale': request.nextUrl.locale, | ||
}, | ||
}) | ||
} |
8 changes: 2 additions & 6 deletions
8
test/integration/middleware-core/pages/redirects/_middleware.js
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
Oops, something went wrong.