1
0
mirror of synced 2025-12-22 11:26:57 -05:00
Files
docs/middleware/handle-next-data-path.js
James M. Greene 2dbae93c53 Try to prevent late/duplicate response errors (#20077)
* Ensure all Express 'next' calls include 'return's

* Add headersSent checks during archived versions flow

* Return a 404 earlier if archived resource fetch fails

* Short-circuit responses for archived stuff

* Be more careful about responding to and short-circuiting after search requests

* Fix tests
2021-06-28 19:31:54 +00:00

20 lines
807 B
JavaScript

module.exports = async function handleNextDataPath (req, res, next) {
if (req.path.startsWith('/_next/data') && req.path.endsWith('.json')) {
// translate a nextjs data request to a page path that the server can use on context
// this is triggered via client-side route tranistions
// example path:
// /_next/data/development/en/free-pro-team%40latest/github/setting-up-and-managing-your-github-user-account.json
const decodedPath = decodeURIComponent(req.path)
const parts = decodedPath.split('/').slice(4)
// free-pro-team@latest should not be included in the page path
if (parts[1] === 'free-pro-team@latest') {
parts.splice(1,1)
}
req.pagePath = '/' + parts.join('/').replace(/.json+$/, '')
} else {
req.pagePath = req.path
}
return next()
}