Skip to content

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

Fix collections SSR (#4722)

Olga Bulat6149929b9c08
diff --git a/frontend/src/components/VSearchResultsGrid/VCollectionResults.vue b/frontend/src/components/VSearchResultsGrid/VCollectionResults.vue
index 811618849..32edb6a4e 100644
--- a/frontend/src/components/VSearchResultsGrid/VCollectionResults.vue
+++ b/frontend/src/components/VSearchResultsGrid/VCollectionResults.vue
@@ -12,6 +12,7 @@
class="mb-2 md:mb-3"
:media-type="results.type"
:collection-params="collectionParams"
+ :creator-url="creatorUrl"
/>
</template>
@@ -68,6 +69,9 @@ export default defineComponent({
type: String,
required: true,
},
+ creatorUrl: {
+ type: String,
+ },
},
emits: {
"load-more": defineEvent(),
diff --git a/frontend/src/composables/use-collection.ts b/frontend/src/composables/use-collection.ts
new file mode 100644
index 000000000..068e3d39a
--- /dev/null
+++ b/frontend/src/composables/use-collection.ts
@@ -0,0 +1,77 @@
+import { useCollectionMeta, useI18n, useRoute } from "#imports"
+
+import { computed, ref, watch } from "vue"
+
+import { SupportedMediaType } from "~/constants/media"
+import { useMediaStore } from "~/stores/media"
+import { useSearchStore } from "~/stores/search"
+import type { AudioDetail, ImageDetail } from "~/types/media"
+
+export const useCollection = <T extends SupportedMediaType>({
+ mediaType,
+}: {
+ mediaType: T
+}) => {
+ type ResultType = T extends "image" ? ImageDetail : AudioDetail
+
+ const mediaStore = useMediaStore()
+ const searchStore = useSearchStore()
+
+ const collectionParams = computed(() => searchStore.collectionParams)
+ const isFetching = computed(() => mediaStore.fetchState.isFetching)
+
+ const media = ref<ResultType[]>(
+ mediaStore.resultItems[mediaType] as ResultType[]
+ )
+ const creatorUrl = ref<string>()
+
+ const i18n = useI18n({ useScope: "global" })
+
+ const collectionLabel = computed(() => {
+ if (!collectionParams.value) {
+ return ""
+ }
+ const { collection, ...params } = collectionParams.value
+ return i18n.t(`collection.ariaLabel.${collection}.image`, { ...params })
+ })
+
+ const fetchMedia = async (
+ { shouldPersistMedia }: { shouldPersistMedia: boolean } = {
+ shouldPersistMedia: false,
+ }
+ ) => {
+ media.value = (await mediaStore.fetchMedia({
+ shouldPersistMedia,
+ })) as ResultType[]
+ creatorUrl.value =
+ media.value.length > 0 ? media.value[0].creator_url : undefined
+ return media.value
+ }
+ // `useAsyncData` is not triggered when the query changes, e.g. when the user navigates from
+ // a creator collection page to a source collection page.
+ const route = useRoute()
+ const routeQuery = computed(() => route.query)
+ watch(routeQuery, async () => {
+ await fetchMedia()
+ })
+
+ const loadMore = async () => {
+ await fetchMedia({ shouldPersistMedia: true })
+ }
+ const { pageTitle } = useCollectionMeta({
+ collectionParams,
+ mediaType,
+ i18n,
+ })
+
+ return {
+ collectionParams,
+ pageTitle,
+ collectionLabel,
+ creatorUrl,
+ isFetching,
+ media,
+ fetchMedia,
+ loadMore,
+ }
+}
diff --git a/frontend/src/pages/audio/collection.vue b/frontend/src/pages/audio/collection.vue
index 2c789dd6a..92016df66 100644
--- a/frontend/src/pages/audio/collection.vue
+++ b/frontend/src/pages/audio/collection.vue
@@ -1,22 +1,13 @@
<script setup lang="ts">
-import {
- definePageMeta,
- useAsyncData,
- useHead,
- useI18n,
- useRoute,
-} from "#imports"
-
-import { computed, ref, watch } from "vue"
+import { definePageMeta, useAsyncData, useHead } from "#imports"
import { collectionMiddleware } from "~/middleware/collection"
-import { useSearchStore } from "~/stores/search"
-import { useMediaStore } from "~/stores/media"
-import { useCollectionMeta } from "~/composables/use-collection-meta"
-import type { AudioDetail } from "~/types/media"
import { skipToContentTargetId } from "~/constants/window"
+import { useCollection } from "~/composables/use-collection"
+import { AUDIO } from "~/constants/media"
+
import VCollectionResults from "~/components/VSearchResultsGrid/VCollectionResults.vue"
defineOptions({
@@ -28,59 +19,21 @@ definePageMeta({
middleware: collectionMiddleware,
})
-const mediaStore = useMediaStore()
-const searchStore = useSearchStore()
-
-const collectionParams = computed(() => searchStore.collectionParams)
-const isFetching = computed(() => mediaStore.fetchState.isFetching)
-
-const media = ref<AudioDetail[]>([])
-const creatorUrl = ref<string>()
-
-const i18n = useI18n({ useScope: "global" })
-
-const collectionLabel = computed(() => {
- if (!collectionParams.value) {
- return ""
- }
- const { collection, ...params } = collectionParams.value
- return i18n.t(`collection.ariaLabel.${collection}.audio`, { ...params })
-})
-
-const fetchMedia = async (
- { shouldPersistMedia }: { shouldPersistMedia: boolean } = {
- shouldPersistMedia: false,
- }
-) => {
- media.value = (await mediaStore.fetchMedia({
- shouldPersistMedia,
- })) as AudioDetail[]
- creatorUrl.value =
- media.value.length > 0 ? media.value[0].creator_url : undefined
- return media.value
-}
-const loadMore = () => {
- fetchMedia({ shouldPersistMedia: true })
-}
-
-const { pageTitle } = useCollectionMeta({
+const {
collectionParams,
- mediaType: "audio",
- i18n,
-})
+ isFetching,
+ media,
+ creatorUrl,
+ collectionLabel,
+ fetchMedia,
+ loadMore,
+ pageTitle,
+} = useCollection({ mediaType: AUDIO })
useHead({
meta: [{ hid: "og:title", property: "og:title", content: pageTitle.value }],
title: pageTitle.value,
})
-
-// `useAsyncData` is not triggered when the query changes, e.g. when the user navigates from
-// a creator collection page to a source collection page.
-const route = useRoute()
-
-watch(route, async () => {
- await fetchMedia()
-})
/**
* Media is not empty when we navigate back to this page, so we don't need to fetch
* it again to make sure that all the previously fetched media is displayed.
@@ -105,6 +58,7 @@ await useAsyncData(
:results="{ type: 'audio', items: media }"
:collection-label="collectionLabel"
:collection-params="collectionParams"
+ :creator-url="creatorUrl"
@load-more="loadMore"
/>
</div>
diff --git a/frontend/src/pages/image/collection.vue b/frontend/src/pages/image/collection.vue
index 491d30bdb..095bc3cf4 100644
--- a/frontend/src/pages/image/collection.vue
+++ b/frontend/src/pages/image/collection.vue
@@ -1,20 +1,11 @@
<script setup lang="ts">
-import {
- definePageMeta,
- useAsyncData,
- useHead,
- useI18n,
- useRoute,
-} from "#imports"
-
-import { computed, ref, watch } from "vue"
+import { definePageMeta, useAsyncData, useHead } from "#imports"
import { collectionMiddleware } from "~/middleware/collection"
-import { useMediaStore } from "~/stores/media"
-import { useSearchStore } from "~/stores/search"
-import { useCollectionMeta } from "~/composables/use-collection-meta"
import { skipToContentTargetId } from "~/constants/window"
-import type { ImageDetail } from "~/types/media"
+
+import { useCollection } from "~/composables/use-collection"
+import { IMAGE } from "~/constants/media"
import VCollectionResults from "~/components/VSearchResultsGrid/VCollectionResults.vue"
@@ -27,60 +18,22 @@ definePageMeta({
middleware: collectionMiddleware,
})
-const mediaStore = useMediaStore()
-const searchStore = useSearchStore()
-
-const collectionParams = computed(() => searchStore.collectionParams)
-const isFetching = computed(() => mediaStore.fetchState.isFetching)
-
-const media = ref<ImageDetail[]>(mediaStore.resultItems.image)
-const creatorUrl = ref<string>()
-
-const i18n = useI18n({ useScope: "global" })
-
-const collectionLabel = computed(() => {
- if (!collectionParams.value) {
- return ""
- }
- const { collection, ...params } = collectionParams.value
- return i18n.t(`collection.ariaLabel.${collection}.image`, { ...params })
-})
-
-const fetchMedia = async (
- { shouldPersistMedia }: { shouldPersistMedia: boolean } = {
- shouldPersistMedia: false,
- }
-) => {
- media.value = (await mediaStore.fetchMedia({
- shouldPersistMedia,
- })) as ImageDetail[]
- creatorUrl.value =
- media.value.length > 0 ? media.value[0].creator_url : undefined
- return media.value
-}
-const loadMore = () => {
- fetchMedia({ shouldPersistMedia: true })
-}
-
-const { pageTitle } = useCollectionMeta({
+const {
collectionParams,
- mediaType: "image",
- i18n,
-})
+ isFetching,
+ media,
+ creatorUrl,
+ collectionLabel,
+ fetchMedia,
+ loadMore,
+ pageTitle,
+} = useCollection({ mediaType: IMAGE })
useHead(() => ({
meta: [{ hid: "og:title", property: "og:title", content: pageTitle.value }],
title: pageTitle.value,
}))
-// `useAsyncData` is not triggered when the query changes, e.g. when the user navigates from
-// a creator collection page to a source collection page.
-const route = useRoute()
-const routeQuery = computed(() => route.query)
-watch(routeQuery, async () => {
- await fetchMedia()
-})
-
/**
* Media is not empty when we navigate back to this page, so we don't need
* to fetch it again to make sure that all the previously fetched media is displayed.
@@ -105,6 +58,7 @@ await useAsyncData(
:results="{ type: 'image', items: media }"
:collection-label="collectionLabel"
:collection-params="collectionParams"
+ :creator-url="creatorUrl"
@load-more="loadMore"
/>
</div>