File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed
examples/app-pages-router
packages/tests-e2e/tests/appPagesRouter Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change 11import type { NextRequest } from "next/server" ;
22import { NextResponse } from "next/server" ;
33
4+ // Needed to test top-level await
5+ // We are using `setTimeout` to simulate a "long" running operation
6+ // we could have used `Promise.resolve` instead, but it would be running in a different way in the event loop
7+ // @ts -expect-error - It will cause a warning at build time, but it should just work
8+ const topLevelAwait = await new Promise < string > ( ( resolve ) => {
9+ setTimeout ( ( ) => {
10+ resolve ( "top-level-await" ) ;
11+ } , 10 ) ;
12+ } ) ;
13+
414export function middleware ( request : NextRequest ) {
515 const path = request . nextUrl . pathname ; //new URL(request.url).pathname;
616
@@ -25,6 +35,14 @@ export function middleware(request: NextRequest) {
2535 } ,
2636 } ) ;
2737 }
38+ if ( path === "/api/middlewareTopLevelAwait" ) {
39+ return new NextResponse ( JSON . stringify ( { hello : topLevelAwait } ) , {
40+ status : 200 ,
41+ headers : {
42+ "content-type" : "application/json" ,
43+ } ,
44+ } ) ;
45+ }
2846 const rHeaders = new Headers ( request . headers ) ;
2947 const r = NextResponse . next ( {
3048 request : {
Original file line number Diff line number Diff line change @@ -27,3 +27,9 @@ test("API call from middleware", async ({ page }) => {
2727 el = page . getByText ( 'API: { "hello": "middleware" }' ) ;
2828 await expect ( el ) . toBeVisible ( ) ;
2929} ) ;
30+
31+ test ( "API call from middleware with top-level await" , async ( { request } ) => {
32+ const response = await request . get ( "/api/middlewareTopLevelAwait" ) ;
33+ const data = await response . json ( ) ;
34+ expect ( data ) . toEqual ( { hello : "top-level-await" } ) ;
35+ } ) ;
You can’t perform that action at this time.
0 commit comments