Skip to content

Commit 094b6c3

Browse files
committed
optimize: size.
1 parent f9a2c48 commit 094b6c3

File tree

5 files changed

+61
-44
lines changed

5 files changed

+61
-44
lines changed

Dockerfile

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
1-
FROM node:22-slim AS base
2-
ENV PNPM_HOME="/pnpm"
3-
ENV PATH="$PNPM_HOME:$PATH"
4-
RUN corepack enable
1+
# install and optimize dependencies
2+
FROM zenika/alpine-chrome:with-node AS base
3+
54
WORKDIR /app
5+
6+
# copy package files
67
COPY package.json pnpm-lock.yaml ./
7-
RUN corepack prepare
88

9-
FROM base AS prod-deps
10-
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile
9+
# install pnpm as root,
10+
# then switch back to chrome user
11+
USER root
12+
RUN npm install -g pnpm@10
13+
14+
# install dependencies as normal user
15+
USER chrome
16+
RUN pnpm install --prod --frozen-lockfile && \
17+
pnpm prune --prod && \
18+
# remove unnecessary files to reduce image size
19+
find /app/node_modules -name "*.md" -delete && \
20+
find /app/node_modules -type d -name "test*" -exec rm -rf {} + 2>/dev/null || true && \
21+
find /app/node_modules -type d -name "doc*" -exec rm -rf {} + 2>/dev/null || true && \
22+
find /app/node_modules -name ".cache" -type d -exec rm -rf {} + 2>/dev/null || true && \
23+
# clean up caches
24+
rm -rf ~/.pnpm ~/.npm /tmp/* /var/cache/apk/*
25+
26+
# production stage
27+
FROM zenika/alpine-chrome:with-node
28+
29+
# environment configuration
30+
ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 \
31+
PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/usr/bin/chromium-browser \
32+
NODE_ENV=production
33+
34+
WORKDIR /app
35+
USER chrome
1136

12-
FROM base AS build
13-
COPY . .
37+
# copy application files
38+
COPY package.json ./
39+
COPY --from=base /app/node_modules ./node_modules
40+
COPY src/index.js ./src/
1441

15-
FROM base
16-
COPY --from=prod-deps /app/node_modules /app/node_modules
17-
RUN pnpm playwright install --with-deps chromium
18-
COPY --from=build /app/src/index.js /app/src/index.js
19-
CMD ["pnpm", "start"]
42+
# start the application
43+
CMD ["node", "src/index.js"]

docker-compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
appwrite-browser:
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
6+
image: appwrite/browser:local
7+
ports:
8+
- "3000:3000"
9+
environment:
10+
- PORT=3000

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"dependencies": {
1919
"h3": "^1.13.0",
2020
"lighthouse": "^12.2.1",
21-
"playwright": "^1.52.0",
21+
"playwright-core": "^1.52.0",
2222
"playwright-lighthouse": "^4.0.0",
2323
"zod": "^3.23.8"
2424
},

pnpm-lock.yaml

Lines changed: 8 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { readValidatedBody } from "h3";
99
import lighthouseDesktopConfig from "lighthouse/core/config/lr-desktop-config.js";
1010
import lighthouseMobileConfig from "lighthouse/core/config/lr-mobile-config.js";
11-
import { chromium } from "playwright";
11+
import { chromium } from "playwright-core";
1212
import { playAudit } from "playwright-lighthouse";
1313
import { z } from "zod";
1414

@@ -20,6 +20,7 @@ const router = createRouter();
2020
console.log("Chromium starting...");
2121
const browser = await chromium.launch({
2222
args: ["--remote-debugging-port=9222"],
23+
executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH,
2324
});
2425
console.log("Chromium started!");
2526

@@ -36,9 +37,10 @@ const defaultContext = {
3637
const screenshotSchema = z.object({
3738
url: z.string().url(),
3839
theme: z.enum(["light", "dark"]).default("light"),
39-
headers: z.record(z.string(), z.any()),
40+
headers: z.record(z.string(), z.any()).default({}),
4041
sleep: z.number().min(0).max(60000).default(3000),
4142
});
43+
4244
router.post(
4345
"/v1/screenshots",
4446
defineEventHandler(async (event) => {

0 commit comments

Comments
 (0)