Skip to content

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

Redirect search pages to home when no search query is present (#1474)

Zack Kridabca2e0f29c0a
diff --git a/src/pages/search.vue b/src/pages/search.vue
index b036c9fb3..9f8baf10f 100644
--- a/src/pages/search.vue
+++ b/src/pages/search.vue
@@ -54,6 +54,15 @@ export default defineComponent({
VSearchGrid,
VSkipToContentContainer,
},
+ middleware({ route, redirect }) {
+ /**
+ * This anonymous middleware redirects any search without a query to the homepage.
+ * This is meant to block direct access to /search and all sub-routes, with
+ * an exception for the 'creator' filter. The creator filter doesn't send
+ * the search query to the API.
+ */
+ if (!route.query.q && !route.query.searchBy) return redirect('/')
+ },
scrollToTop: false,
setup() {
const searchGridRef = ref(null)
diff --git a/test/playwright/e2e/redirect-queryless-searches.spec.ts b/test/playwright/e2e/redirect-queryless-searches.spec.ts
new file mode 100644
index 000000000..1dea4f9f9
--- /dev/null
+++ b/test/playwright/e2e/redirect-queryless-searches.spec.ts
@@ -0,0 +1,21 @@
+/**
+ * Searches without a search term should
+ * redirect to the homepage.
+ */
+
+import { test, expect } from '@playwright/test'
+
+import { searchTypes, searchPath } from '~/constants/media'
+
+test.describe.configure({ mode: 'parallel' })
+
+searchTypes.forEach((type) => {
+ test(`Queryless ${type} search redirects to homepage`, async ({ page }) => {
+ const response = await page.goto(searchPath(type))
+ expect(response?.request().redirectedFrom()?.url()).toBeDefined()
+ })
+ test(`${type} search with query doesn't redirect`, async ({ page }) => {
+ const response = await page.goto(searchPath(type) + '?q=dog')
+ expect(response?.request().redirectedFrom()?.url()).toBeUndefined()
+ })
+})
diff --git a/test/playwright/e2e/translation-banner.spec.ts b/test/playwright/e2e/translation-banner.spec.ts
index 710b2d598..aeb1cc429 100644
--- a/test/playwright/e2e/translation-banner.spec.ts
+++ b/test/playwright/e2e/translation-banner.spec.ts
@@ -1,10 +1,12 @@
import { test, expect } from '@playwright/test'
+const russianSearchPath = '/ru/search?q=dog'
+
test.describe('translation banner', () => {
test('Can see the translation banner and go to the correct link', async ({
page,
}) => {
- await page.goto('/ru/search/')
+ await page.goto(russianSearchPath)
await expect(
page.locator(
'text=The translation for Russian locale is incomplete. Help us get to 100 percent by'
@@ -21,7 +23,7 @@ test.describe('translation banner', () => {
})
test('Can close the translation banner', async ({ page }) => {
- await page.goto('/ru/search/')
+ await page.goto(russianSearchPath)
await page.click(
'[data-testid="banner-translation-ru"] [aria-label="Закрыть"]:visible',
{ timeout: 500 }
@@ -34,7 +36,7 @@ test.describe('translation banner', () => {
await page.click('a[role="menuitemcheckbox"]:has-text("Наша история")')
await expect(banner).not.toBeVisible({ timeout: 500 })
- await page.goto('/ru/search/')
+ await page.goto(russianSearchPath)
await expect(banner).not.toBeVisible({ timeout: 500 })
})
})