Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/libs/chromium.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { Browser, PuppeteerLaunchOptions } from 'puppeteer-core'
import puppeteer from 'puppeteer-core'

export const Chromium = (() => {
const instances: WeakMap<PuppeteerLaunchOptions, Browser> = new WeakMap()

const createInstance = (options: PuppeteerLaunchOptions) => {

Check warning on line 7 in src/libs/chromium.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable")

Check warning on line 7 in src/libs/chromium.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable")
// eslint-disable-next-line functional/no-expression-statements
console.log('&&&&&', 'new chromium instanse will be created')
return puppeteer.launch(options)
}

return {
getInstance: async (options: PuppeteerLaunchOptions) => {

Check warning on line 14 in src/libs/chromium.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable")

Check warning on line 14 in src/libs/chromium.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable")
const fromCache = instances.get(options)
const instance = fromCache
? fromCache
: (instances
.set(options, await createInstance(options))
.get(options) as Browser)
return instance
},
}
})()
19 changes: 9 additions & 10 deletions src/pages/api/generate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable functional/no-expression-statements */
import { type APIRoute } from 'astro'
import puppeteer from 'puppeteer-core'
import chromium from "@sparticuz/chromium-min";
import chromium from '@sparticuz/chromium-min'
import { Chromium } from '../../libs/chromium.ts'

const exePath =
process.platform === 'win32'
Expand All @@ -10,10 +10,8 @@ const exePath =
? '/usr/bin/google-chrome'
: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'



const chromiumPack = "https://github.com/Sparticuz/chromium/releases/download/v121.0.0/chromium-v121.0.0-pack.tar"

const chromiumPack =
'https://github.com/Sparticuz/chromium/releases/download/v121.0.0/chromium-v121.0.0-pack.tar'

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

const browser = await puppeteer.launch(options)
const browser = await Chromium.getInstance(options)
const page = await browser.newPage()

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

// close the browser
await browser.close()
// to reuse the instance, now commented out.
// await browser.close()

// Return the PNG image as the response
return new Response(file, {
status: 200,
headers: {
'Content-Type': 'image/png',
'access-control-allow-origin': '*',
'cache-control': `public, max-age=31536000`,
'access-control-allow-origin': '*',
'cache-control': `public, max-age=31536000`,
},
})
} catch (error) {
Expand Down