Skip to content

Commit c42823a

Browse files
author
Peter Bengtsson
authored
use 302 when redirecting from enterprise-server@latest to enterprise-server@<version> (github#24494)
* use 302 when redirecting from enterprise-server@latest to enterprise-server@<version> * correct tests
1 parent bc73bde commit c42823a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

middleware/redirects/handle-redirects.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,27 @@ export default function handleRedirects(req, res, next) {
8383
cacheControl(res)
8484
}
8585

86+
const permanent = usePermanentRedirect(req)
87+
return res.redirect(permanent ? 301 : 302, redirect)
88+
}
89+
90+
function usePermanentRedirect(req) {
91+
// If the redirect was to essentially swap `enterprise-server@latest`
92+
// for `[email protected]` then, we definitely don't want to
93+
// do a permanent redirect.
94+
// When this is the case, we don't want a permanent redirect because
95+
// it could overzealously cache in the users' browser which could
96+
// be bad when whatever "latest" means changes.
97+
if (req.path.includes('/enterprise-server@latest')) return false
98+
8699
// If the redirect involved injecting a language prefix, then don't
87100
// permanently redirect because that could overly cache in users'
88101
// browsers if we some day want to make the language redirect
89102
// depend on a cookie or 'Accept-Language' header.
90-
return res.redirect(pathLanguagePrefixed(req.path) ? 301 : 302, redirect)
103+
if (pathLanguagePrefixed(req.path)) return true
104+
105+
// The default is to *not* do a permanent redirect.
106+
return false
91107
}
92108

93109
function removeQueryParams(redirect) {

tests/routing/redirects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ describe('redirects', () => {
201201

202202
test('hardcoded @latest redirects to latest version', async () => {
203203
const res = await get('/en/enterprise-server@latest')
204-
expect(res.statusCode).toBe(301)
204+
expect(res.statusCode).toBe(302)
205205
expect(res.headers.location).toBe(enterpriseHome)
206206
})
207207
})

0 commit comments

Comments
 (0)