harlan-zw / unlighthouse

Scan your entire site with Google Lighthouse in 2 minutes (on average). Open source, fully configurable with minimal setup.
https://unlighthouse.dev
MIT License
3.81k stars 110 forks source link

puppeteer:before-goto hook runs on every url change for Next v12.3.1 #146

Closed RuslanAktaev closed 1 year ago

RuslanAktaev commented 1 year ago

Describe the bug

As I understand from the doc, puppeteer:before-goto hook supposed to run once for each url. But now it runs on every url change (targetchanged event). It leads to the collisions.

Steps to reproduce

I'm using Next v12.3.1 app with NX monorepo. Node v18.15.0

  1. Install Unlighthouse locally
  2. Add unlighthouse.dev.config file to the app:
import { defineConfig } from 'unlighthouse';

const HOST = 'app.dev.mywebsite.me';
const email = 'myemail@example.com';
const password = 'test1234';

export default defineConfig({
  debug: true,
  cache: false,
  site: HOST,
  root: './apps/business',
  urls: ['/', '/launches', '/launches/177'],
  scanner: {
    device: 'desktop',
    skipJavascript: false
  },
  puppeteerOptions: {
    slowMo: 50,
    args: ['--disable-web-security', '--disable-features=IsolateOrigins', '--disable-site-isolation-trials']
  },
  lighthouseOptions: {
    disableStorageReset: true
  },
  hooks: {
    'puppeteer:before-goto': async (page) => {
      await page.goto(`https://${HOST}/sign-in`);

      const emailInput = await page.$('input[type=email]');
      const passwordInput = await page.$('input[type=password]');

      const submitButton = await page.$('button[type=submit]');

      await emailInput.type(email);
      await passwordInput.type(password);

      await Promise.all([page.waitForNavigation(), submitButton.click()]);
    }
  }
});

Expected behavior

Unlighthouse shows report for 3 protected routes:

image

Current behavior

puppeteer:before-goto runs on every url change (on initialization, after await page.goto(https://${HOST}/sign-in), after submit login), that leads to the TargetCloseError:

this._reject(callback, new TargetCloseError('Target closed'));
                                   ^

TargetCloseError: Protocol error (Input.dispatchKeyEvent): Target closed
    at CallbackRegistry.clear (file:///Users/rsaktaev/Documents/Projects/unlighthouse/node_modules/.pnpm/puppeteer-core@20.9.0_typescript@5.1.6/node_modules/puppeteer-core/lib/esm/puppeteer/common/Connection.js:134:36)
    at CDPSessionImpl._onClosed (file:///Users/rsaktaev/Documents/Projects/unlighthouse/node_modules/.pnpm/puppeteer-core@20.9.0_typescript@5.1.6/node_modules/puppeteer-core/lib/esm/puppeteer/common/Connection.js:444:25)
    at Connection.onMessage (file:///Users/rsaktaev/Documents/Projects/unlighthouse/node_modules/.pnpm/puppeteer-core@20.9.0_typescript@5.1.6/node_modules/puppeteer-core/lib/esm/puppeteer/common/Connection.js:243:25)
    at runNextTicks (node:internal/process/task_queues:60:5)
    at listOnTimeout (node:internal/timers:538:9)
    at process.processTimers (node:internal/timers:512:7)

Reproduction

No response

System / Nuxt Info

No response

RuslanAktaev commented 1 year ago

Attaching the pull request GH-147

harlan-zw commented 1 year ago

Hi @RuslanAktaev. As far as I'm aware, this is how its always worked?

I'd recommend that you use the authenticate hook if you're trying to authenticate. It only runs once initially before it starts any of the scans.

See https://unlighthouse.dev/guide/guides/authentication#programmatic-usage

RuslanAktaev commented 1 year ago

@harlan-zw Oh! I didn't see you added a new hook. Perfect! Thank you