use srcset

This commit is contained in:
Shinigami92 2022-12-12 18:43:38 +01:00
parent 2f72c03fd9
commit ed091d284f
2 changed files with 15 additions and 23 deletions

View file

@ -8,30 +8,13 @@ const props = defineProps<{
/** When it is root card in the list, not appear as a child card */ /** When it is root card in the list, not appear as a child card */
root?: boolean root?: boolean
}>() }>()
const cardImage = computed(() => props.card.image) const cardImage = $computed(() => props.card.image)
const alt = $computed(() => `${props.card.title} - ${props.card.title}`) const alt = $computed(() => `${props.card.title} - ${props.card.title}`)
const isSquare = $computed(() => props.smallPictureOnly || props.card.width === props.card.height) const isSquare = $computed(() => props.smallPictureOnly || props.card.width === props.card.height)
const providerName = $computed(() => props.card.providerName ? props.card.providerName : new URL(props.card.url).hostname) const providerName = $computed(() => props.card.providerName ? props.card.providerName : new URL(props.card.url).hostname)
const imageSrc = ref<string | null>() const imageSrcset = $computed(() => props.card.image ? `${props.card.image}, /api/og-image/${encodeURIComponent(props.card.url)} 2x` : '')
// TODO: handle card.type: 'photo' | 'video' | 'rich'; // TODO: handle card.type: 'photo' | 'video' | 'rich';
// Only try to fetch og:image if the card.image is already provided from mastodon
// We only want to improve the image quality
watch(cardImage, (image) => {
imageSrc.value = image
if (image) {
$fetch<string>(`/api/og-image/${encodeURIComponent(props.card.url)}`).then((ogImageUrl) => {
// eslint-disable-next-line no-console
console.log('ogImageUrl', ogImageUrl)
// Only override if ogImageUrl is not empty
if (ogImageUrl)
imageSrc.value = ogImageUrl
}).catch(() => {})
}
}, { immediate: true })
</script> </script>
<template> <template>
@ -48,7 +31,7 @@ watch(cardImage, (image) => {
target="_blank" target="_blank"
> >
<div <div
v-if="imageSrc" v-if="cardImage"
flex flex-col flex flex-col
display-block of-hidden display-block of-hidden
border="base" border="base"
@ -60,7 +43,8 @@ watch(cardImage, (image) => {
> >
<CommonBlurhash <CommonBlurhash
:blurhash="card.blurhash" :blurhash="card.blurhash"
:src="imageSrc" :src="cardImage"
:srcset="imageSrcset"
:width="card.width" :width="card.width"
:height="card.height" :height="card.height"
:alt="alt" :alt="alt"

View file

@ -66,7 +66,10 @@ export default defineEventHandler(async (event) => {
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).catch(() =>
// Try another fallback
null,
)
ogImageUrl = response?.openGraph?.image?.url || response?.hybridGraph?.image || '' ogImageUrl = response?.openGraph?.image?.url || response?.hybridGraph?.image || ''
} }
@ -75,7 +78,12 @@ export default defineEventHandler(async (event) => {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log(JSON.stringify({ cardUrl, ogImageUrl })) console.log(JSON.stringify({ cardUrl, ogImageUrl }))
await send(event, ogImageUrl) if (!ogImageUrl) {
// If nothing helped, set cardUrl as default
ogImageUrl = cardUrl
}
await sendRedirect(event, ogImageUrl)
} }
catch (error) { catch (error) {
throw createError({ throw createError({