-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathgetPrerenderManifest.js
37 lines (33 loc) · 1.3 KB
/
getPrerenderManifest.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
33
34
35
36
37
const { join } = require('path')
const { readJSONSync } = require('fs-extra')
const getNextConfig = require('../../../helpers/getNextConfig')
const getNextDistDir = require('./getNextDistDir')
const getDataRouteForRoute = require('./getDataRouteForRoute')
const asyncForEach = require('./asyncForEach')
const transformManifestForI18n = async (manifest) => {
const { routes } = manifest
const newRoutes = {}
await asyncForEach(Object.entries(routes), async ([route, { dataRoute, srcRoute, ...params }]) => {
const isDynamicRoute = !!srcRoute
if (isDynamicRoute) {
newRoutes[route] = routes[route]
} else {
const locale = route.split('/')[1]
const routeWithoutLocale = `/${route.split('/').slice(2, route.split('/').length).join('/')}`
newRoutes[route] = {
dataRoute: await getDataRouteForRoute(routeWithoutLocale, locale),
srcRoute: routeWithoutLocale,
...params,
}
}
})
return { ...manifest, routes: newRoutes }
}
const getPrerenderManifest = async () => {
const nextConfig = await getNextConfig()
const nextDistDir = await getNextDistDir()
const manifest = readJSONSync(join(nextDistDir, 'prerender-manifest.json'))
if (nextConfig.i18n) return await transformManifestForI18n(manifest)
return manifest
}
module.exports = getPrerenderManifest