Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fallback nuxt image #8252

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/ui/src/components/MediaItem/MediaItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const props = withDefaults(
original: false,
isLewd: false,
isDetail: false,
placeholder: '',
placeholder: './Koda.svg',
disableOperation: undefined,
audioPlayerCover: '',
imageComponent: 'img',
Expand Down
36 changes: 28 additions & 8 deletions libs/ui/src/components/MediaItem/type/ImageMedia.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,32 @@
'is-square image': !original,
'is-detail': isDetail,
}">
<!-- load normal image -->
<TheImage
v-if="status === 'ok'"
:image-component="imageComponent"
:image-component-props="{ sizes }"
:src="src"
:alt="alt"
class="is-block image-media__image no-border-radius"
data-testid="type-image"
@error.once="onError" />
@error.once="() => onError('error-1')" />
<!-- if fail, try to load original url -->
<!-- e.g: NuxtImg component will replace image-worker url to cf-image. there is a case when cf-image return 404 -->
<img
v-if="status === 'error-1'"
:src="src"
:alt="alt"
class="is-block image-media__image no-border-radius"
data-testid="type-image"
@error.once="() => onError('error-2')" />
<!-- else, load placeholder -->
<img
v-if="status === 'error-2'"
:src="placeholder"
:alt="alt"
class="is-block image-media__image no-border-radius"
data-testid="type-image" />
</figure>
</template>

Expand All @@ -34,16 +52,18 @@ const props = withDefaults(
}>(),
{
sizes: '450px md:350px lg:270px',
imageComponent: 'img',
src: '',
alt: '',
},
)

const onError = (e: Event) => {
const target = e.target as HTMLImageElement
if (target) {
consola.log('[KODADOT::IMAGE] unable to load', props.src, e)
target.removeAttribute('srcset')
target.src = props.placeholder
}
type Status = 'ok' | 'error-1' | 'error-2'
const status = ref<Status>('ok')

const onError = async (phase: Status) => {
consola.log('[KODADOT::IMAGE] unable to load:', `${phase}:`, props.src)
status.value = phase
}
</script>

Expand Down
16 changes: 5 additions & 11 deletions libs/ui/src/components/NeoAvatar/NeoAvatar.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<template>
<TheImage
<ImageMedia
v-if="avatar"
:placeholder="placeholder"
:image-component="imageComponent"
:src="avatar"
:alt="name"
class="border neo-avatar"
:width="size"
:height="size"
:style="customStyle"
@error.once="onError" />
:original="true" />
<img
v-else
:src="placeholder"
:alt="name"
:width="size"
:height="size"
:style="customStyle"
Expand All @@ -20,7 +22,7 @@

<script setup lang="ts">
import type { ImageComponent } from '../TheImage/TheImage.vue'
import TheImage from '../TheImage/TheImage.vue'
import ImageMedia from '../MediaItem/type/ImageMedia.vue'

const props = defineProps<{
imageComponent?: ImageComponent
Expand All @@ -34,14 +36,6 @@ const customStyle = computed(() => ({
width: `${props.size}px`,
height: `${props.size}px`,
}))

const onError = (e: Event) => {
const target = e.target as HTMLImageElement
if (target) {
target.removeAttribute('srcset')
target.src = props.placeholder
}
}
</script>

<style lang="scss">
Expand Down