Skip to content

Commit

Permalink
feat: use single browser instance
Browse files Browse the repository at this point in the history
  • Loading branch information
junseublim committed Dec 30, 2024
1 parent 8ccc204 commit fb0dd9d
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 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, Page } from 'puppeteer'
import puppeteer, { Page } from 'puppeteer'
import { StickerStyle } from '@/types'
import {
createPolaroidSearchParams,
Expand All @@ -12,28 +12,20 @@ type RequestBodyType = {
stickers: StickerStyle[]
}

let browser: Browser | null = null

const initializeBrowser = async () => {
browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-gpu',
'--no-zygote',
'--no-first-run',
'--disable-default-apps',
],
defaultViewport: null,
})
}
const 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()
const page = await browser.newPage()
await page.setViewport({
width: 1080,
height: 1920,
Expand Down Expand Up @@ -76,8 +68,8 @@ export async function POST(request: Request) {
console.error('Error taking screenshot:', error)
return new NextResponse('Error taking screenshot', { status: 500 })
} finally {
if (browser) {
await page?.close()
if (page) {
await page.close()
}
}
}

0 comments on commit fb0dd9d

Please sign in to comment.