@progfay/nextjs-ssg-rewrite-rule-gen
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -9,9 +9,32 @@ "use strict"; | ||
const node_path_1 = __importDefault(require("node:path")); | ||
function assertNginxConfig(obj) { | ||
if (typeof obj !== "object" || obj === null) { | ||
throw new TypeError("nginxConfig must be JSON Object"); | ||
} | ||
if (!("pattern" in obj) || typeof obj.pattern !== "string") { | ||
throw new TypeError('nginxConfig property "pattern" must be string'); | ||
} | ||
try { | ||
new RegExp(obj.pattern); | ||
} | ||
catch (e) { | ||
if (e instanceof SyntaxError) { | ||
const error = new SyntaxError(`invalid RegExp pattern: ${JSON.stringify(obj.pattern)}`); | ||
error.cause = e; | ||
throw error; | ||
} | ||
} | ||
if (!("directives" in obj) || | ||
!Array.isArray(obj.directives) || | ||
obj.directives.some((d) => typeof d !== "string")) { | ||
throw new TypeError('nginxConfig property "directives" must be string array'); | ||
} | ||
} | ||
function assertRawConfig(obj) { | ||
if (typeof obj !== "object" || obj === null) { | ||
throw new Error("config must be JSON"); | ||
throw new TypeError("config must be JSON Object"); | ||
} | ||
if ("pagesDirPath" in obj) { | ||
if (typeof obj.pagesDirPath !== "string") { | ||
throw new Error('config must have string field "pagesDirPath"'); | ||
throw new TypeError('config property "pagesDirPath" must be string'); | ||
} | ||
@@ -22,11 +45,19 @@ } | ||
obj.ignoredRoutes.some((route) => typeof route !== "string")) { | ||
throw new Error('config must have string array field "ignoredRoutes"'); | ||
throw new TypeError('config property "ignoredRoutes" must be string array'); | ||
} | ||
} | ||
if ("nginxConfigs" in obj) { | ||
if (!Array.isArray(obj.nginxConfigs)) { | ||
throw new TypeError('config property "nginxConfigs" must be array'); | ||
} | ||
for (const nginxConfig of obj.nginxConfigs) { | ||
assertNginxConfig(nginxConfig); | ||
} | ||
} | ||
if ("basePath" in obj) { | ||
if (typeof obj.basePath !== "string") { | ||
throw new Error('"basePath" config must be string'); | ||
throw new TypeError('config property "basePath" must be string'); | ||
} | ||
if (obj.basePath.startsWith("/")) { | ||
throw new Error('"basePath" config must be start with "/"'); | ||
if (!obj.basePath.startsWith("/")) { | ||
throw new TypeError('config property "basePath" must be start with "/"'); | ||
} | ||
@@ -36,3 +67,3 @@ } | ||
if (typeof obj.trailingSlash !== "boolean") { | ||
throw new Error('"trailingSlash" config must be boolean'); | ||
throw new TypeError('config property "trailingSlash" must be boolean'); | ||
} | ||
@@ -82,5 +113,6 @@ } | ||
ignoredRoutes: rawConfig.ignoredRoutes ?? [], | ||
nginxConfigs: rawConfig.nginxConfigs ?? [], | ||
// `basePath` config is `""` by default. | ||
// ref. https://github.com/vercel/next.js/blob/127c5bbf80d44e256533db028d7a595a1c3defe0/packages/next/src/server/config-shared.ts#L675 | ||
basePath: "", | ||
basePath: rawConfig.basePath ?? "", | ||
// `trailingSlash` config is `false` by default. | ||
@@ -87,0 +119,0 @@ // ref. https://github.com/vercel/next.js/blob/127c5bbf80d44e256533db028d7a595a1c3defe0/packages/next/src/server/config-shared.ts#L677 |
@@ -27,3 +27,3 @@ "use strict"; | ||
const orderedRoutes = (0, nextjs_1.sortRoutesByRoutingPriorityOrder)(filteredRoutes); | ||
const pageDirectives = orderedRoutes.map((route) => { | ||
const rewriteRules = orderedRoutes.map((route) => { | ||
const pattern = (0, nextjs_1.generateNextjsPathPattern)({ | ||
@@ -37,11 +37,14 @@ route, | ||
}); | ||
return (0, nginx_1.generateNginxRewriteRule)({ pattern, filePath }); | ||
const additionalDirectives = config.nginxConfigs | ||
.filter((nginxConfig) => new RegExp(nginxConfig.pattern).test(route)) | ||
.flatMap((nginxConfig) => nginxConfig.directives); | ||
return (0, nginx_1.generateNginxRewriteRule)({ | ||
pattern, | ||
filePath, | ||
additionalDirectives, | ||
}); | ||
}); | ||
const directives = [ | ||
...pageDirectives, | ||
(0, nginx_1.generateFallbackNginxRewriteRule)(config.basePath), | ||
]; | ||
// eslint-disable-next-line no-console | ||
console.log(directives.join("\n\n")); | ||
console.log(rewriteRules.join("\n\n")); | ||
}; | ||
main(); |
@@ -9,3 +9,2 @@ "use strict"; | ||
exports.generateNextjsExportedHtmlFilePath = exports.generateNextjsPathPattern = exports.rejectUnnecessaryRoutes = exports.convertPageFilePathToRoute = exports.sortRoutesByRoutingPriorityOrder = void 0; | ||
const utils_1 = require("./utils"); | ||
/** | ||
@@ -47,2 +46,3 @@ * @see {@link https://github.com/vercel/next.js/blob/127c5bbf80d44e256533db028d7a595a1c3defe0/docs/03-pages/01-building-your-application/03-data-fetching/06-building-forms.mdx#L168} | ||
exports.sortRoutesByRoutingPriorityOrder = sortRoutesByRoutingPriorityOrder; | ||
const trimPrefix = (target, prefix) => target.startsWith(prefix) ? target.slice(prefix.length) : target; | ||
/** | ||
@@ -53,3 +53,3 @@ * @param pageFilePath path for file under the `pages` directory | ||
const convertPageFilePathToRoute = (absolutePageFilePath, { pagesDirPath }) => { | ||
const pageFilePath = (0, utils_1.trimPrefix)(absolutePageFilePath, pagesDirPath); | ||
const pageFilePath = trimPrefix(absolutePageFilePath, pagesDirPath); | ||
const route = pageFilePath | ||
@@ -56,0 +56,0 @@ .replace(/\.(js|jsx|ts|tsx)$/, "") |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.generateFallbackNginxRewriteRule = exports.generateNginxRewriteRule = void 0; | ||
exports.generateNginxRewriteRule = void 0; | ||
/** | ||
@@ -8,4 +8,6 @@ * @description generate {@link https://www.nginx.com/blog/creating-nginx-rewrite-rules/ | nginx rewrite rule} | ||
*/ | ||
const generateNginxRewriteRule = ({ pattern, filePath, }) => ` | ||
location ~ ${pattern} { | ||
const generateNginxRewriteRule = ({ pattern, filePath, additionalDirectives, }) => ` | ||
location ~ ${pattern} {${additionalDirectives.length > 0 | ||
? `\n ${additionalDirectives.join("\n ")}` | ||
: ""} | ||
rewrite ${pattern} ${filePath} break; | ||
@@ -15,11 +17,1 @@ } | ||
exports.generateNginxRewriteRule = generateNginxRewriteRule; | ||
/** | ||
* @description generate {@link https://www.nginx.com/blog/creating-nginx-rewrite-rules/ | nginx rewrite rule} | ||
* for non-page files (assets other than `.html` files) | ||
*/ | ||
const generateFallbackNginxRewriteRule = (basePath) => ` | ||
location / { | ||
rewrite ^${basePath}/(.*)$ /$1 break; | ||
} | ||
`.trim(); | ||
exports.generateFallbackNginxRewriteRule = generateFallbackNginxRewriteRule; |
{ | ||
"name": "@progfay/nextjs-ssg-rewrite-rule-gen", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "generate nginx rewrite rules for Next.js SSG (Pages Router)", | ||
@@ -9,3 +9,7 @@ "main": "dist/index.js", | ||
}, | ||
"files": ["bin", "dist", "package.json"], | ||
"files": [ | ||
"bin", | ||
"dist", | ||
"package.json" | ||
], | ||
"scripts": { | ||
@@ -15,3 +19,3 @@ "prepare": "npm run build", | ||
"build": "tsc -p tsconfig.json", | ||
"generate": "ts-node -P tsconfig.json index.ts", | ||
"generate": "./bin/nextjs-ssg-rewrite-rule-gen.js", | ||
"test": "jest --config jest.config.js", | ||
@@ -28,11 +32,9 @@ "lint": "biome check src", | ||
"devDependencies": { | ||
"@biomejs/biome": "1.5.0", | ||
"@biomejs/biome": "1.5.2", | ||
"@swc/jest": "0.2.29", | ||
"@types/jest": "29.5.11", | ||
"jest": "29.7.0", | ||
"ts-node": "10.9.2", | ||
"typescript": "5.3.3" | ||
}, | ||
"dependencies": { | ||
"assert-never": "1.2.1", | ||
"glob": "10.3.10" | ||
@@ -39,0 +41,0 @@ }, |
@@ -34,2 +34,8 @@ # `@progfay/nextjs-ssg-rewrite-rule-gen` | ||
"ignoreRoutes": ["/debug"], | ||
"nginxConfigs": [ | ||
{ | ||
"pattern": "^/credential$", | ||
"directives": ["add_header Cache-Control \"no-store\";"] | ||
} | ||
], | ||
"basePath": "/app", | ||
@@ -43,2 +49,5 @@ "trailingSlash": true | ||
- `ignoreRoutes`: exclude specific paths from outputs | ||
- `nginxConfigs`: customize configuration inside of [nginx `location` directive](https://nginx.org/en/docs/http/ngx_http_core_module.html#location) | ||
- `pattern`: pattern string of [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp#pattern) | ||
- `directives`: additional nginx directives | ||
- `basePath`: [next.config.js Options: basePath | Next.js](https://nextjs.org/docs/app/api-reference/next-config-js/basePath) | ||
@@ -45,0 +54,0 @@ - `trailingSlash`: [next.config.js Options: trailingSlash | Next.js](https://nextjs.org/docs/app/api-reference/next-config-js/trailingSlash) |
16943
1
5
273
88
8
- Removedassert-never@1.2.1
- Removedassert-never@1.2.1(transitive)