Skip to content

Commit

Permalink
Merge pull request #148 from DDD-Community/feature/screenshot-perform…
Browse files Browse the repository at this point in the history
…ance

[FEATURE] 스크린샷 성능 개선
  • Loading branch information
junseublim authored Dec 30, 2024
2 parents 1a6cc6c + 5f52050 commit 4280055
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ jobs:
- name: Delete existing container
run: docker rm -f polabo-fe || true
- name: Run container
run: docker run -d -p 3000:3000 --name polabo-fe ${{ vars.DOCKER_IMAGE }}
run: docker run -d -p 3000:3000 --shm-size=1g --name polabo-fe ${{ vars.DOCKER_IMAGE }}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const DecorateScreenshot = () => {
getBoard(boardId).then((board) => {
setBoardName(board.title)
})
}, [boardId])
}, [])

useEffect(() => {
const takePreview = async () => {
Expand All @@ -55,7 +55,7 @@ const DecorateScreenshot = () => {
}

takePreview()
}, [boardId, polaroidIds])
}, [])

const takeScreenshot = () => {
setIsLoadingDownload(true)
Expand Down
36 changes: 26 additions & 10 deletions src/app/board/api/screenshot/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextResponse } from 'next/server'
import puppeteer, { Browser } from 'puppeteer'
import puppeteer, { Browser, Page } from 'puppeteer'
import { StickerStyle } from '@/types'
import {
createPolaroidSearchParams,
Expand All @@ -12,19 +12,37 @@ type RequestBodyType = {
stickers: StickerStyle[]
}

let browser: Browser | null = null

const initializeBrowser = async () => {
const browser = await puppeteer.launch({ args: ['--no-sandbox'] })
const page = await browser.newPage()
browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-gpu',
'--no-zygote',
'--no-first-run',
'--disable-default-apps',
],
defaultViewport: null,
})
}

const openPage = async () => {
if (browser === null) {
await initializeBrowser()
}

const page = await browser!.newPage()
await page.setViewport({
width: 1080,
height: 1920,
})
return { browser, page }
return page
}

export async function POST(request: Request) {
let browser: Browser | null = null

let page: Page | null = null
try {
const {
boardId,
Expand All @@ -38,9 +56,7 @@ export async function POST(request: Request) {
boardId,
)}/screenshot?${polaroidParams}&${stickerParams}`

const { browser: initializedBrowser, page } = await initializeBrowser()
browser = initializedBrowser

page = await openPage()
await page.goto(url, { waitUntil: 'networkidle2' })

const element = await page.$('div#screenshot_target')
Expand All @@ -61,7 +77,7 @@ export async function POST(request: Request) {
return new NextResponse('Error taking screenshot', { status: 500 })
} finally {
if (browser) {
await browser.close()
await page?.close()
}
}
}

0 comments on commit 4280055

Please sign in to comment.