Skip to content

Commit 74103cd

Browse files
author
Peter Bengtsson
authored
redirect /search to /api/search/legacy (github#31566)
1 parent d4ccf09 commit 74103cd

File tree

5 files changed

+35
-183
lines changed

5 files changed

+35
-183
lines changed

middleware/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import findPage from './find-page.js'
2626
import blockRobots from './block-robots.js'
2727
import archivedEnterpriseVersionsAssets from './archived-enterprise-versions-assets.js'
2828
import api from './api/index.js'
29-
import search from './search.js'
3029
import healthz from './healthz.js'
3130
import anchorRedirect from './anchor-redirect.js'
3231
import remoteIP from './remote-ip.js'
@@ -231,7 +230,6 @@ export default function (app) {
231230

232231
// *** Rendering, 2xx responses ***
233232
app.use('/api', instrument(api, './api'))
234-
app.use('/search', instrument(search, './search')) // The old search API
235233
app.use('/healthz', instrument(healthz, './healthz'))
236234
app.use('/anchor-redirect', instrument(anchorRedirect, './anchor-redirect'))
237235
app.get('/_ip', instrument(remoteIP, './remoteIP'))

middleware/redirects/handle-redirects.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,26 @@ export default function handleRedirects(req, res, next) {
2424
return res.redirect(302, `/${language}`)
2525
}
2626

27-
// Don't try to redirect if the URL is `/search` which is the XHR
28-
// endpoint. It should not become `/en/search`.
29-
// It's unfortunate and looks a bit needlessly complicated. But
30-
// it comes from the legacy that the JSON API endpoint was and needs to
31-
// continue to be `/search` when it would have been more neat if it
32-
// was something like `/api/search`.
33-
// If someone types in `/search?query=foo` manually, they'll get JSON.
34-
// Maybe sometime in 2023 we remove `/search` as an endpoint for the
35-
// JSON.
36-
if (req.path === '/search') return next()
27+
// The URL `/search` was the old JSON API. We no longer use it anywhere
28+
// and neither does support.github.com any more.
29+
// But there could be legacy third-party integrators which we don't know
30+
// about.
31+
// In the future we might want to re-use this for our dedicated search
32+
// result page which is `/$lang/search` but until we're certain all
33+
// third-party search apps have noticed, we can't do that. Perhaps
34+
// some time in mid to late 2023.
35+
if (req.path === '/search') {
36+
let url = '/api/search/legacy'
37+
if (Object.keys(req.query).length) {
38+
url += `?${new URLSearchParams(req.query)}`
39+
}
40+
// This is a 302 redirect.
41+
// Why not a 301? Because permanent redirects tend to get very stuck
42+
// in client caches (e.g. browsers) which would make it hard to one
43+
// day turn this redirect into a redirect to `/en/search` which is
44+
// how all pages work when typed in without a language prefix.
45+
return res.redirect(url)
46+
}
3747

3848
// begin redirect handling
3949
let redirect = req.path

middleware/search.js

Lines changed: 0 additions & 57 deletions
This file was deleted.

tests/content/search.js

Lines changed: 0 additions & 114 deletions
This file was deleted.

tests/routing/redirects.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,4 +486,19 @@ describe('redirects', () => {
486486
expect(res.headers.location).toBe(`/en`)
487487
})
488488
})
489+
490+
describe('redirects from old Lunr search to ES legacy search', () => {
491+
test('redirects even without query string', async () => {
492+
const res = await get(`/search`, { followRedirects: false })
493+
expect(res.statusCode).toBe(302)
494+
expect(res.headers.location).toBe(`/api/search/legacy`)
495+
})
496+
497+
test('redirects with query string', async () => {
498+
const params = new URLSearchParams({ foo: 'bar' })
499+
const res = await get(`/search?${params}`, { followRedirects: false })
500+
expect(res.statusCode).toBe(302)
501+
expect(res.headers.location).toBe(`/api/search/legacy?${params}`)
502+
})
503+
})
489504
})

0 commit comments

Comments
 (0)