Skip to content

Commit ef791ff

Browse files
authored
Merge pull request #19 from dev-protocol/singleton-chromium
Make chromium a singleton
2 parents 88310a2 + 59ec81c commit ef791ff

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/libs/chromium.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { Browser, PuppeteerLaunchOptions } from 'puppeteer-core'
2+
import puppeteer from 'puppeteer-core'
3+
4+
export const Chromium = (() => {
5+
const instances: WeakMap<PuppeteerLaunchOptions, Browser> = new WeakMap()
6+
7+
const createInstance = (options: PuppeteerLaunchOptions) => {
8+
// eslint-disable-next-line functional/no-expression-statements
9+
console.log('&&&&&', 'new chromium instanse will be created')
10+
return puppeteer.launch(options)
11+
}
12+
13+
return {
14+
getInstance: async (options: PuppeteerLaunchOptions) => {
15+
const fromCache = instances.get(options)
16+
const instance = fromCache
17+
? fromCache
18+
: (instances
19+
.set(options, await createInstance(options))
20+
.get(options) as Browser)
21+
return instance
22+
},
23+
}
24+
})()

src/pages/api/generate.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable functional/no-expression-statements */
22
import { type APIRoute } from 'astro'
3-
import puppeteer from 'puppeteer-core'
4-
import chromium from "@sparticuz/chromium-min";
3+
import chromium from '@sparticuz/chromium-min'
4+
import { Chromium } from '../../libs/chromium.ts'
55

66
const exePath =
77
process.platform === 'win32'
@@ -10,10 +10,8 @@ const exePath =
1010
? '/usr/bin/google-chrome'
1111
: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
1212

13-
14-
15-
const chromiumPack = "https://github.com/Sparticuz/chromium/releases/download/v121.0.0/chromium-v121.0.0-pack.tar"
16-
13+
const chromiumPack =
14+
'https://github.com/Sparticuz/chromium/releases/download/v121.0.0/chromium-v121.0.0-pack.tar'
1715

1816
const Localoptions = {
1917
args: [],
@@ -43,7 +41,7 @@ export const GET: APIRoute = async ({ url }) => {
4341
return new Response('Invalid URL format', { status: 400 })
4442
}
4543

46-
const browser = await puppeteer.launch(options)
44+
const browser = await Chromium.getInstance(options)
4745
const page = await browser.newPage()
4846

4947
// set the viewport size
@@ -62,15 +60,16 @@ export const GET: APIRoute = async ({ url }) => {
6260
})
6361

6462
// close the browser
65-
await browser.close()
63+
// to reuse the instance, now commented out.
64+
// await browser.close()
6665

6766
// Return the PNG image as the response
6867
return new Response(file, {
6968
status: 200,
7069
headers: {
7170
'Content-Type': 'image/png',
72-
'access-control-allow-origin': '*',
73-
'cache-control': `public, max-age=31536000`,
71+
'access-control-allow-origin': '*',
72+
'cache-control': `public, max-age=31536000`,
7473
},
7574
})
7675
} catch (error) {

0 commit comments

Comments
 (0)