forked from opennextjs/opennextjs-netlify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetDataRouteForRoute.js
28 lines (22 loc) · 1.05 KB
/
getDataRouteForRoute.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
const { join } = require('path')
const { readFileSync } = require('fs-extra')
const getFilePathForRoute = require('./getFilePathForRoute')
const getNextDistDir = require('./getNextDistDir')
const getPlainDataRoute = (route, buildId) => {
const filePath = getFilePathForRoute(route, 'json')
return `/_next/data/${buildId}${filePath}`
}
const getI18nDataRoute = (route, locale, buildId) => {
const filePath = getFilePathForRoute(route, 'json')
return route === '/' ? getPlainDataRoute(`/${locale}`, buildId) : `/_next/data/${buildId}/${locale}${filePath}`
}
// Return the data route for the given route
const getDataRouteForRoute = async ({ route, locale, publishPath }) => {
const nextDistDir = await getNextDistDir({ publishPath })
// Get build ID that is used for data routes, e.g. /_next/data/BUILD_ID/...
const fileContents = readFileSync(join(nextDistDir, 'BUILD_ID'))
const buildId = fileContents.toString()
if (locale) return getI18nDataRoute(route, locale, buildId)
return getPlainDataRoute(route, buildId)
}
module.exports = getDataRouteForRoute