more safe

This commit is contained in:
Shinigami92 2022-12-12 16:31:29 +01:00
parent a34e7e23f0
commit 26bc4e7251

View file

@ -41,23 +41,32 @@ export default defineEventHandler(async (event) => {
let ogImageUrl = ''
// First we want to try to get the og:image from the html
// But sometimes it is not included due to async JS loading
const html = await $fetch<string>(cardUrl)
ogImageUrl = extractOgImageUrl(html)
try {
// First we want to try to get the og:image from the html
// But sometimes it is not included due to async JS loading
const html = await $fetch<string>(cardUrl)
ogImageUrl = extractOgImageUrl(html)
if (process.env.NUXT_OPENGRAPH_API) {
if (process.env.NUXT_OPENGRAPH_API) {
// If no og:image was found, try to get it from opengraph.io
if (!ogImageUrl) {
const response = await getOpenGraphClient().getSiteInfo(cardUrl)
if (!ogImageUrl) {
const response = await getOpenGraphClient().getSiteInfo(cardUrl)
ogImageUrl = response?.openGraph?.image?.url || response?.hybridGraph?.image || ''
ogImageUrl = response?.openGraph?.image?.url || response?.hybridGraph?.image || ''
}
}
// eslint-disable-next-line no-console
console.log(JSON.stringify({ cardUrl, ogImageUrl }))
await send(event, ogImageUrl)
}
catch (error) {
throw createError({
statusCode: 500,
statusMessage: (error as Error)?.message || 'Unknown error.',
cause: error,
})
}
// eslint-disable-next-line no-console
console.log(JSON.stringify({ cardUrl, ogImageUrl }))
await send(event, ogImageUrl)
})