diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b46f29bbda..0d6db808d3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,6 +23,9 @@ env: # Setting this will activate the jest tests that depend on actually # sending real search queries to Elasticsearch ELASTICSEARCH_URL: http://localhost:9200/ + # Hopefully the name is clear enough. By enabling this, we're testing + # the future code. + ENABLE_SEARCH_RESULTS_PAGE: true jobs: test: diff --git a/middleware/redirects/handle-redirects.js b/middleware/redirects/handle-redirects.js index 3395554e6a..48eea18472 100644 --- a/middleware/redirects/handle-redirects.js +++ b/middleware/redirects/handle-redirects.js @@ -39,11 +39,11 @@ export default function handleRedirects(req, res, next) { let redirect = req.path let queryParams = req._parsedUrl.query - // If process.env.ELASTICSEARCH_URL isn't set, you can't go to the + // If process.env.ENABLE_SEARCH_RESULTS_PAGE isn't set, you can't go to the // dedicated search results page. // If that's the case, use the "old redirect" where all it does is // "correcting" the old query string 'q' to 'query'. - if (!process.env.ELASTICSEARCH_URL && 'q' in req.query && !('query' in req.query)) { + if (!process.env.ENABLE_SEARCH_RESULTS_PAGE && 'q' in req.query && !('query' in req.query)) { // update old-style query params (#9467) const newQueryParams = new URLSearchParams(queryParams) newQueryParams.set('query', newQueryParams.get('q')) @@ -51,19 +51,19 @@ export default function handleRedirects(req, res, next) { return res.redirect(301, `${req.path}?${newQueryParams.toString()}`) } - // If process.env.ELASTICSEARCH_URL is set, the dedicated search + // If process.env.ENABLE_SEARCH_RESULTS_PAGE is set, the dedicated search // result page is ready. If that's the case, we can redirect to // `/$locale/search?query=...` from `/foo/bar?query=...` or from // (the old style) `/foo/bar/?q=...` if ( - process.env.ELASTICSEARCH_URL && + process.env.ENABLE_SEARCH_RESULTS_PAGE && ('q' in req.query || ('query' in req.query && !(req.path.endsWith('/search') || req.path.startsWith('/api/search')))) ) { // If you had the old legacy format of /some/uri?q=stuff // it needs to redirect to /en/search?query=stuff or - // /some/uri?query=stuff depending on if ELASTICSEARCH_URL has been + // /some/uri?query=stuff depending on if ENABLE_SEARCH_RESULTS_PAGE has been // set up. // If you have the new format of /some/uri?query=stuff it too needs // to redirect to /en/search?query=stuff diff --git a/pages/search.tsx b/pages/search.tsx index fa6281d615..67703ae44b 100644 --- a/pages/search.tsx +++ b/pages/search.tsx @@ -26,7 +26,10 @@ export const getServerSideProps: GetServerSideProps = async (context) => // So if that's the case, which might be true in production (Aug 2022) // or on an engineers local development, we basically pretend the // page doesn't exist. - if (!process.env.ELASTICSEARCH_URL) { + // By depending conditionally on these two environment variables we're + // able to carefully launch the dedicated search results page + // separately from the JSON API endpoint. + if (!process.env.ELASTICSEARCH_URL || !process.env.ENABLE_SEARCH_RESULTS_PAGE) { return { notFound: true } } diff --git a/tests/helpers/conditional-runs.js b/tests/helpers/conditional-runs.js index da762a1397..2e1951358b 100644 --- a/tests/helpers/conditional-runs.js +++ b/tests/helpers/conditional-runs.js @@ -4,9 +4,13 @@ const runningActionsOnInternalRepo = export const testViaActionsOnly = runningActionsOnInternalRepo ? test : test.skip export const describeViaActionsOnly = runningActionsOnInternalRepo ? describe : describe.skip export const describeIfElasticsearchURL = process.env.ELASTICSEARCH_URL ? describe : describe.skip +export const describeIfDedicatedSearchResultsPage = process.env.ENABLE_SEARCH_RESULTS_PAGE + ? describe + : describe.skip export default { testViaActionsOnly, describeViaActionsOnly, describeIfElasticsearchURL, + describeIfDedicatedSearchResultsPage, } diff --git a/tests/rendering/search.js b/tests/rendering/search.js index eafe42ee75..c2b4899293 100644 --- a/tests/rendering/search.js +++ b/tests/rendering/search.js @@ -1,8 +1,9 @@ import { expect, jest } from '@jest/globals' import { getDOM } from '../helpers/e2etest.js' +import { describeIfDedicatedSearchResultsPage } from '../helpers/conditional-runs.js' -describe('search results page', () => { +describeIfDedicatedSearchResultsPage('search results page', () => { jest.setTimeout(5 * 60 * 1000) test('says something if no query is provided', async () => { diff --git a/tests/routing/redirects.js b/tests/routing/redirects.js index 47a5e12f60..cd4e4baa9a 100644 --- a/tests/routing/redirects.js +++ b/tests/routing/redirects.js @@ -16,7 +16,9 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)) // dedicated search results page works. // In a near future, we won't be needing this and assume it's always // true. -const USE_DEDICATED_SEARCH_RESULTS_PAGE = Boolean(process.env.ELASTICSEARCH_URL) +const USE_DEDICATED_SEARCH_RESULTS_PAGE = Boolean( + JSON.parse(process.env.ENABLE_SEARCH_RESULTS_PAGE || 'false') +) describe('redirects', () => { jest.setTimeout(5 * 60 * 1000)