| 1 | export default function handleNextDataPath(req, res, next) { |
| 2 | if (req.path.startsWith('/_next/data') && req.path.endsWith('.json')) { |
| 3 | // translate a nextjs data request to a page path that the server can use on context |
| 4 | // this is triggered via client-side route tranistions |
| 5 | // example path: |
| 6 | // /_next/data/development/en/free-pro-team%40latest/github/setting-up-and-managing-your-github-user-account.json |
| 7 | const decodedPath = decodeURIComponent(req.path) |
| 8 | const parts = decodedPath.split('/').slice(4) |
| 9 | // free-pro-team@latest should not be included in the page path |
| 10 | if (parts[1] === 'free-pro-team@latest') { |
| 11 | parts.splice(1, 1) |
| 12 | } |
| 13 | req.pagePath = '/' + parts.join('/').replace(/.json+$/, '') |
| 14 | } else { |
| 15 | req.pagePath = req.path |
| 16 | } |
| 17 | |
| 18 | return next() |
| 19 | } |