Skip to content

Commit

Permalink
chore: improve docker build and lighthouse config
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Sep 30, 2024
1 parent 8b6a740 commit 57ebaf6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ WORKDIR /app/tianji
# We don't need the standalone Chromium
ENV PUPPETEER_SKIP_DOWNLOAD=true
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
ENV DEBUG=puppeteer:*

RUN apk add --no-cache \
udev \
Expand Down
40 changes: 23 additions & 17 deletions src/server/utils/screenshot/lighthouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,37 @@ export async function generateLighthouse(url: string): Promise<Result> {
const browser = await puppeteer.launch({
// Set to false if you want to see the script in action.
headless: 'new',
args: ['--no-sandbox'],
args: ['--no-sandbox', '--single-process'],
defaultViewport: null,
ignoreDefaultArgs: ['--enable-automation'],
dumpio: true,
});
const page = await browser.newPage();

// Wait for Lighthouse to open url, then inject our stylesheet.
browser.on('targetchanged', async (target) => {
if (page && page.url() === url) {
await page.addStyleTag({ content: '* {color: red}' });
}
});
try {
const page = await browser.newPage();

// Lighthouse will open the URL.
// Puppeteer will observe `targetchanged` and inject our stylesheet.
const res = await lighthouse(url, undefined, undefined, page);
if (!res) {
throw new Error('Lighthouse failed to generate report');
}
// Wait for Lighthouse to open url, then inject our stylesheet.
browser.on('targetchanged', async (target) => {
if (page && page.url() === url) {
await page.addStyleTag({ content: '* {color: red}' });
}
});

const { lhr } = res;
// Lighthouse will open the URL.
// Puppeteer will observe `targetchanged` and inject our stylesheet.
const res = await lighthouse(url, undefined, undefined, page);
if (!res) {
throw new Error('Lighthouse failed to generate report');
}

page.close({ runBeforeUnload: false });

await browser.close();
const { lhr } = res;

return lhr;
return lhr;
} finally {
await browser.close();
}
}

export function getLighthouseReport(lhr: Result): string {
Expand Down

0 comments on commit 57ebaf6

Please sign in to comment.