1
0
mirror of synced 2025-12-23 21:07:12 -05:00
Files
docs/middleware/find-page.js
Mike Surowiec a24d01f78a Fix NextJS 500s (#20048)
* feat: allow server to contextualize request when on a /_next/data path

* add client side routing test, run lint

* enable nextjs client side routing
2021-06-22 17:30:40 +00:00

20 lines
649 B
JavaScript

// This middleware uses the request path to find a page in the preloaded context.pages object
module.exports = 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()
}