(req, res, next)
| 89 | // by routing them to static content in help-docs-archived-enterprise-versions |
| 90 | |
| 91 | export default async function archivedEnterpriseVersions(req, res, next) { |
| 92 | const { isArchived, requestedVersion } = isArchivedVersion(req) |
| 93 | if (!isArchived) return next() |
| 94 | |
| 95 | // Skip asset paths |
| 96 | if (patterns.assetPaths.test(req.path)) return next() |
| 97 | |
| 98 | const redirectCode = pathLanguagePrefixed(req.path) ? 301 : 302 |
| 99 | |
| 100 | if (deprecatedWithFunctionalRedirects.includes(requestedVersion)) { |
| 101 | const redirectTo = getRedirect(req.path, req.context) |
| 102 | if (redirectTo) { |
| 103 | if (redirectCode === 302) { |
| 104 | languageCacheControl(res) // call first to get `vary` |
| 105 | } |
| 106 | archivedCacheControl(res) // call second to extend duration |
| 107 | return res.redirect(redirectCode, redirectTo) |
| 108 | } |
| 109 | |
| 110 | const redirectJson = await getRemoteJSON(getProxyPath('redirects.json', requestedVersion), { |
| 111 | retry: retryConfiguration, |
| 112 | // This is allowed to be different compared to the other requests |
| 113 | // we make because downloading the `redirects.json` once is very |
| 114 | // useful because it caches so well. |
| 115 | // And, as of 2021 that `redirects.json` is 10MB so it's more likely |
| 116 | // to time out. |
| 117 | timeout: { response: 1000 }, |
| 118 | }) |
| 119 | const [language, withoutLanguage] = splitPathByLanguage(req.path, req.context.userLanguage) |
| 120 | const newRedirectTo = redirectJson[withoutLanguage] |
| 121 | if (newRedirectTo) { |
| 122 | if (redirectCode === 302) { |
| 123 | languageCacheControl(res) // call first to get `vary` |
| 124 | } |
| 125 | archivedCacheControl(res) // call second to extend duration |
| 126 | return res.redirect(redirectCode, `/${language}${newRedirectTo}`) |
| 127 | } |
| 128 | } |
| 129 | // redirect language-prefixed URLs like /en/enterprise/2.10 -> /enterprise/2.10 |
| 130 | // (this only applies to versions <2.13) |
| 131 | if ( |
| 132 | req.path.startsWith('/en/') && |
| 133 | versionSatisfiesRange(requestedVersion, `<${firstVersionDeprecatedOnNewSite}`) |
| 134 | ) { |
| 135 | archivedCacheControl(res) |
| 136 | return res.redirect(redirectCode, req.baseUrl + req.path.replace(/^\/en/, '')) |
| 137 | } |
| 138 | |
| 139 | // find redirects for versions between 2.13 and 2.17 |
| 140 | // starting with 2.18, we updated the archival script to create a redirects.json file |
| 141 | if ( |
| 142 | versionSatisfiesRange(requestedVersion, `>=${firstVersionDeprecatedOnNewSite}`) && |
| 143 | versionSatisfiesRange(requestedVersion, `<=${lastVersionWithoutArchivedRedirectsFile}`) |
| 144 | ) { |
| 145 | const [language, withoutLanguagePath] = splitByLanguage(req.path) |
| 146 | |
| 147 | // `archivedRedirects` is a callable because it's a lazy function |
| 148 | // and memoized so calling it is cheap. |
nothing calls this directly
no test coverage detected