forked from Mirrors/elk
pass url as path variable
This commit is contained in:
parent
26bc4e7251
commit
2f72c03fd9
2 changed files with 23 additions and 9 deletions
|
@ -22,9 +22,7 @@ watch(cardImage, (image) => {
|
||||||
imageSrc.value = image
|
imageSrc.value = image
|
||||||
|
|
||||||
if (image) {
|
if (image) {
|
||||||
$fetch<string>('/api/og-image', {
|
$fetch<string>(`/api/og-image/${encodeURIComponent(props.card.url)}`).then((ogImageUrl) => {
|
||||||
params: { cardUrl: props.card.url },
|
|
||||||
}).then((ogImageUrl) => {
|
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log('ogImageUrl', ogImageUrl)
|
console.log('ogImageUrl', ogImageUrl)
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,23 @@ function extractOgImageUrl(html: string): string {
|
||||||
return match?.[1] ?? ''
|
return match?.[1] ?? ''
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function resolveOgImageUrlManually(cardUrl: string): Promise<string> {
|
||||||
|
const html = await $fetch<string>(cardUrl)
|
||||||
|
|
||||||
|
const ogImageUrl = extractOgImageUrl(html)
|
||||||
|
|
||||||
|
if (!ogImageUrl) {
|
||||||
|
// Throw an error so we can try to apply another fallback
|
||||||
|
throw new Error('Could not find og:image in html.')
|
||||||
|
}
|
||||||
|
|
||||||
|
return ogImageUrl
|
||||||
|
}
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const { cardUrl } = getQuery(event)
|
const { url } = getRouterParams(event)
|
||||||
|
|
||||||
|
const cardUrl = decodeURIComponent(url)
|
||||||
|
|
||||||
if (!cardUrl) {
|
if (!cardUrl) {
|
||||||
throw createError({
|
throw createError({
|
||||||
|
@ -39,13 +54,14 @@ export default defineEventHandler(async (event) => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let ogImageUrl = ''
|
// If anything goes wrong, fail gracefully
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// First we want to try to get the og:image from the html
|
// First we want to try to get the og:image from the html
|
||||||
// But sometimes it is not included due to async JS loading
|
// But sometimes it is not included due to async JS loading
|
||||||
const html = await $fetch<string>(cardUrl)
|
let ogImageUrl = await resolveOgImageUrlManually(cardUrl).catch(() =>
|
||||||
ogImageUrl = extractOgImageUrl(html)
|
// Try another fallback
|
||||||
|
'',
|
||||||
|
)
|
||||||
|
|
||||||
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
|
Loading…
Reference in a new issue