Run Lighthouse on pages that require a login using Puppeteer, Chrome DevTools, request headers, or a debug Chrome instance.
By default, Lighthouse loads a page as a new user with no session or storage data. Pages behind a login require additional setup. There are several approaches depending on your use case.
Puppeteer is the most flexible approach. It lets you automate the login flow before handing the authenticated page to Lighthouse.
npm install puppeteer lighthouse
import puppeteer from 'puppeteer';import lighthouse from 'lighthouse';async function login(page, origin) { await page.goto(origin); await page.waitForSelector('input[type="email"]', { visible: true }); const emailInput = await page.$('input[type="email"]'); await emailInput.type('[email protected]'); const passwordInput = await page.$('input[type="password"]'); await passwordInput.type('password'); await Promise.all([ page.$eval('.login-form', form => form.submit()), page.waitForNavigation(), ]);}async function main() { const browser = await puppeteer.launch({ headless: 'new', slowMo: 50, }); const page = await browser.newPage(); // Log in before running Lighthouse. await login(page, 'https://example.com'); // Pass the authenticated page to Lighthouse. // disableStorageReset preserves the login session. const result = await lighthouse( 'https://example.com/dashboard', { disableStorageReset: true }, undefined, page ); await browser.close(); console.log(JSON.stringify(result.lhr, null, 2));}await main();
Pass disableStorageReset: true to Lighthouse when reusing a Puppeteer page.
Without it, Lighthouse will clear storage on startup and your session will be
lost before the audit begins.
The Lighthouse panel in Chrome DevTools never clears cookies. Log in to your site normally, then open the Lighthouse panel and run an audit.If your authentication depends on localStorage or indexedDB, uncheck Clear storage in the Lighthouse panel settings before running.
Setting the Cookie header via --extra-headers overrides all other cookies
for the session. For cookie-based authentication with multiple cookies, use
the Puppeteer approach (Option 1) instead.