@@ -12,18 +12,24 @@ import shortVersions from '../../middleware/contextualizers/short-versions.js'
1212import contextualize from '../../middleware/context.js'
1313import features from '../../middleware/contextualizers/features.js'
1414import getRedirect from '../../lib/get-redirect.js'
15+ import { isArchivedVersionByPath } from '../../lib/is-archived-version.js'
1516
1617const router = express . Router ( )
1718
1819const validationMiddleware = ( req , res , next ) => {
19- let { pathname } = req . query
20+ const { pathname } = req . query
2021 if ( ! pathname ) {
2122 return res . status ( 400 ) . json ( { error : `No 'pathname' query` } )
2223 }
2324 if ( ! pathname . trim ( ) ) {
2425 return res . status ( 400 ) . json ( { error : `'pathname' query empty` } )
2526 }
27+ req . pageinfo = { pathname }
28+ return next ( )
29+ }
2630
31+ const pageinfoMiddleware = ( req , res , next ) => {
32+ let { pathname } = req . pageinfo
2733 // We can't use the `findPage` middleware utility function because we
2834 // need to know when the pathname is a redirect.
2935 // This is important so that the final `pathname` value
@@ -46,35 +52,52 @@ const validationMiddleware = (req, res, next) => {
4652 }
4753
4854 if ( ! ( pathname in req . context . pages ) ) {
49- const redirect = getRedirect ( pathname , redirectsContext )
50- if ( redirect ) {
51- pathname = redirect
55+ // If a pathname is not a known page, it might *either* be a redirect,
56+ // or an archived enterprise version, or both.
57+ // That's why it's import to not bother looking at the redirects
58+ // if the pathname is an archived enterprise version.
59+ // This mimics how our middleware work and their order.
60+ req . pageinfo . archived = isArchivedVersionByPath ( pathname )
61+ if ( ! req . pageinfo . archived . isArchived ) {
62+ const redirect = getRedirect ( pathname , redirectsContext )
63+ if ( redirect ) {
64+ pathname = redirect
65+ }
5266 }
5367 }
54- const page = req . context . pages [ pathname ]
5568
56- if ( ! page ) {
57- return res . status ( 400 ) . json ( { error : `No page found for '${ pathname } '` } )
58- }
59-
60- req . pageinfo = {
61- pathname,
62- page,
63- }
69+ // Remember this might yield undefined if the pathname is not a page
70+ req . pageinfo . page = req . context . pages [ pathname ]
71+ // The pathname might have changed if it was a redirect
72+ req . pageinfo . pathname = pathname
6473
6574 return next ( )
6675}
6776
6877router . get (
6978 '/v1' ,
7079 validationMiddleware ,
80+ pageinfoMiddleware ,
7181 catchMiddlewareError ( async function pageInfo ( req , res ) {
7282 // Remember, the `validationMiddleware` will use redirects if the
7383 // `pathname` used is a redirect (e.g. /en/articles/foo or
7484 // /articles or '/en/enterprise-server@latest/foo/bar)
7585 // So by the time we get here, the pathname should be one of the
7686 // page's valid permalinks.
77- const { page, pathname } = req . pageinfo
87+ const { page, pathname, archived } = req . pageinfo
88+
89+ if ( archived && archived . isArchived ) {
90+ const { requestedVersion } = archived
91+ const title = `GitHub Enterprise Server ${ requestedVersion } Help Documentation`
92+ const intro = ''
93+ const product = 'GitHub Enterprise Server'
94+ defaultCacheControl ( res )
95+ return res . json ( { info : { intro, title, product } } )
96+ }
97+
98+ if ( ! page ) {
99+ return res . status ( 400 ) . json ( { error : `No page found for '${ pathname } '` } )
100+ }
78101
79102 const pagePermalinks = page . permalinks . map ( ( p ) => p . href )
80103 if ( ! pagePermalinks . includes ( pathname ) ) {
0 commit comments