Skip to content

Commit 9782b80

Browse files
author
Peter Bengtsson
authored
refactor redirecting specifically for the old query string key (#23749)
* refactor redirecting specifically for the old query string key * fix tests
1 parent 1037a3d commit 9782b80

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

middleware/redirects/handle-redirects.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@ export default function handleRedirects(req, res, next) {
3535
let redirect = req.path
3636
let queryParams = req._parsedUrl.query
3737

38+
// update old-style query params (#9467)
39+
if ('q' in req.query) {
40+
const newQueryParams = new URLSearchParams(queryParams)
41+
newQueryParams.set('query', newQueryParams.get('q'))
42+
newQueryParams.delete('q')
43+
return res.redirect(301, `${req.path}?${newQueryParams.toString()}`)
44+
}
45+
3846
// have to do this now because searchPath replacement changes the path as well as the query params
3947
if (queryParams) {
40-
// update old-style query params (#9467)
41-
if ('q' in req.query) {
42-
queryParams = queryParams.replace('q=', 'query=')
43-
}
4448
queryParams = '?' + queryParams
4549
redirect = (redirect + queryParams).replace(patterns.searchPath, '$1')
4650
}

tests/routing/redirects.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('redirects', () => {
7676
test('have q= converted to query=', async () => {
7777
const res = await get('/en/enterprise/admin?q=pulls')
7878
expect(res.statusCode).toBe(301)
79-
const expected = `/en/enterprise-server@${enterpriseServerReleases.latest}/admin?query=pulls`
79+
const expected = '/en/enterprise/admin?query=pulls'
8080
expect(res.headers.location).toBe(expected)
8181
})
8282

@@ -104,7 +104,8 @@ describe('redirects', () => {
104104

105105
test('work on deprecated versions', async () => {
106106
const res = await get('/enterprise/2.12/admin/search?utf8=%E2%9C%93&q=pulls')
107-
expect(res.statusCode).toBe(200)
107+
expect(res.statusCode).toBe(301)
108+
expect(res.headers.location).toBe('/enterprise/2.12/admin/search?utf8=%E2%9C%93&query=pulls')
108109
})
109110
})
110111

0 commit comments

Comments
 (0)