From fb0dd9d5335f4720a772928c5dff39e1a9b9daea Mon Sep 17 00:00:00 2001 From: junseublim Date: Mon, 30 Dec 2024 19:04:40 +0900 Subject: [PATCH] feat: use single browser instance --- src/app/board/api/screenshot/route.ts | 38 +++++++++++---------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/src/app/board/api/screenshot/route.ts b/src/app/board/api/screenshot/route.ts index d0d3931..9331a83 100644 --- a/src/app/board/api/screenshot/route.ts +++ b/src/app/board/api/screenshot/route.ts @@ -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, @@ -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, @@ -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() } } }