File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed 
examples/app-pages-router 
packages/tests-e2e/tests/appPagesRouter Expand file tree Collapse file tree 2 files changed +22
-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+ // @ts -expect-error - It will cause a warning at build time, but it should just work 
6+ const  topLevelAwait  =  await  new  Promise < string > ( ( resolve )  =>  { 
7+   setTimeout ( ( )  =>  { 
8+     resolve ( "top-level-await" ) ; 
9+   } ,  150 ) ; 
10+ } ) ; 
11+ 
412export  function  middleware ( request : NextRequest )  { 
513  const  path  =  request . nextUrl . pathname ;  //new URL(request.url).pathname; 
614
@@ -25,6 +33,14 @@ export function middleware(request: NextRequest) {
2533      } , 
2634    } ) ; 
2735  } 
36+   if  ( path  ===  "/api/middlewareTopLevelAwait" )  { 
37+     return  new  NextResponse ( JSON . stringify ( {  hello : topLevelAwait  } ) ,  { 
38+       status : 200 , 
39+       headers : { 
40+         "content-type" : "application/json" , 
41+       } , 
42+     } ) ; 
43+   } 
2844  const  rHeaders  =  new  Headers ( request . headers ) ; 
2945  const  r  =  NextResponse . next ( { 
3046    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