forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product-groups.js
32 lines (29 loc) · 1.17 KB
/
product-groups.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { getProductGroups } from '../../lib/all-products.js'
import warmServer from '../../lib/warm-server.js'
import { languageKeys } from '../../lib/languages.js'
import { allVersionKeys } from '../../lib/all-versions.js'
const isHomepage = (path) => {
const split = path.split('/')
// E.g. `/foo` but not `foo/bar` or `foo/`
if (split.length === 2 && split[1] && !split[0]) {
return languageKeys.includes(split[1])
}
// E.g. `/foo/possiblyproductname` but not `foo/possiblyproductname` or
// `/foo/something/`
if (split.length === 3 && !split[0] && split[2]) {
return allVersionKeys.includes(split[2])
}
return false
}
export default async function productGroups(req, res, next) {
// It's important to use `req.pathPage` instead of `req.path` because
// the request could be the client-side routing from Next where the URL
// might be something like `/_next/data/foo/bar.json` which is translated,
// in another middleware, to what it would equate to if it wasn't
// client-side routing.
if (isHomepage(req.pagePath)) {
const { pages } = await warmServer()
req.context.productGroups = getProductGroups(pages, req.language)
}
return next()
}