Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support basePath #6

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions __snapshots__/generate.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`generate custom routes for example project 1`] = `
"
location ~ ^/next(?:/)?$ {
try_files /index.html /index.html;
}

location ~ ^/next/hello(?:/)?$ {
try_files /hello.html /index.html;
}

location ~ ^/next/hello/world(?:/)?$ {
try_files /hello/world.html /index.html;
}

location ~ ^/next/([^/]+?)(?:/)?$ {
try_files /[foo].html /index.html;
}

location ~ ^/next/([^/]+?)/([^/]+?)/hello(?:/)?$ {
try_files /[foo]/[bar]/hello.html /index.html;
}

location ~ ^/next/([^/]+?)/([^/]+?)/([^/]+?)(?:/)?$ {
try_files /[foo]/[bar]/[id].html /index.html;
}"
`;

exports[`generate routes for example project 1`] = `
"
location ~ ^/(?:/)?$ {
Expand Down
3 changes: 3 additions & 0 deletions example/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
basePath: process.env.BASE_PATH || ''
}
5 changes: 2 additions & 3 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
},
"devDependencies": {
"eslint": "^8.26.0",
"eslint-config-next": "^13.0.0",
"next-nginx-routes": "./.."
"eslint-config-next": "^13.0.0"
},
"scripts": {
"export": "next build && next export && next-nginx-routes"
"export": "next build && next export && node ../generate.js"
}
}
19 changes: 16 additions & 3 deletions generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,25 @@ const manifest = JSON.parse(readFileSync(routesManifest, "utf8"));
const routes = manifest.staticRoutes
.concat(manifest.dynamicRoutes)
.map((route) => {
const basePath = manifest.basePath || "";

let page = route.page;
let regex = route.regex;

if (route.page === "/") {
route.page = "/index";
page = "/index";
}

if (basePath) {
regex =
route.regex.slice(0, 1) +
basePath +
route.regex.slice(route.page === "/" ? 2 : 1);
}

return `
location ~ ${route.regex} {
try_files ${route.page}.html /index.html;
location ~ ${regex} {
try_files ${page}.html /index.html;
}`;
});

Expand Down
5 changes: 5 additions & 0 deletions generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ test("generate routes for example project", () => {
execSync("cd example && npm run export");
expect(readFileSync("./example/next-routes.conf", "utf8")).toMatchSnapshot();
});

test("generate custom routes for example project", () => {
execSync("cd example && export BASE_PATH=/next && npm run export");
expect(readFileSync("./example/next-routes.conf", "utf8")).toMatchSnapshot();
});