Skip to content

Commit ecc5d01

Browse files
authored
Merge pull request #17940 from github/use-redirects-file-instead-of-stubbed-archived-redirects
Use archived redirects.json files instead of stubbed redirect files
2 parents 7ec38c4 + 9521bdc commit ecc5d01

File tree

4 files changed

+42
-37
lines changed

4 files changed

+42
-37
lines changed

lib/enterprise-server-releases.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ const nextDeprecationDate = dates[oldestSupported].deprecationDate
4747
const isOldestReleaseDeprecated = new Date() > new Date(nextDeprecationDate)
4848
const deprecatedOnNewSite = deprecated.filter(version => versionSatisfiesRange(version, '>=2.13'))
4949
const firstVersionDeprecatedOnNewSite = '2.13'
50-
// starting from 2.18, we updated the archival script to create stubbed HTML redirect files
51-
const lastVersionWithoutStubbedRedirectFiles = '2.17'
50+
// starting from 2.18, we updated the archival script to create a redirects.json top-level file in the archived repo
51+
const lastVersionWithoutArchivedRedirectsFile = '2.17'
5252
// last version using paths like /enterprise/<release>/<user>/<product>/<category>/<article>
5353
// instead of /enterprise-server@<release>/<product>/<category>/<article>
5454
const lastReleaseWithLegacyFormat = '2.18'
@@ -68,7 +68,7 @@ module.exports = {
6868
deprecatedOnNewSite,
6969
dates,
7070
firstVersionDeprecatedOnNewSite,
71-
lastVersionWithoutStubbedRedirectFiles,
71+
lastVersionWithoutArchivedRedirectsFile,
7272
lastReleaseWithLegacyFormat,
7373
deprecatedReleasesWithLegacyFormat,
7474
deprecatedReleasesWithNewFormat,

middleware/archived-enterprise-versions.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const path = require('path')
22
const slash = require('slash')
3-
const { firstVersionDeprecatedOnNewSite, lastVersionWithoutStubbedRedirectFiles } = require('../lib/enterprise-server-releases')
3+
const { firstVersionDeprecatedOnNewSite, lastVersionWithoutArchivedRedirectsFile } = require('../lib/enterprise-server-releases')
44
const patterns = require('../lib/patterns')
55
const versionSatisfiesRange = require('../lib/version-satisfies-range')
66
const isArchivedVersion = require('../lib/is-archived-version')
@@ -25,21 +25,42 @@ module.exports = async (req, res, next) => {
2525
}
2626

2727
// find redirects for versions between 2.13 and 2.17
28-
// starting with 2.18, we updated the archival script to create stubbed HTML redirect files
28+
// starting with 2.18, we updated the archival script to create a redirects.json file
2929
if (versionSatisfiesRange(requestedVersion, `>=${firstVersionDeprecatedOnNewSite}`) &&
30-
versionSatisfiesRange(requestedVersion, `<=${lastVersionWithoutStubbedRedirectFiles}`)) {
30+
versionSatisfiesRange(requestedVersion, `<=${lastVersionWithoutArchivedRedirectsFile}`)) {
3131
const redirect = archvivedRedirects[req.path]
3232
if (redirect && redirect !== req.path) {
3333
return res.redirect(301, redirect)
3434
}
3535
}
3636

37+
let reqPath = req.path
38+
let isRedirect = false
39+
if (versionSatisfiesRange(requestedVersion, `>${lastVersionWithoutArchivedRedirectsFile}`)) {
40+
try {
41+
const redirectJson = await got(getProxyPath('redirects.json', requestedVersion))
42+
43+
if (redirectJson[req.path]) {
44+
isRedirect = true
45+
}
46+
reqPath = redirectJson[req.path] || req.path
47+
} catch (err) {
48+
// nooop
49+
}
50+
}
51+
3752
try {
38-
const r = await got(getProxyPath(req.path, requestedVersion))
53+
const r = await got(getProxyPath(reqPath, requestedVersion))
3954
res.set('content-type', r.headers['content-type'])
4055
res.set('x-robots-tag', 'noindex')
4156

42-
// make the stubbed redirect files added in >=2.18 return 301 instead of 200
57+
// make redirects found via redirects.json return 301 instead of 200
58+
if (isRedirect) {
59+
res.status(301)
60+
res.set('location', reqPath)
61+
}
62+
63+
// make stubbed redirect files (which exist in versions <2.13) return 301 instead of 200
4364
const staticRedirect = r.body.match(patterns.staticRedirect)
4465
if (staticRedirect) {
4566
res.status(301)
@@ -73,7 +94,7 @@ function getProxyPath (reqPath, requestedVersion) {
7394
// this workaround finds potentially relevant frontmatter redirects in currently supported pages
7495
function getFallbackRedirects (req, requestedVersion) {
7596
if (versionSatisfiesRange(requestedVersion, `<${firstVersionDeprecatedOnNewSite}`)) return
76-
if (versionSatisfiesRange(requestedVersion, `>${lastVersionWithoutStubbedRedirectFiles}`)) return
97+
if (versionSatisfiesRange(requestedVersion, `>${lastVersionWithoutArchivedRedirectsFile}`)) return
7798

7899
return archivedFrontmatterFallbacks.find(arrayOfFallbacks => arrayOfFallbacks.includes(req.path))
79100
}

script/enterprise-server-deprecations/archive-version.js

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const host = `http://localhost:${port}`
99
const scrape = require('website-scraper')
1010
const program = require('commander')
1111
const rimraf = require('rimraf').sync
12-
const mkdirp = require('mkdirp').sync
1312
const version = require('../../lib/enterprise-server-releases').oldestSupported
1413
const archivalRepoName = 'help-docs-archived-enterprise-versions'
1514
const archivalRepoUrl = `https://github.com/github/${archivalRepoName}`
@@ -173,18 +172,20 @@ async function main () {
173172
console.log(`\n\ndone scraping! added files to ${path.relative(process.cwd(), finalDirectory)}\n`)
174173

175174
// create redirect html files to preserve frontmatter redirects
176-
await createRedirectPages(permalinksPerVersion, pageMap, finalDirectory)
175+
await createRedirectsFile(permalinksPerVersion, pageMap, finalDirectory)
177176

178177
console.log(`next step: deprecate ${version} in lib/enterprise-server-releases.js`)
179178

180179
process.exit()
181180
})
182181
}
183182

184-
async function createRedirectPages (permalinks, pageMap, finalDirectory) {
183+
async function createRedirectsFile (permalinks, pageMap, finalDirectory) {
185184
const pagesPerVersion = permalinks.map(permalink => pageMap[permalink])
186185
const redirects = await loadRedirects(pagesPerVersion, pageMap)
187186

187+
const redirectsPerVersion = {}
188+
188189
Object.entries(redirects).forEach(([oldPath, newPath]) => {
189190
// remove any liquid variables that sneak in
190191
oldPath = oldPath
@@ -193,31 +194,8 @@ async function createRedirectPages (permalinks, pageMap, finalDirectory) {
193194
// ignore any old paths that are not in this version
194195
if (!(oldPath.includes(`/enterprise-server@${version}`) || oldPath.includes(`/enterprise/${version}`))) return
195196

196-
const fullPath = path.join(finalDirectory, oldPath)
197-
const filename = `${fullPath}/index.html`
198-
const html = getRedirectHtml(newPath)
199-
200-
mkdirp(fullPath)
201-
fs.writeFileSync(filename, html)
197+
redirectsPerVersion[oldPath] = newPath
202198
})
203199

204-
console.log('done creating redirect files!\n')
205-
}
206-
207-
// redirect html files already exist in <=2.12 because these versions were deprecated on the old static site
208-
function getRedirectHtml (newPath) {
209-
return `<!DOCTYPE html>
210-
<html>
211-
<head>
212-
<meta charset="utf-8">
213-
<title>Redirecting...</title>
214-
<link rel="canonical" href="${newPath}">
215-
<meta http-equiv="refresh" content="0; url=${newPath}">
216-
</head>
217-
<body>
218-
<h1>Redirecting...</h1>
219-
<a href="${newPath}">Click here if you are not redirected.</a>
220-
<script>location='${newPath}'</script>
221-
</body>
222-
</html>`
200+
fs.writeFileSync(path.posix.join(finalDirectory, 'redirects.json'), JSON.stringify(redirectsPerVersion, null, 2))
223201
}

tests/routing/deprecated-enterprise-versions.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ describe('enterprise deprecation', () => {
3030
expect(res.headers.location).toBe('/en/enterprise/2.15/user/articles/viewing-contributions-on-your-profile')
3131
})
3232

33+
test('can access redirects from redirects.json in deprecated enterprise content >2.17', async () => {
34+
const res = await get('/enterprise/2.19/admin/categories/time')
35+
expect(res.statusCode).toBe(301)
36+
expect(res.headers.location).toBe('/en/[email protected]/admin/configuration/configuring-time-synchronization')
37+
})
38+
3339
test('handles requests for deprecated Enterprise pages ( >=2.13 )', async () => {
3440
expect(enterpriseServerReleases.deprecated.includes('2.13')).toBe(true)
3541
const $ = await getDOM('/en/enterprise/2.13/user/articles/about-branches')

0 commit comments

Comments
 (0)