MCPcopy
hub / github.com/github/docs / handleRedirects

Function handleRedirects

middleware/redirects/handle-redirects.js:8–161  ·  view source on GitHub ↗
(req, res, next)

Source from the content-addressed store, hash-verified

6import { defaultCacheControl, languageCacheControl } from '../cache-control.js'
7
8export default function handleRedirects(req, res, next) {
9 // never redirect assets
10 if (patterns.assetPaths.test(req.path)) return next()
11
12 // Any double-slashes in the URL should be removed first
13 if (req.path.includes('//')) {
14 return res.redirect(301, req.path.replace(/\/\//g, '/'))
15 }
16
17 // blanket redirects for languageless homepage
18 if (req.path === '/') {
19 const language = getLanguage(req)
20 languageCacheControl(res)
21 return res.redirect(302, `/${language}`)
22 }
23
24 // The URL `/search` was the old JSON API. We no longer use it anywhere
25 // and neither does support.github.com any more.
26 // But there could be legacy third-party integrators which we don't know
27 // about.
28 // In the future we might want to re-use this for our dedicated search
29 // result page which is `/$lang/search` but until we're certain all
30 // third-party search apps have noticed, we can't do that. Perhaps
31 // some time in mid to late 2023.
32 if (req.path === '/search') {
33 let url = '/api/search/legacy'
34 if (Object.keys(req.query).length) {
35 url += `?${new URLSearchParams(req.query)}`
36 }
37 // This is a 302 redirect.
38 // Why not a 301? Because permanent redirects tend to get very stuck
39 // in client caches (e.g. browsers) which would make it hard to one
40 // day turn this redirect into a redirect to `/en/search` which is
41 // how all pages work when typed in without a language prefix.
42 return res.redirect(url)
43 }
44
45 // begin redirect handling
46 let redirect = req.path
47 let queryParams = req._parsedUrl.query
48
49 // If process.env.ENABLE_SEARCH_RESULTS_PAGE isn't set, you can't go to the
50 // dedicated search results page.
51 // If that's the case, use the "old redirect" where all it does is
52 // "correcting" the old query string 'q' to 'query'.
53 if (!process.env.ENABLE_SEARCH_RESULTS_PAGE && 'q' in req.query && !('query' in req.query)) {
54 // update old-style query params (#9467)
55 const newQueryParams = new URLSearchParams(queryParams)
56 newQueryParams.set('query', newQueryParams.get('q'))
57 newQueryParams.delete('q')
58 return res.redirect(301, `${req.path}?${newQueryParams.toString()}`)
59 }
60
61 // If process.env.ENABLE_SEARCH_RESULTS_PAGE is set, the dedicated search
62 // result page is ready. If that's the case, we can redirect to
63 // `/$locale/search?query=...` from `/foo/bar?query=...` or from
64 // (the old style) `/foo/bar/?q=...`
65 if (

Callers

nothing calls this directly

Calls 9

languageCacheControlFunction · 0.90
pathLanguagePrefixedFunction · 0.90
defaultCacheControlFunction · 0.90
getLanguageFunction · 0.85
removeQueryParamsFunction · 0.85
isDeprecatedVersionFunction · 0.85
usePermanentRedirectFunction · 0.85
nextFunction · 0.50
getRedirectFunction · 0.50

Tested by

no test coverage detected