最近在团队中落地CI/CD 缓存策略优化, and accumulated quite a bit of experience. Here's a summary for reference, hoping it helps those doing similar work.
Core Concepts
Let's start with the basic implementation:
javascript
FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN corepack enable && pnpm install --frozen-lockfile
FROM node:20-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN pnpm build
FROM nginx:alpine AS runner
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
这段代码展示了基本的使用方式。实际项目中还需要考虑错误处理和边界条件。
In-Depth Analysis
Building on this foundation, we can further optimize:
javascript
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
if (entry.entryType === 'largest-contentful-paint') {
reportMetric('LCP', entry.startTime)
}
if (entry.entryType === 'first-input') {
reportMetric('FID', entry.processingStart - entry.startTime)
}
}
})
observer.observe({ entryTypes: ['largest-contentful-paint', 'first-input'] })
This pattern is very practical in large projects and can significantly reduce maintenance costs.
Implementation Experience
实际项目中的用法会更复杂一些:
javascript
FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN corepack enable && pnpm install --frozen-lockfile
FROM node:20-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN pnpm build
FROM nginx:alpine AS runner
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Through this approach, both the testability and scalability of the code are improved.
Optimization Strategies
Here is a complete example:
javascript
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
if (entry.entryType === 'largest-contentful-paint') {
reportMetric('LCP', entry.startTime)
}
if (entry.entryType === 'first-input') {
reportMetric('FID', entry.processingStart - entry.startTime)
}
}
})
observer.observe({ entryTypes: ['largest-contentful-paint', 'first-input'] })
Pay attention to boundary condition handling, which is critical in production.
Summary
- Understanding underlying principles is more important than memorizing APIs
- Always verify compatibility before using in production
- In team collaboration, conventions and documentation are more important than the technology itself