зеркало из https://github.com/github/docs.git
Merge pull request #17940 from github/use-redirects-file-instead-of-stubbed-archived-redirects
Use archived redirects.json files instead of stubbed redirect files
This commit is contained in:
Коммит
ecc5d0154c
|
@ -47,8 +47,8 @@ const nextDeprecationDate = dates[oldestSupported].deprecationDate
|
|||
const isOldestReleaseDeprecated = new Date() > new Date(nextDeprecationDate)
|
||||
const deprecatedOnNewSite = deprecated.filter(version => versionSatisfiesRange(version, '>=2.13'))
|
||||
const firstVersionDeprecatedOnNewSite = '2.13'
|
||||
// starting from 2.18, we updated the archival script to create stubbed HTML redirect files
|
||||
const lastVersionWithoutStubbedRedirectFiles = '2.17'
|
||||
// starting from 2.18, we updated the archival script to create a redirects.json top-level file in the archived repo
|
||||
const lastVersionWithoutArchivedRedirectsFile = '2.17'
|
||||
// last version using paths like /enterprise/<release>/<user>/<product>/<category>/<article>
|
||||
// instead of /enterprise-server@<release>/<product>/<category>/<article>
|
||||
const lastReleaseWithLegacyFormat = '2.18'
|
||||
|
@ -68,7 +68,7 @@ module.exports = {
|
|||
deprecatedOnNewSite,
|
||||
dates,
|
||||
firstVersionDeprecatedOnNewSite,
|
||||
lastVersionWithoutStubbedRedirectFiles,
|
||||
lastVersionWithoutArchivedRedirectsFile,
|
||||
lastReleaseWithLegacyFormat,
|
||||
deprecatedReleasesWithLegacyFormat,
|
||||
deprecatedReleasesWithNewFormat,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const path = require('path')
|
||||
const slash = require('slash')
|
||||
const { firstVersionDeprecatedOnNewSite, lastVersionWithoutStubbedRedirectFiles } = require('../lib/enterprise-server-releases')
|
||||
const { firstVersionDeprecatedOnNewSite, lastVersionWithoutArchivedRedirectsFile } = require('../lib/enterprise-server-releases')
|
||||
const patterns = require('../lib/patterns')
|
||||
const versionSatisfiesRange = require('../lib/version-satisfies-range')
|
||||
const isArchivedVersion = require('../lib/is-archived-version')
|
||||
|
@ -25,21 +25,42 @@ module.exports = async (req, res, next) => {
|
|||
}
|
||||
|
||||
// find redirects for versions between 2.13 and 2.17
|
||||
// starting with 2.18, we updated the archival script to create stubbed HTML redirect files
|
||||
// starting with 2.18, we updated the archival script to create a redirects.json file
|
||||
if (versionSatisfiesRange(requestedVersion, `>=${firstVersionDeprecatedOnNewSite}`) &&
|
||||
versionSatisfiesRange(requestedVersion, `<=${lastVersionWithoutStubbedRedirectFiles}`)) {
|
||||
versionSatisfiesRange(requestedVersion, `<=${lastVersionWithoutArchivedRedirectsFile}`)) {
|
||||
const redirect = archvivedRedirects[req.path]
|
||||
if (redirect && redirect !== req.path) {
|
||||
return res.redirect(301, redirect)
|
||||
}
|
||||
}
|
||||
|
||||
let reqPath = req.path
|
||||
let isRedirect = false
|
||||
if (versionSatisfiesRange(requestedVersion, `>${lastVersionWithoutArchivedRedirectsFile}`)) {
|
||||
try {
|
||||
const redirectJson = await got(getProxyPath('redirects.json', requestedVersion))
|
||||
|
||||
if (redirectJson[req.path]) {
|
||||
isRedirect = true
|
||||
}
|
||||
reqPath = redirectJson[req.path] || req.path
|
||||
} catch (err) {
|
||||
// nooop
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const r = await got(getProxyPath(req.path, requestedVersion))
|
||||
const r = await got(getProxyPath(reqPath, requestedVersion))
|
||||
res.set('content-type', r.headers['content-type'])
|
||||
res.set('x-robots-tag', 'noindex')
|
||||
|
||||
// make the stubbed redirect files added in >=2.18 return 301 instead of 200
|
||||
// make redirects found via redirects.json return 301 instead of 200
|
||||
if (isRedirect) {
|
||||
res.status(301)
|
||||
res.set('location', reqPath)
|
||||
}
|
||||
|
||||
// make stubbed redirect files (which exist in versions <2.13) return 301 instead of 200
|
||||
const staticRedirect = r.body.match(patterns.staticRedirect)
|
||||
if (staticRedirect) {
|
||||
res.status(301)
|
||||
|
@ -73,7 +94,7 @@ function getProxyPath (reqPath, requestedVersion) {
|
|||
// this workaround finds potentially relevant frontmatter redirects in currently supported pages
|
||||
function getFallbackRedirects (req, requestedVersion) {
|
||||
if (versionSatisfiesRange(requestedVersion, `<${firstVersionDeprecatedOnNewSite}`)) return
|
||||
if (versionSatisfiesRange(requestedVersion, `>${lastVersionWithoutStubbedRedirectFiles}`)) return
|
||||
if (versionSatisfiesRange(requestedVersion, `>${lastVersionWithoutArchivedRedirectsFile}`)) return
|
||||
|
||||
return archivedFrontmatterFallbacks.find(arrayOfFallbacks => arrayOfFallbacks.includes(req.path))
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ const host = `http://localhost:${port}`
|
|||
const scrape = require('website-scraper')
|
||||
const program = require('commander')
|
||||
const rimraf = require('rimraf').sync
|
||||
const mkdirp = require('mkdirp').sync
|
||||
const version = require('../../lib/enterprise-server-releases').oldestSupported
|
||||
const archivalRepoName = 'help-docs-archived-enterprise-versions'
|
||||
const archivalRepoUrl = `https://github.com/github/${archivalRepoName}`
|
||||
|
@ -173,7 +172,7 @@ async function main () {
|
|||
console.log(`\n\ndone scraping! added files to ${path.relative(process.cwd(), finalDirectory)}\n`)
|
||||
|
||||
// create redirect html files to preserve frontmatter redirects
|
||||
await createRedirectPages(permalinksPerVersion, pageMap, finalDirectory)
|
||||
await createRedirectsFile(permalinksPerVersion, pageMap, finalDirectory)
|
||||
|
||||
console.log(`next step: deprecate ${version} in lib/enterprise-server-releases.js`)
|
||||
|
||||
|
@ -181,10 +180,12 @@ async function main () {
|
|||
})
|
||||
}
|
||||
|
||||
async function createRedirectPages (permalinks, pageMap, finalDirectory) {
|
||||
async function createRedirectsFile (permalinks, pageMap, finalDirectory) {
|
||||
const pagesPerVersion = permalinks.map(permalink => pageMap[permalink])
|
||||
const redirects = await loadRedirects(pagesPerVersion, pageMap)
|
||||
|
||||
const redirectsPerVersion = {}
|
||||
|
||||
Object.entries(redirects).forEach(([oldPath, newPath]) => {
|
||||
// remove any liquid variables that sneak in
|
||||
oldPath = oldPath
|
||||
|
@ -193,31 +194,8 @@ async function createRedirectPages (permalinks, pageMap, finalDirectory) {
|
|||
// ignore any old paths that are not in this version
|
||||
if (!(oldPath.includes(`/enterprise-server@${version}`) || oldPath.includes(`/enterprise/${version}`))) return
|
||||
|
||||
const fullPath = path.join(finalDirectory, oldPath)
|
||||
const filename = `${fullPath}/index.html`
|
||||
const html = getRedirectHtml(newPath)
|
||||
|
||||
mkdirp(fullPath)
|
||||
fs.writeFileSync(filename, html)
|
||||
redirectsPerVersion[oldPath] = newPath
|
||||
})
|
||||
|
||||
console.log('done creating redirect files!\n')
|
||||
}
|
||||
|
||||
// redirect html files already exist in <=2.12 because these versions were deprecated on the old static site
|
||||
function getRedirectHtml (newPath) {
|
||||
return `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Redirecting...</title>
|
||||
<link rel="canonical" href="${newPath}">
|
||||
<meta http-equiv="refresh" content="0; url=${newPath}">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Redirecting...</h1>
|
||||
<a href="${newPath}">Click here if you are not redirected.</a>
|
||||
<script>location='${newPath}'</script>
|
||||
</body>
|
||||
</html>`
|
||||
fs.writeFileSync(path.posix.join(finalDirectory, 'redirects.json'), JSON.stringify(redirectsPerVersion, null, 2))
|
||||
}
|
||||
|
|
|
@ -30,6 +30,12 @@ describe('enterprise deprecation', () => {
|
|||
expect(res.headers.location).toBe('/en/enterprise/2.15/user/articles/viewing-contributions-on-your-profile')
|
||||
})
|
||||
|
||||
test('can access redirects from redirects.json in deprecated enterprise content >2.17', async () => {
|
||||
const res = await get('/enterprise/2.19/admin/categories/time')
|
||||
expect(res.statusCode).toBe(301)
|
||||
expect(res.headers.location).toBe('/en/enterprise-server@2.19/admin/configuration/configuring-time-synchronization')
|
||||
})
|
||||
|
||||
test('handles requests for deprecated Enterprise pages ( >=2.13 )', async () => {
|
||||
expect(enterpriseServerReleases.deprecated.includes('2.13')).toBe(true)
|
||||
const $ = await getDOM('/en/enterprise/2.13/user/articles/about-branches')
|
||||
|
|
Загрузка…
Ссылка в новой задаче