1
1
// @ts -check
2
- const { writeFile } = require ( "fs" ) . promises ;
3
- const { readJSON } = require ( "fs-extra" ) ;
4
2
const path = require ( "path" ) ;
5
3
4
+ const { readJSON, writeFile } = require ( "fs-extra" ) ;
5
+
6
6
const ODB_FUNCTION_PATH = "/.netlify/functions/___netlify-odb-handler" ;
7
7
const HANDLER_FUNCTION_PATH = "/.netlify/functions/___netlify-handler" ;
8
8
@@ -27,41 +27,26 @@ const getNetlifyRoutes = (nextRoute) => {
27
27
// create an incorrect route for the data route. For example, it creates
28
28
// /_next/data/%BUILDID%.json, but NextJS looks for
29
29
// /_next/data/%BUILDID%/index.json
30
- netlifyRoute = netlifyRoute . replace (
31
- / ( \/ _ n e x t \/ d a t a \/ [ ^ / ] + ) .j s o n / ,
32
- "$1/index.json"
33
- ) ;
30
+ netlifyRoute = netlifyRoute . replace ( / ( \/ _ n e x t \/ d a t a \/ [ ^ / ] + ) .j s o n / , "$1/index.json" ) ;
34
31
35
32
// Add second route to the front of the array
36
33
netlifyRoutes . unshift ( netlifyRoute ) ;
37
34
}
38
35
39
36
// Replace catch-all, e.g., [...slug]
40
- netlifyRoutes = netlifyRoutes . map ( ( route ) =>
41
- route . replace ( CATCH_ALL_REGEX , "/:$1/*" )
42
- ) ;
37
+ netlifyRoutes = netlifyRoutes . map ( ( route ) => route . replace ( CATCH_ALL_REGEX , "/:$1/*" ) ) ;
43
38
44
39
// Replace optional catch-all, e.g., [[...slug]]
45
- netlifyRoutes = netlifyRoutes . map ( ( route ) =>
46
- route . replace ( OPTIONAL_CATCH_ALL_REGEX , "/*" )
47
- ) ;
40
+ netlifyRoutes = netlifyRoutes . map ( ( route ) => route . replace ( OPTIONAL_CATCH_ALL_REGEX , "/*" ) ) ;
48
41
49
42
// Replace dynamic parameters, e.g., [id]
50
- netlifyRoutes = netlifyRoutes . map ( ( route ) =>
51
- route . replace ( DYNAMIC_PARAMETER_REGEX , "/:$1" )
52
- ) ;
43
+ netlifyRoutes = netlifyRoutes . map ( ( route ) => route . replace ( DYNAMIC_PARAMETER_REGEX , "/:$1" ) ) ;
53
44
54
45
return netlifyRoutes ;
55
46
} ;
56
47
57
- exports . writeRedirects = async function ( {
58
- publishDir = "out" ,
59
- nextRoot = process . cwd ( ) ,
60
- netlifyConfig,
61
- } ) {
62
- const { dynamicRoutes } = await readJSON (
63
- path . join ( nextRoot , ".next" , "prerender-manifest.json" )
64
- ) ;
48
+ const writeRedirects = async ( { publishDir = "out" , nextRoot = process . cwd ( ) , netlifyConfig } ) => {
49
+ const { dynamicRoutes } = await readJSON ( path . join ( nextRoot , ".next" , "prerender-manifest.json" ) ) ;
65
50
66
51
const redirects = [ ] ;
67
52
Object . entries ( dynamicRoutes ) . forEach ( ( [ route , { dataRoute, fallback } ] ) => {
@@ -93,3 +78,5 @@ exports.writeRedirects = async function ({
93
78
// `;
94
79
// await writeFile(path.join(nextRoot, publishDir, "_redirects"), odbRedirects);
95
80
} ;
81
+
82
+ module . exports = writeRedirects ;
0 commit comments