chore: improve docker build and lighthouse config

This commit is contained in:
moonrailgun 2024-09-30 14:48:24 +08:00
parent 8b6a74033c
commit 57ebaf6ad3
2 changed files with 25 additions and 18 deletions

View File

@ -35,6 +35,7 @@ WORKDIR /app/tianji
# We don't need the standalone Chromium # We don't need the standalone Chromium
ENV PUPPETEER_SKIP_DOWNLOAD=true ENV PUPPETEER_SKIP_DOWNLOAD=true
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
ENV DEBUG=puppeteer:*
RUN apk add --no-cache \ RUN apk add --no-cache \
udev \ udev \

View File

@ -8,10 +8,13 @@ export async function generateLighthouse(url: string): Promise<Result> {
const browser = await puppeteer.launch({ const browser = await puppeteer.launch({
// Set to false if you want to see the script in action. // Set to false if you want to see the script in action.
headless: 'new', headless: 'new',
args: ['--no-sandbox'], args: ['--no-sandbox', '--single-process'],
defaultViewport: null, defaultViewport: null,
ignoreDefaultArgs: ['--enable-automation'], ignoreDefaultArgs: ['--enable-automation'],
dumpio: true,
}); });
try {
const page = await browser.newPage(); const page = await browser.newPage();
// Wait for Lighthouse to open url, then inject our stylesheet. // Wait for Lighthouse to open url, then inject our stylesheet.
@ -28,11 +31,14 @@ export async function generateLighthouse(url: string): Promise<Result> {
throw new Error('Lighthouse failed to generate report'); throw new Error('Lighthouse failed to generate report');
} }
page.close({ runBeforeUnload: false });
const { lhr } = res; const { lhr } = res;
await browser.close();
return lhr; return lhr;
} finally {
await browser.close();
}
} }
export function getLighthouseReport(lhr: Result): string { export function getLighthouseReport(lhr: Result): string {