-
Notifications
You must be signed in to change notification settings - Fork 16
/
generate.js
35 lines (27 loc) · 1 KB
/
generate.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
#!/usr/bin/env node
"use strict";
const { cwd } = require("process");
const { readFileSync, writeFileSync } = require("fs");
const routesManifest = "./.next/routes-manifest.json";
const manifest = JSON.parse(readFileSync(routesManifest, "utf8"));
const { basePath, dynamicRoutes, staticRoutes } = manifest;
const requiredServerFiles = "./.next/required-server-files.json";
const { config } = JSON.parse(readFileSync(requiredServerFiles, "utf-8"));
const routes = staticRoutes.concat(dynamicRoutes).map((route) => {
let { page, regex } = route;
if (route.page === "/") {
page = "/index";
regex = basePath ? `^${basePath}${regex.slice(2)}` : regex;
} else {
if (config.trailingSlash) {
page = `${route.page}/index`;
}
regex = `^${basePath || ""}${regex.slice(1)}`;
}
return `
location ~ ${regex} {
try_files ${page}.html /index.html;
}`;
});
writeFileSync("./next-routes.conf", routes.join("\n"));
console.log(`Nginx routes configuration written to ${cwd()}/next-routes.conf`);