Skip to content

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

Make `search.vue` media fetching non-blocking (#1522)

Olga Bulat515693f829e5
diff --git a/src/components/VHeader/VHeader.vue b/src/components/VHeader/VHeader.vue
index 078e8b8a2..23347212f 100644
--- a/src/components/VHeader/VHeader.vue
+++ b/src/components/VHeader/VHeader.vue
@@ -166,12 +166,14 @@ export default defineComponent({
* - search term hasn't changed:
* - on a search route, do nothing.
* - on other routes: set searchType to 'All content', reset the media,
- * change the path to `/search/` (All content) and fetch media.
+ * change the path to `/search/` (All content).
* - search term changed:
* - on a search route: Update the store searchTerm value, update query `q` param, reset media,
* fetch new media.
* - on other routes: Update the store searchTerm value, set searchType to 'All content', reset media,
- * update query `q` param, fetch new media.
+ * update query `q` param.
+ * Updating the path causes the `search.vue` page's route watcher
+ * to run and fetch new media.
*/
const handleSearch = async () => {
window.scrollTo({ top: 0, left: 0, behavior: 'auto' })
@@ -198,7 +200,6 @@ export default defineComponent({
query: searchStore.searchQueryParams,
})
router.push(newPath)
- await mediaStore.fetchMedia()
}
}
const areFiltersDisabled = computed(
diff --git a/src/pages/search.vue b/src/pages/search.vue
index 9f8baf10f..1a0d99e92 100644
--- a/src/pages/search.vue
+++ b/src/pages/search.vue
@@ -37,6 +37,8 @@
import { isShallowEqualObjects } from '@wordpress/is-shallow-equal'
import { computed, defineComponent, inject, ref } from '@nuxtjs/composition-api'
+import { Context } from '@nuxt/types'
+
import { isMinScreen } from '~/composables/use-media-query'
import { useFilterSidebarVisibility } from '~/composables/use-filter-sidebar-visibility'
import { Focus, focusIn } from '~/utils/focus-management'
@@ -80,6 +82,14 @@ export default defineComponent({
const fetchState = computed(() => mediaStore.fetchState)
const resultItems = computed(() => mediaStore.resultItems)
+ const needsFetching = computed(() =>
+ Boolean(
+ searchStore.searchTypeIsSupported &&
+ !mediaStore.resultCount &&
+ searchStore.searchTerm.trim() !== ''
+ )
+ )
+
const handleTab = (event: KeyboardEvent, element: string) => {
if (showScrollButton.value && element !== 'scroll-button') {
return
@@ -99,41 +109,44 @@ export default defineComponent({
resultCount,
fetchState,
resultItems,
+ needsFetching,
fetchMedia: mediaStore.fetchMedia,
setSearchStateFromUrl: searchStore.setSearchStateFromUrl,
handleTab,
}
},
+ /**
+ * asyncData blocks the rendering of the page, so we only
+ * update the state from the route here, and do not fetch media.
+ */
async asyncData({ route, $pinia }) {
const searchStore = useSearchStore($pinia)
- const mediaStore = useMediaStore($pinia)
await searchStore.initProviderFilters()
searchStore.setSearchStateFromUrl({
path: route.path,
urlQuery: route.query,
})
- if (
- searchStore.searchTypeIsSupported &&
- !mediaStore.resultCount &&
- searchStore.searchTerm.trim() !== ''
- ) {
- await mediaStore.fetchMedia()
+ },
+ /**
+ * Fetch media, if necessary, in a non-blocking way.
+ */
+ async fetch() {
+ if (this.needsFetching) {
+ await this.fetchMedia()
}
},
watch: {
/**
* Updates the search type only if the route's path changes.
- * @param {import('@nuxt/types').Context['route']} newRoute
- * @param {import('@nuxt/types').Context['route']} oldRoute
*/
- async $route(newRoute, oldRoute) {
+ async $route(newRoute: Context['route'], oldRoute: Context['route']) {
if (
newRoute.path !== oldRoute.path ||
!isShallowEqualObjects(newRoute.query, oldRoute.query)
) {
const { query, path } = newRoute
- await this.setSearchStateFromUrl({ urlQuery: query, path })
- this.fetchMedia()
+ this.setSearchStateFromUrl({ urlQuery: query, path })
+ await this.fetchMedia()
}
},
},