(req, res, next)
| 28 | // Supply all route handlers with a baseline `req.context` object |
| 29 | // Note that additional middleware in middleware/index.js adds to this context object |
| 30 | export default async function contextualize(req, res, next) { |
| 31 | // Ensure that we load some data only once on first request |
| 32 | const { redirects, siteTree, pages: pageMap } = await warmServer() |
| 33 | |
| 34 | req.context = {} |
| 35 | req.context.process = { env: {} } |
| 36 | |
| 37 | // define each context property explicitly for code-search friendliness |
| 38 | // e.g. searches for "req.context.page" will include results from this file |
| 39 | req.context.currentLanguage = req.language |
| 40 | req.context.userLanguage = req.userLanguage |
| 41 | req.context.currentVersion = getVersionStringFromPath(req.pagePath) |
| 42 | req.context.currentProduct = getProductStringFromPath(req.pagePath) |
| 43 | req.context.currentCategory = getCategoryStringFromPath(req.pagePath) |
| 44 | req.context.productMap = productMap |
| 45 | req.context.activeProducts = activeProducts |
| 46 | req.context.allVersions = allVersions |
| 47 | req.context.currentPathWithoutLanguage = getPathWithoutLanguage(req.pagePath) |
| 48 | |
| 49 | // define property for writers to link to the current page in a different version |
| 50 | // includes any type of rendered page not just "articles" |
| 51 | req.context.currentArticle = getPathWithoutVersion(req.context.currentPathWithoutLanguage) |
| 52 | req.context.currentPath = req.pagePath |
| 53 | req.context.query = req.query |
| 54 | req.context.languages = languages |
| 55 | req.context.productNames = productNames |
| 56 | req.context.enterpriseServerReleases = enterpriseServerReleases |
| 57 | req.context.enterpriseServerVersions = enterpriseServerVersions |
| 58 | req.context.redirects = redirects |
| 59 | req.context.site = { |
| 60 | data: { |
| 61 | ui: getUIDataMerged(req.language), |
| 62 | }, |
| 63 | } |
| 64 | req.context.getDottedData = (dottedPath) => getDataByLanguage(dottedPath, req.language) |
| 65 | req.context.siteTree = siteTree |
| 66 | req.context.pages = pageMap |
| 67 | req.context.searchVersions = searchVersions |
| 68 | req.context.nonEnterpriseDefaultVersion = nonEnterpriseDefaultVersion |
| 69 | req.context.initialRestVersioningReleaseDate = |
| 70 | allVersions[nonEnterpriseDefaultVersion].apiVersions[0] |
| 71 | |
| 72 | const restDate = new Date(req.context.initialRestVersioningReleaseDate) |
| 73 | req.context.initialRestVersioningReleaseDateLong = restDate.toUTCString().split(' 00:')[0] |
| 74 | |
| 75 | // Conditionally add this for non-English pages so what inside the |
| 76 | // `Page.render` method, when it calls out to `renderContentWithFallback` |
| 77 | // it can be able to fall back get original content from English if there's |
| 78 | // some runtime rendering error from the translation. |
| 79 | if (req.language !== 'en') { |
| 80 | // The reason this is a function is because most of the time, we don't |
| 81 | // need to know the English equivalent. It only comes into play if a |
| 82 | // translated |
| 83 | req.context.getEnglishPage = (context) => { |
| 84 | if (!context.enPage) { |
| 85 | const { page } = context |
| 86 | if (!page) { |
| 87 | throw new Error("The 'page' has not been put into the context yet.") |
no test coverage detected