1- import type { BrowserContextOptions } from "playwright-core" ;
1+ import type { BrowserContext , BrowserContextOptions } from "playwright-core" ;
22import { playAudit } from "playwright-lighthouse" ;
33import { browser , defaultContext , lighthouseConfigs } from "../config" ;
44import { lighthouseSchema } from "../schemas" ;
55
66export async function handleReportsRequest ( req : Request ) : Promise < Response > {
7+ let context : BrowserContext | undefined ;
8+
79 try {
810 const json = await req . json ( ) ;
911 const body = lighthouseSchema . parse ( json ) ;
@@ -19,7 +21,7 @@ export async function handleReportsRequest(req: Request): Promise<Response> {
1921 if ( body . locale ) contextOptions . locale = body . locale ;
2022 if ( body . timezoneId ) contextOptions . timezoneId = body . timezoneId ;
2123
22- const context = await browser . newContext ( contextOptions ) ;
24+ context = await browser . newContext ( contextOptions ) ;
2325
2426 // Grant permissions if specified
2527 if ( body . permissions && body . permissions . length > 0 ) {
@@ -72,7 +74,6 @@ export async function handleReportsRequest(req: Request): Promise<Response> {
7274 thresholds,
7375 } ) ;
7476
75- await context . close ( ) ;
7677 const report = Array . isArray ( results . report )
7778 ? results . report . join ( "" )
7879 : results . report ;
@@ -85,5 +86,7 @@ export async function handleReportsRequest(req: Request): Promise<Response> {
8586 status : 400 ,
8687 headers : { "Content-Type" : "application/json" } ,
8788 } ) ;
89+ } finally {
90+ await context ?. close ( ) ;
8891 }
8992}
0 commit comments