Skip to content

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

Use the `route.meta` parameter to pass the path to search page (#1445)

Olga Bulat59ef5cce27c9
diff --git a/src/composables/use-match-routes.ts b/src/composables/use-match-routes.ts
index a661da37e..514af9813 100644
--- a/src/composables/use-match-routes.ts
+++ b/src/composables/use-match-routes.ts
@@ -20,14 +20,17 @@ export const useMatchRoute = (
const { app } = useContext()
const route = useRoute()
const router = useRouter()
+
const localizedRoutes = routes.map(
(route) => app.localeRoute({ name: route })?.name
)
const matches = ref(localizedRoutes.includes(route.value.name))
+
router.beforeEach((to, _from, next) => {
matches.value = localizedRoutes.includes(to.name)
next()
})
+
return { matches }
}
diff --git a/src/locales/po-files/openverse.pot b/src/locales/po-files/openverse.pot
index b37af404f..f59faf384 100644
--- a/src/locales/po-files/openverse.pot
+++ b/src/locales/po-files/openverse.pot
@@ -4,7 +4,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Openverse \n"
"Report-Msgid-Bugs-To: https://github.com/wordpress/openverse/issues \n"
-"POT-Creation-Date: 2022-05-12T06:11:29+00:00\n"
+"POT-Creation-Date: 2022-05-16T17:11:33+00:00\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
@@ -1140,13 +1140,13 @@ msgid "An error occurred"
msgstr ""
#. Do not translate words between ### ###.
-#: src/pages/image/_id.vue:197
+#: src/pages/image/_id.vue:195
msgctxt "error.image-not-found"
msgid "Couldn't find image with id ###id###"
msgstr ""
#. Do not translate words between ### ###.
-#: src/pages/audio/_id.vue:84
+#: src/pages/audio/_id.vue:75
msgctxt "error.media-not-found"
msgid "Couldn't find ###mediaType### with id ###id###"
msgstr ""
diff --git a/src/pages/audio/_id.vue b/src/pages/audio/_id.vue
index e6261cdbb..6e4c7c7bb 100644
--- a/src/pages/audio/_id.vue
+++ b/src/pages/audio/_id.vue
@@ -19,7 +19,7 @@
</template>
<script lang="ts">
-import { computed, defineComponent } from '@nuxtjs/composition-api'
+import { computed, defineComponent, useRoute } from '@nuxtjs/composition-api'
import { AUDIO } from '~/constants/media'
import type { AudioDetail } from '~/models/media'
@@ -32,8 +32,6 @@ import VBackToSearchResultsLink from '~/components/VBackToSearchResultsLink.vue'
import VRelatedAudio from '~/components/VAudioDetails/VRelatedAudio.vue'
import VMediaReuse from '~/components/VMediaInfo/VMediaReuse.vue'
-import type { NuxtApp } from '@nuxt/types/app'
-
export default defineComponent({
name: 'AudioDetailPage',
components: {
@@ -43,22 +41,14 @@ export default defineComponent({
VMediaReuse,
VRelatedAudio,
},
- beforeRouteEnter(to, from, nextPage) {
- nextPage((_this) => {
- // `_this` is the component instance that also has nuxt app properties injected
- const localeRoute = (_this as NuxtApp).localeRoute
- if (
- from.name === localeRoute({ path: '/search/' }).name ||
- from.name === localeRoute({ path: '/search/audio' }).name
- ) {
- // I don't know how to type `_this` here
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore
- _this.backToSearchPath = from.fullPath
- }
- })
+ beforeRouteEnter(to, from, next) {
+ if (from.path.includes('/search/')) {
+ to.meta.backToSearchPath = from.fullPath
+ }
+ next()
},
setup() {
+ const route = useRoute()
const singleResultStore = useSingleResultStore()
const relatedMediaStore = useRelatedMediaStore()
@@ -69,8 +59,9 @@ export default defineComponent({
)
const relatedMedia = computed(() => relatedMediaStore.media)
const relatedFetchState = computed(() => relatedMediaStore.fetchState)
+ const backToSearchPath = computed(() => route.value.meta?.backToSearchPath)
- return { audio, relatedMedia, relatedFetchState }
+ return { audio, backToSearchPath, relatedMedia, relatedFetchState }
},
async asyncData({ route, error, app, $pinia }) {
const audioId = route.params.id
@@ -88,11 +79,6 @@ export default defineComponent({
})
}
},
- data() {
- return {
- backToSearchPath: '',
- }
- },
head() {
const title = this.audio.title
return {
diff --git a/src/pages/image/_id.vue b/src/pages/image/_id.vue
index df82bd08d..0dc96cb49 100644
--- a/src/pages/image/_id.vue
+++ b/src/pages/image/_id.vue
@@ -78,7 +78,12 @@
<script lang="ts">
import axios from 'axios'
-import { computed, defineComponent, ref } from '@nuxtjs/composition-api'
+import {
+ computed,
+ defineComponent,
+ ref,
+ useRoute,
+} from '@nuxtjs/composition-api'
import { IMAGE } from '~/constants/media'
import { useSingleResultStore } from '~/stores/media/single-result'
@@ -93,8 +98,6 @@ import VRelatedImages from '~/components/VImageDetails/VRelatedImages.vue'
import VSketchFabViewer from '~/components/VSketchFabViewer.vue'
import VBackToSearchResultsLink from '~/components/VBackToSearchResultsLink.vue'
-import type { NuxtApp } from '@nuxt/types/app'
-
export default defineComponent({
name: 'VImageDetailsPage',
components: {
@@ -106,22 +109,15 @@ export default defineComponent({
VSketchFabViewer,
VBackToSearchResultsLink,
},
- beforeRouteEnter(_to, from, nextPage) {
- nextPage((_this) => {
- // `_this` is the component instance that also has nuxt app properties injected
- const localeRoute = (_this as NuxtApp).localeRoute
- if (
- from.name === localeRoute({ path: '/search/' })?.name ||
- from.name === localeRoute({ path: '/search/image' })?.name
- ) {
- // I don't know how to type `this` here
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore
- _this.backToSearchPath = from.fullPath
- }
- })
+ beforeRouteEnter(to, from, next) {
+ if (from.path.includes('/search/')) {
+ to.meta.backToSearchPath = from.fullPath
+ }
+ next()
},
setup() {
+ const route = useRoute()
+
const singleResultStore = useSingleResultStore()
const relatedMediaStore = useRelatedMediaStore()
const image = computed(() =>
@@ -130,6 +126,7 @@ export default defineComponent({
: null
)
+ const backToSearchPath = computed(() => route.value.meta?.backToSearchPath)
const relatedMedia = computed(() => relatedMediaStore.media)
const relatedFetchState = computed(() => relatedMediaStore.fetchState)
@@ -165,7 +162,7 @@ export default defineComponent({
})
.catch(() => {
/**
- * Do nothing. This avoid the console warning "Uncaught (in promise) Error:
+ * Do nothing. This avoids the console warning "Uncaught (in promise) Error:
* Network Error" in Firefox in development mode.
*/
})
@@ -185,6 +182,7 @@ export default defineComponent({
sketchFabfailure,
sketchFabUid,
onImageLoaded,
+ backToSearchPath,
}
},
async asyncData({ app, error, route, $pinia }) {
@@ -204,9 +202,6 @@ export default defineComponent({
})
}
},
- data: () => ({
- backToSearchPath: '',
- }),
head() {
const title = `${this.image.title} | Openverse`
diff --git a/typings/vue-router/index.d.ts b/typings/vue-router/index.d.ts
new file mode 100644
index 000000000..03b1e8791
--- /dev/null
+++ b/typings/vue-router/index.d.ts
@@ -0,0 +1,7 @@
+import 'vue-router'
+
+declare module 'vue-router' {
+ export interface RouteMeta {
+ backToSearchPath?: string
+ }
+}