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 = ''
|
let ogImageUrl = ''
|
||||||
|
|
||||||
// First we want to try to get the og:image from the html
|
try {
|
||||||
// But sometimes it is not included due to async JS loading
|
// First we want to try to get the og:image from the html
|
||||||
const html = await $fetch<string>(cardUrl)
|
// But sometimes it is not included due to async JS loading
|
||||||
ogImageUrl = extractOgImageUrl(html)
|
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 no og:image was found, try to get it from opengraph.io
|
||||||
if (!ogImageUrl) {
|
if (!ogImageUrl) {
|
||||||
const response = await getOpenGraphClient().getSiteInfo(cardUrl)
|
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