(
req,
res,
next,
// Express won't execute these but it makes it easier to unit test
// the middleware.
{ isDev = process.env.NODE_ENV === 'development', contentRoot = CONTENT_ROOT } = {}
)
| 10 | const CONTENT_ROOT = path.join(ROOT, 'content') |
| 11 | |
| 12 | export default async function findPage( |
| 13 | req, |
| 14 | res, |
| 15 | next, |
| 16 | // Express won't execute these but it makes it easier to unit test |
| 17 | // the middleware. |
| 18 | { isDev = process.env.NODE_ENV === 'development', contentRoot = CONTENT_ROOT } = {} |
| 19 | ) { |
| 20 | // Filter out things like `/will/redirect` or `/_next/data/...` |
| 21 | if (!languagePrefixRegex.test(req.pagePath)) { |
| 22 | return next() |
| 23 | } |
| 24 | |
| 25 | let page = req.context.pages[req.pagePath] |
| 26 | if (page && isDev && englishPrefixRegex.test(req.pagePath)) { |
| 27 | page = await rereadByPath(req.pagePath, contentRoot, req.context.currentVersion) |
| 28 | |
| 29 | // This can happen if the page we just re-read has changed which |
| 30 | // versions it's available in (the `versions` frontmatter) meaning |
| 31 | // it might no longer be available on the current URL. |
| 32 | if (!page.applicableVersions.includes(req.context.currentVersion)) { |
| 33 | return res |
| 34 | .status(404) |
| 35 | .send( |
| 36 | `After re-reading the page, '${req.context.currentVersion}' is no longer an applicable version. ` + |
| 37 | 'A restart is required.' |
| 38 | ) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | if (page) { |
| 43 | req.context.page = page |
| 44 | req.context.page.version = req.context.currentVersion |
| 45 | |
| 46 | // We can't depend on `page.hidden` because the dedicated search |
| 47 | // results page is a hidden page but it needs to offer all possible |
| 48 | // languages. |
| 49 | if (page.relativePath.startsWith('early-access')) { |
| 50 | // Override the languages to be only English |
| 51 | req.context.languages = { |
| 52 | en: req.context.languages.en, |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return next() |
| 58 | } |
| 59 | |
| 60 | async function rereadByPath(uri, contentRoot, currentVersion) { |
| 61 | const languageCode = uri.match(languagePrefixRegex)[1] |
nothing calls this directly
no test coverage detected