Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Make fetching related images on the client non-blocking (#1524)

Olga Bulat05b2f40cc438
diff --git a/src/components/VImageDetails/VRelatedImages.vue b/src/components/VImageDetails/VRelatedImages.vue
index d500675ff..7bdb302a9 100644
--- a/src/components/VImageDetails/VRelatedImages.vue
+++ b/src/components/VImageDetails/VRelatedImages.vue
@@ -3,6 +3,7 @@
<h3 class="text-2xl md:text-3xl mb-6">
{{ $t('image-details.related-images') }}
</h3>
+ <VLoadingIcon v-if="fetchState.isFetching" />
<VImageGrid
:images="media"
:show-load-more="false"
@@ -11,21 +12,27 @@
</aside>
</template>
-<script>
+<script lang="ts">
+import { defineComponent, PropType } from '@nuxtjs/composition-api'
+
+import type { ImageDetail } from '~/models/media'
+import type { FetchState } from '~/composables/use-fetch-state'
+
import VImageGrid from '~/components/VImageGrid/VImageGrid.vue'
+import VLoadingIcon from '~/components/LoadingIcon.vue'
-export default {
+export default defineComponent({
name: 'VRelatedImages',
- components: { VImageGrid },
+ components: { VImageGrid, VLoadingIcon },
props: {
media: {
- type: Array,
+ type: Array as PropType<ImageDetail[]>,
required: true,
},
fetchState: {
- type: Object,
+ type: Object as PropType<FetchState>,
required: true,
},
},
-}
+})
</script>
diff --git a/src/stores/media/related-media.ts b/src/stores/media/related-media.ts
index 970a8265d..35c2f5a65 100644
--- a/src/stores/media/related-media.ts
+++ b/src/stores/media/related-media.ts
@@ -45,6 +45,7 @@ export const useRelatedMediaStore = defineStore('related-media', {
'end',
`Could not fetch related ${mediaType} for id ${id}`
)
+ throw new Error(`Could not fetch related ${mediaType} for id ${id}`)
}
},
},
diff --git a/src/stores/media/single-result.ts b/src/stores/media/single-result.ts
index 1ade3cff8..669a6489e 100644
--- a/src/stores/media/single-result.ts
+++ b/src/stores/media/single-result.ts
@@ -77,7 +77,23 @@ export const useSingleResultStore = defineStore('single-result', {
} else {
await this.fetchMediaItem(type, id)
}
- await useRelatedMediaStore().fetchMedia(type, id)
+ /**
+ * On the server, we await the related media to make sure it's rendered on the page.
+ * On the client, we don't await it to render the whole page while the related media are loading.
+ */
+ if (process.server) {
+ try {
+ await useRelatedMediaStore().fetchMedia(type, id)
+ } catch (error) {
+ console.warn('Could not load related media: ', error)
+ }
+ } else {
+ useRelatedMediaStore()
+ .fetchMedia(type, id)
+ .catch((error) =>
+ console.warn('Could not load related media: ', error)
+ )
+ }
},
/**
* Fetches a media item from the API.