forked from Mirrors/elk
more safe
This commit is contained in:
parent
a34e7e23f0
commit
26bc4e7251
1 changed files with 22 additions and 13 deletions
|
@ -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)
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue