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

[BUG] Nw: Target page, context or browser has been closed #345

Open
dominicarrojado opened this issue Mar 15, 2025 · 0 comments
Open

[BUG] Nw: Target page, context or browser has been closed #345

dominicarrojado opened this issue Mar 15, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@dominicarrojado
Copy link

dominicarrojado commented Mar 15, 2025

Environment

  • chromium Version: 133
  • playwright-core Version: 1.51
  • Node.js Version: nodejs22.x
  • Lambda / GCF Runtime: nodejs22.x

Expected Behavior

I should see the page title from the response.

{"statusCode": 200, "body": "{\"pageTitle\":\"Example Domain\"}"}

Current Behavior

Got an error when invoking the function locally:

{"errorType": "Nw", "errorMessage": "Target page, context or browser has been closed", "trace": [""]}
2025-03-15T09:47:38.140Z        d435d0ff-dad4-4af3-934d-ad1f4800f8ae    ERROR   Unhandled Promise Rejection     {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Nw: Target page, context or browser has been closed","reason":{"errorType":"Nw","errorMessage":"Target page, context or browser has been closed","name":"Nw","stack":[""]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Nw: Target page, context or browser has been closed","    at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)","    at process.emit (node:events:518:28)","    at emitUnhandledRejection (node:internal/process/promises:252:13)","    at throwUnhandledRejectionsMode (node:internal/process/promises:388:19)","    at processPromiseRejections (node:internal/process/promises:475:17)","    at process.processTicksAndRejections (node:internal/process/task_queues:106:32)"]}

Steps to Reproduce

SAM Template

# template.yaml
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: my-lambda-function

Globals:
  Function:
    Timeout: 10
    MemorySize: 1024

Resources:
  ChromiumLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      Description: Chromium with Node.js integration for AWS Lambda
      ContentUri: layers/chromium
      CompatibleRuntimes:
        - &NodejsRuntime nodejs22.x
      CompatibleArchitectures:
        - &ChromiumArch x86_64
      RetentionPolicy: Delete
    Metadata:
      BuildMethod: *NodejsRuntime
      BuildArchitecture: *ChromiumArch

  MyLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: my-lambda/
      Handler: app.lambdaHandler
      Runtime: *NodejsRuntime
      Architectures:
        - *ChromiumArch
      Layers:
        - !Ref ChromiumLayer

    Metadata:
      BuildMethod: esbuild
      BuildProperties:
        Minify: true
        Target: "es2020"
        Sourcemap: true
        EntryPoints:
          - app.ts
        External:
          - "@sparticuz/chromium"

My code

// my-lambda/app.ts
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { chromium as playwright, Page } from 'playwright-core';
import chromium from '@sparticuz/chromium';

export const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
  try {
    const browser = await playwright.launch({
      args: chromium.args,
      executablePath: await chromium.executablePath(),
      headless: true,
    });

    const context = await browser.newContext();
    const page = await context.newPage();

    await page.goto('https://example.com');

    const pageTitle = await page.title();

    await browser.close();

    return {
      statusCode: 200,
      body: JSON.stringify({
        pageTitle,
      }),
    };
  catch (err) {
    return {
      statusCode: 200,
      body: JSON.stringify(err.message),
    }
  }

Chromium Layer

// layers/chromium/package.json
{
  "name": "ChromiumLayer",
  "private": true,
  "version": "1.0.0",
  "description": "Chromium layer for AWS Lambda",
  "dependencies": {
    "@sparticuz/chromium": "133"
  }
}

Possible Solution

When I use puppeteer-core, I don't face this issue but I would like to use Playwright if possible. Please help to check if I did something wrong here. Thank you

@dominicarrojado dominicarrojado added the bug Something isn't working label Mar 15, 2025
@dominicarrojado dominicarrojado changed the title [BUG] [BUG] {"errorType": "Nw", "errorMessage": "Target page, context or browser has been closed", "trace": [""]} Mar 15, 2025
@dominicarrojado dominicarrojado changed the title [BUG] {"errorType": "Nw", "errorMessage": "Target page, context or browser has been closed", "trace": [""]} [BUG] Nw: Target page, context or browser has been closed Mar 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant