1
0
mirror of synced 2025-12-21 02:46:50 -05:00
Files
docs/middleware/find-page.js
Kevin Heis 8a56437c93 Pretty format (#20352)
* Update prettier flow to include JS

* Run prettier

* ...run prettier
2021-07-14 14:35:01 -07:00

20 lines
646 B
JavaScript

// This middleware uses the request path to find a page in the preloaded context.pages object
export default async function findPage(req, res, next) {
let page = req.context.pages[req.pagePath]
// if this is a localized request that can't be found, try finding an English variant
if (!page && req.language !== 'en') {
const englishPath = req.pagePath.replace(new RegExp(`^/${req.language}`), '/en')
// NOTE the fallback page will have page.languageCode = 'en'
page = req.context.pages[englishPath]
}
if (page) {
req.context.page = page
req.context.page.version = req.context.currentVersion
}
return next()
}