11const path = require ( 'path' )
22const slash = require ( 'slash' )
3- const { firstVersionDeprecatedOnNewSite, lastVersionWithoutStubbedRedirectFiles } = require ( '../lib/enterprise-server-releases' )
3+ const { firstVersionDeprecatedOnNewSite, lastVersionWithoutArchivedRedirectsFile } = require ( '../lib/enterprise-server-releases' )
44const patterns = require ( '../lib/patterns' )
55const versionSatisfiesRange = require ( '../lib/version-satisfies-range' )
66const isArchivedVersion = require ( '../lib/is-archived-version' )
@@ -25,21 +25,43 @@ 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 res = await got ( getProxyPath ( 'redirects.json' , requestedVersion ) )
42+ const redirectJson = JSON . parse ( res . body )
43+
44+ if ( redirectJson [ req . path ] ) {
45+ isRedirect = true
46+ }
47+ reqPath = redirectJson [ req . path ] || req . path
48+ } catch ( err ) {
49+ // nooop
50+ }
51+ }
52+
3753 try {
38- const r = await got ( getProxyPath ( req . path , requestedVersion ) )
54+ const r = await got ( getProxyPath ( reqPath , requestedVersion ) )
3955 res . set ( 'content-type' , r . headers [ 'content-type' ] )
4056 res . set ( 'x-robots-tag' , 'noindex' )
4157
42- // make the stubbed redirect files added in >=2.18 return 301 instead of 200
58+ // make redirects found via redirects.json return 301 instead of 200
59+ if ( isRedirect ) {
60+ res . status ( 301 )
61+ res . set ( 'location' , reqPath )
62+ }
63+
64+ // make stubbed redirect files (which exist in versions <2.13) return 301 instead of 200
4365 const staticRedirect = r . body . match ( patterns . staticRedirect )
4466 if ( staticRedirect ) {
4567 res . status ( 301 )
@@ -73,7 +95,7 @@ function getProxyPath (reqPath, requestedVersion) {
7395// this workaround finds potentially relevant frontmatter redirects in currently supported pages
7496function getFallbackRedirects ( req , requestedVersion ) {
7597 if ( versionSatisfiesRange ( requestedVersion , `<${ firstVersionDeprecatedOnNewSite } ` ) ) return
76- if ( versionSatisfiesRange ( requestedVersion , `>${ lastVersionWithoutStubbedRedirectFiles } ` ) ) return
98+ if ( versionSatisfiesRange ( requestedVersion , `>${ lastVersionWithoutArchivedRedirectsFile } ` ) ) return
7799
78100 return archivedFrontmatterFallbacks . find ( arrayOfFallbacks => arrayOfFallbacks . includes ( req . path ) )
79101}
0 commit comments