@koine/next 1.0.41 → 1.0.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/config/index.d.ts CHANGED
@@ -1,27 +1,30 @@
1
1
  import type { NextConfig } from "next";
2
2
  import type { Redirect, Rewrite } from "next/dist/lib/load-custom-routes";
3
+ declare type Route = string | {
4
+ [key: string]: Route | string;
5
+ };
6
+ declare type Routes = Record<string, Route>;
3
7
  /**
4
- * Transform to path any absolute or relative URL
5
- *
6
- * Useful when setting up `rewrites` and `redirects` especally in a [multi-zones
7
- * setup](https://nextjs.org/docs/advanced-features/multi-zones).
8
+ * Normalise pathname
8
9
  *
9
- * From a path like `http://localhost/some//malformed/path///` it returns `/some/malformed/path`
10
+ * From a path like `/some//malformed/path///` it returns `some/malformed/path`
10
11
  *
11
- * - just get the pathname form an absolute URL (if that is given)
12
12
  * - Removes subsequent slashes
13
13
  * - Removing initial and ending slashes
14
+ * - Returns an empty string `"""` if only slashes are given
14
15
  */
15
- export declare function toPath(urlOrPathname?: string): string;
16
+ export declare function normaliseUrlPathname(pathname?: string): string;
16
17
  /**
17
- * Normalise pathname
18
+ * Transform to path any absolute or relative URL
18
19
  *
19
- * From a path like `/some//malformed/path///` it returns `some/malformed/path`
20
+ * Useful when setting up `rewrites` and `redirects` especally in a [multi-zones
21
+ * setup](https://nextjs.org/docs/advanced-features/multi-zones).
20
22
  *
21
- * - Removes subsequent slashes
22
- * - Removing initial and ending slashes
23
+ * From a path like `http://localhost/some//malformed/path///` it returns `/some/malformed/path`
24
+ *
25
+ * @see {@link normaliseUrlPathname}
23
26
  */
24
- export declare function normaliseUrlPathname(pathname?: string): string;
27
+ export declare function toPath(urlOrPathname?: string): string;
25
28
  /**
26
29
  * Clean a pathname and encode each part
27
30
  *
@@ -29,33 +32,27 @@ export declare function normaliseUrlPathname(pathname?: string): string;
29
32
  */
30
33
  export declare function encodePathname(pathname?: string): string;
31
34
  /**
35
+ * Get path rewrite
32
36
  */
33
- export declare function getPathRedirect(locale: string, pathname: string, template: string, dynamic?: boolean, permanent?: boolean): {
37
+ export declare function getPathRewrite(pathname: string, template: string): {
34
38
  source: string;
35
39
  destination: string;
36
- permanent: boolean;
37
- locale: false;
38
40
  };
39
41
  /**
42
+ * Get path redirect
40
43
  */
41
- export declare function getPathRewrite(pathname: string, template: string, dynamic?: boolean): {
44
+ export declare function getPathRedirect(locale: string | undefined, pathname: string, template: string, permanent?: boolean): {
42
45
  source: string;
43
46
  destination: string;
47
+ permanent: boolean;
48
+ locale: false;
44
49
  };
45
50
  /**
46
51
  */
47
- export declare function getRedirects({ defaultLocale, routes, dynamicRoutes, permanent, }: {
48
- defaultLocale: string;
49
- routes: Record<string, string>;
50
- dynamicRoutes: Record<string, boolean>;
51
- permanent?: boolean;
52
- }): Promise<Redirect[]>;
52
+ export declare function getRedirects(defaultLocale: string, routes: Routes, permanent?: boolean, debug?: boolean): Promise<Redirect[]>;
53
53
  /**
54
54
  */
55
- export declare function getRewrites({ routes, dynamicRoutes, }: {
56
- routes: Record<string, string>;
57
- dynamicRoutes: Record<string, boolean>;
58
- }): Promise<Rewrite[]>;
55
+ export declare function getRewrites(routes: Routes, debug?: boolean): Promise<Rewrite[]>;
59
56
  declare type KoineNextConfig = {
60
57
  /** @default true Nx monorepo setup */
61
58
  nx?: boolean;
@@ -68,6 +65,58 @@ declare type KoineNextConfig = {
68
65
  * @default false
69
66
  */
70
67
  page?: boolean;
68
+ /**
69
+ * A JSON file containing the routes definition mapping template folders
70
+ * to localised slugs. It supports slugs's dynamic portions.
71
+ * All translated slugs needs to be defined starting from root `/`.
72
+ * The `require("...json")` file for each locale should look like:
73
+ *
74
+ * ```json
75
+ * {
76
+ * "home": "/",
77
+ * "products": {
78
+ * "list": "/products",
79
+ * "[category]": "/products/[category]",
80
+ * "[tag]": {
81
+ * "view": "/products/[category]/[tag]",
82
+ * "related": "/products/[category]/[tag]/related"
83
+ * }
84
+ * },
85
+ * "company": {
86
+ * "about": "/about",
87
+ * "contact": "/contact"
88
+ * }
89
+ * }
90
+ * ```
91
+ *
92
+ * NOTE1:
93
+ * You cannot name your dynamic template file "index.tsx" when they have nested
94
+ * segments or the rewrites won't work. So while this is allowed:
95
+ * `/pages/cats/[category]/index.tsx` it is not when you also have e.g.:
96
+ * `/pages/cats/[category]/reviews.tsx`
97
+ * TODO: This might be fixable?
98
+ *
99
+ * NOTE2:
100
+ * When `routes` is used be sure to pass before than the `i18n.defaultLocale`
101
+ * configuration. That is used for localised routing. By default we set `i18n`
102
+ * as such:
103
+ * ```js
104
+ * {
105
+ * // ...nextConfig,
106
+ * i18n: {
107
+ * defaultLocale: "en",
108
+ * locales: ["en"],
109
+ * }
110
+ * }
111
+ * ```
112
+ */
113
+ routes?: Routes;
114
+ /**
115
+ * Whether the routes redirecting should be permanent. Switch this on once you
116
+ * go live and the routes structure is stable.
117
+ */
118
+ permanent?: boolean;
119
+ debug?: boolean;
71
120
  };
72
121
  /**
73
122
  * Get Next.js config with some basic opinionated defaults
@@ -80,5 +129,5 @@ declare type KoineNextConfig = {
80
129
  * extension for next.js config option [`pageExtensions`](https://nextjs.org/docs/api-reference/next.config.js/custom-page-extensions#including-non-page-files-in-the-pages-directory)
81
130
  * and it enables the same for `next-translate`.
82
131
  */
83
- export declare function withKoine({ nx, svg, sc, page, ...nextConfig }?: NextConfig & KoineNextConfig): NextConfig;
132
+ export declare function withKoine({ nx, svg, sc, page, routes, permanent, debug, ...custom }?: NextConfig & KoineNextConfig): NextConfig;
84
133
  export default withKoine;
package/config/index.js CHANGED
@@ -1,4 +1,19 @@
1
- import { __assign, __awaiter, __generator, __rest } from "tslib";
1
+ import { __assign, __awaiter, __generator, __rest, __spreadArray } from "tslib";
2
+ /**
3
+ * Normalise pathname
4
+ *
5
+ * From a path like `/some//malformed/path///` it returns `some/malformed/path`
6
+ *
7
+ * - Removes subsequent slashes
8
+ * - Removing initial and ending slashes
9
+ * - Returns an empty string `"""` if only slashes are given
10
+ */
11
+ export function normaliseUrlPathname(pathname) {
12
+ if (pathname === void 0) { pathname = ""; }
13
+ // with return pathname.replace(/\/+\//g, "/").replace(/^\/+(.*?)\/+$/, "$1");
14
+ // we would instead return a single slash if only slashes are given
15
+ return pathname.replace(/\/+\//g, "/").replace(/^\/*(.*?)\/*$/, "$1");
16
+ }
2
17
  /**
3
18
  * Transform to path any absolute or relative URL
4
19
  *
@@ -7,9 +22,7 @@ import { __assign, __awaiter, __generator, __rest } from "tslib";
7
22
  *
8
23
  * From a path like `http://localhost/some//malformed/path///` it returns `/some/malformed/path`
9
24
  *
10
- * - just get the pathname form an absolute URL (if that is given)
11
- * - Removes subsequent slashes
12
- * - Removing initial and ending slashes
25
+ * @see {@link normaliseUrlPathname}
13
26
  */
14
27
  export function toPath(urlOrPathname) {
15
28
  if (urlOrPathname === void 0) { urlOrPathname = ""; }
@@ -21,19 +34,9 @@ export function toPath(urlOrPathname) {
21
34
  catch (e) {
22
35
  pathname = urlOrPathname;
23
36
  }
24
- return pathname.replace(/\/+\//g, "/").replace(/^\/+(.*?)\/+$/, "$1");
25
- }
26
- /**
27
- * Normalise pathname
28
- *
29
- * From a path like `/some//malformed/path///` it returns `some/malformed/path`
30
- *
31
- * - Removes subsequent slashes
32
- * - Removing initial and ending slashes
33
- */
34
- export function normaliseUrlPathname(pathname) {
35
- if (pathname === void 0) { pathname = ""; }
36
- return pathname.replace(/\/+\//g, "/").replace(/^\/+(.*?)\/+$/, "$1");
37
+ // with return pathname.replace(/\/+\//g, "/").replace(/^\/+(.*?)\/+$/, "$1");
38
+ // we would instead return a single slash if only slashes are given
39
+ return pathname.replace(/\/+\//g, "/").replace(/^\/*(.*?)\/*$/, "$1");
37
40
  }
38
41
  /**
39
42
  * Clean a pathname and encode each part
@@ -49,75 +52,108 @@ export function encodePathname(pathname) {
49
52
  .join("/");
50
53
  }
51
54
  /**
55
+ * It replaces `/[dynamic-slug]/` with the given replacer.
52
56
  */
53
- export function getPathRedirect(locale, pathname, template, dynamic, permanent) {
54
- var suffix = dynamic ? "/:slug*" : "";
55
- var source = "/".concat(locale ? locale + "/" : "").concat(encodePathname(pathname)).concat(suffix);
56
- var destination = "/".concat(encodePathname(template)).concat(suffix);
57
- // console.log(`redirect pathname "${source}" to template "${destination}"`);
57
+ function transformRoutePathname(rawPathname, replacer) {
58
+ var pathNameParts = rawPathname.split("/").filter(function (part) { return !!part; });
59
+ return pathNameParts
60
+ .map(function (part, _idx) {
61
+ var isDynamic = part.startsWith("[") && part.endsWith("]");
62
+ part = isDynamic ? replacer : part;
63
+ // if (isDynamic && _idx === pathNameParts.length - 1) {
64
+ // part += "*";
65
+ // }
66
+ return isDynamic ? part : encodeURIComponent(part);
67
+ })
68
+ .join("/");
69
+ }
70
+ /**
71
+ * Get flat key/value routes map dictionary
72
+ */
73
+ function getRoutesMap(map, routes, pathnameBuffer, templateBuffer) {
74
+ if (map === void 0) { map = {}; }
75
+ if (pathnameBuffer === void 0) { pathnameBuffer = ""; }
76
+ if (templateBuffer === void 0) { templateBuffer = ""; }
77
+ for (var key in routes) {
78
+ var pathOrNestedRoutes = routes[key];
79
+ var template = "".concat(templateBuffer, "/").concat(key);
80
+ if (typeof pathOrNestedRoutes === "string") {
81
+ map[template] = pathOrNestedRoutes;
82
+ }
83
+ else {
84
+ getRoutesMap(map, pathOrNestedRoutes, pathnameBuffer, template);
85
+ }
86
+ }
87
+ return map;
88
+ }
89
+ /**
90
+ * Get path rewrite
91
+ */
92
+ export function getPathRewrite(pathname, template) {
93
+ pathname = transformRoutePathname(pathname, ":path");
94
+ template = transformRoutePathname(template, ":path");
95
+ var source = "/".concat(normaliseUrlPathname(pathname));
96
+ var destination = "/".concat(normaliseUrlPathname(template));
97
+ // console.log(`rewrite pathname "${source}" to template "${destination}"`);
58
98
  return {
59
99
  source: source,
60
100
  destination: destination,
61
- permanent: Boolean(permanent),
62
- locale: false,
63
101
  };
64
102
  }
65
103
  /**
104
+ * Get path redirect
66
105
  */
67
- export function getPathRewrite(pathname, template, dynamic) {
68
- var suffix = dynamic ? "/:path*" : "";
69
- var source = "/".concat(encodePathname(pathname)).concat(suffix);
70
- var destination = "/".concat(encodePathname(template)).concat(suffix);
71
- // console.log(`rewriting pathname "${source}" to template "${destination}"`);
106
+ export function getPathRedirect(locale, pathname, template, permanent) {
107
+ if (locale === void 0) { locale = ""; }
108
+ template = transformRoutePathname(template, ":slug");
109
+ pathname = transformRoutePathname(pathname, ":slug");
110
+ var source = "/".concat(normaliseUrlPathname((locale ? "/".concat(locale, "/") : "/") + template));
111
+ var destination = "/".concat(normaliseUrlPathname(pathname));
112
+ // console.log(`redirect template "${source}" to pathname "${destination}"`);
72
113
  return {
73
114
  source: source,
74
115
  destination: destination,
116
+ permanent: Boolean(permanent),
117
+ locale: false,
75
118
  };
76
119
  }
77
120
  /**
78
121
  */
79
- export function getRedirects(_a) {
80
- var defaultLocale = _a.defaultLocale, routes = _a.routes, dynamicRoutes = _a.dynamicRoutes, permanent = _a.permanent;
122
+ export function getRedirects(defaultLocale, routes, permanent, debug) {
81
123
  return __awaiter(this, void 0, void 0, function () {
82
- var redirects;
83
- return __generator(this, function (_b) {
124
+ var redirects, routesMap;
125
+ return __generator(this, function (_a) {
84
126
  redirects = [];
85
- Object.keys(routes).forEach(function (page) {
86
- var dynamic = dynamicRoutes[page];
87
- if (routes[page] !== page) {
88
- if (dynamic) {
89
- redirects.push(getPathRedirect(defaultLocale, page, routes[page], true, permanent));
90
- }
91
- else {
92
- redirects.push(getPathRedirect(defaultLocale, page, routes[page], false, permanent));
93
- }
127
+ routesMap = getRoutesMap({}, routes);
128
+ Object.keys(routesMap).forEach(function (template) {
129
+ var pathname = routesMap[template];
130
+ if (pathname !== template) {
131
+ redirects.push(getPathRedirect(defaultLocale, pathname, template, permanent));
132
+ redirects.push(getPathRedirect("", pathname, template, permanent));
94
133
  }
95
134
  });
96
- // console.log("redirects", redirects);
135
+ if (debug)
136
+ console.log("redirects", redirects);
97
137
  return [2 /*return*/, redirects];
98
138
  });
99
139
  });
100
140
  }
101
141
  /**
102
142
  */
103
- export function getRewrites(_a) {
104
- var routes = _a.routes, dynamicRoutes = _a.dynamicRoutes;
143
+ export function getRewrites(routes, debug) {
105
144
  return __awaiter(this, void 0, void 0, function () {
106
- var rewrites;
107
- return __generator(this, function (_b) {
145
+ var rewrites, routesMap;
146
+ return __generator(this, function (_a) {
108
147
  rewrites = [];
109
- Object.keys(routes).forEach(function (page) {
110
- var dynamic = dynamicRoutes[page];
111
- if (routes[page] !== page) {
112
- if (dynamic) {
113
- rewrites.push(getPathRewrite(routes[page], page, true));
114
- }
115
- else {
116
- rewrites.push(getPathRewrite(routes[page], page));
117
- }
148
+ routesMap = getRoutesMap({}, routes);
149
+ Object.keys(routesMap).forEach(function (template) {
150
+ var pathname = routesMap[template];
151
+ if (pathname !== template) {
152
+ rewrites.push(getPathRewrite(pathname, template));
118
153
  }
119
154
  });
120
- // console.log("rewrites", rewrites);
155
+ if (debug)
156
+ console.log("rewrites", rewrites);
121
157
  return [2 /*return*/, rewrites];
122
158
  });
123
159
  });
@@ -134,10 +170,12 @@ export function getRewrites(_a) {
134
170
  * and it enables the same for `next-translate`.
135
171
  */
136
172
  export function withKoine(_a) {
137
- var _b;
138
- if (_a === void 0) { _a = {}; }
139
- var _c = _a.nx, nx = _c === void 0 ? true : _c, _d = _a.svg, svg = _d === void 0 ? true : _d, _e = _a.sc, sc = _e === void 0 ? true : _e, page = _a.page, nextConfig = __rest(_a, ["nx", "svg", "sc", "page"]);
140
- nextConfig = __assign({
173
+ var _b, _c;
174
+ if (_a === void 0) { _a = {
175
+ i18n: { locales: ["en"], defaultLocale: "en" },
176
+ }; }
177
+ var _d = _a.nx, nx = _d === void 0 ? true : _d, _e = _a.svg, svg = _e === void 0 ? true : _e, _f = _a.sc, sc = _f === void 0 ? true : _f, page = _a.page, routes = _a.routes, permanent = _a.permanent, debug = _a.debug, custom = __rest(_a, ["nx", "svg", "sc", "page", "routes", "permanent", "debug"]);
178
+ var nextConfig = __assign({
141
179
  // @see https://nextjs.org/docs/api-reference/next.config.js/custom-page-extensions#including-non-page-files-in-the-pages-directory
142
180
  pageExtensions: page ? ["page.tsx", "page.ts"] : undefined, eslint: {
143
181
  ignoreDuringBuilds: true, // we have this strict check on each commit
@@ -148,9 +186,9 @@ export function withKoine(_a) {
148
186
  // @see critters error https://github.com/vercel/next.js/issues/20742
149
187
  // optimizeCss: true,
150
188
  // @see https://github.com/vercel/next.js/discussions/30174#discussion-3643870
151
- scrollRestoration: true }, (nextConfig.experimental || {})), {
189
+ scrollRestoration: true }, (custom["experimental"] || {})), {
152
190
  // @see https://nextjs.org/docs/advanced-features/compiler#modularize-imports
153
- modularizeImports: __assign(__assign({}, (((_b = nextConfig === null || nextConfig === void 0 ? void 0 : nextConfig.experimental) === null || _b === void 0 ? void 0 : _b.modularizeImports) || {})), {
191
+ modularizeImports: __assign(__assign({}, (((_b = custom === null || custom === void 0 ? void 0 : custom["experimental"]) === null || _b === void 0 ? void 0 : _b.modularizeImports) || {})), {
154
192
  // FIXME: make these work with the right file/folder structure?
155
193
  // "@koine/next/?(((\\w*)?/?)*)": {
156
194
  // transform: "@koine/next/{{ matches.[1] }}/{{member}}",
@@ -163,7 +201,7 @@ export function withKoine(_a) {
163
201
  // },
164
202
  "@koine/utils": { transform: "@koine/utils/{{member}}" } }) }),
165
203
  // @see https://github.com/vercel/next.js/issues/7322#issuecomment-887330111
166
- reactStrictMode: true }, nextConfig);
204
+ reactStrictMode: true }, custom);
167
205
  if (svg) {
168
206
  if (nx) {
169
207
  // @see https://github.com/gregberge/svgr
@@ -204,6 +242,56 @@ export function withKoine(_a) {
204
242
  styledComponents: true,
205
243
  };
206
244
  }
245
+ if (routes) {
246
+ // we pass the default values, so we can assert I guess
247
+ var defaultLocale_1 = (_c = nextConfig === null || nextConfig === void 0 ? void 0 : nextConfig.i18n) === null || _c === void 0 ? void 0 : _c.defaultLocale;
248
+ return __assign(__assign({}, nextConfig), { redirects: function () {
249
+ return __awaiter(this, void 0, void 0, function () {
250
+ var defaults, customs;
251
+ return __generator(this, function (_a) {
252
+ switch (_a.label) {
253
+ case 0: return [4 /*yield*/, getRedirects(defaultLocale_1, routes, permanent, debug)];
254
+ case 1:
255
+ defaults = _a.sent();
256
+ if (!nextConfig.redirects) return [3 /*break*/, 3];
257
+ return [4 /*yield*/, nextConfig.redirects()];
258
+ case 2:
259
+ customs = _a.sent();
260
+ return [2 /*return*/, __spreadArray(__spreadArray([], defaults, true), customs, true)];
261
+ case 3: return [2 /*return*/, defaults];
262
+ }
263
+ });
264
+ });
265
+ }, rewrites: function () {
266
+ return __awaiter(this, void 0, void 0, function () {
267
+ var defaults, customs;
268
+ return __generator(this, function (_a) {
269
+ switch (_a.label) {
270
+ case 0: return [4 /*yield*/, getRewrites(routes, debug)];
271
+ case 1:
272
+ defaults = _a.sent();
273
+ if (!nextConfig.rewrites) return [3 /*break*/, 3];
274
+ return [4 /*yield*/, nextConfig.rewrites()];
275
+ case 2:
276
+ customs = _a.sent();
277
+ if (Array.isArray(customs)) {
278
+ return [2 /*return*/, {
279
+ beforeFiles: defaults,
280
+ afterFiles: customs,
281
+ fallback: [],
282
+ }];
283
+ }
284
+ return [2 /*return*/, __assign(__assign({}, customs), { beforeFiles: __spreadArray(__spreadArray([], defaults, true), (customs.beforeFiles || []), true) })];
285
+ case 3: return [2 /*return*/, {
286
+ afterFiles: [],
287
+ beforeFiles: defaults,
288
+ fallback: [],
289
+ }];
290
+ }
291
+ });
292
+ });
293
+ } });
294
+ }
207
295
  return nextConfig;
208
296
  }
209
297
  export default withKoine;
@@ -44,9 +44,7 @@ var Document = /** @class */ (function (_super) {
44
44
  return [4 /*yield*/, NextDocument.getInitialProps(ctx)];
45
45
  case 2:
46
46
  initialProps = _a.sent();
47
- return [2 /*return*/, __assign(__assign({}, initialProps), {
48
- // @ts-expect-error FIXME: have they changed type?
49
- styles: (_jsxs(React.Fragment, { children: [initialProps.styles, sheet.getStyleElement()] })) })];
47
+ return [2 /*return*/, __assign(__assign({}, initialProps), { styles: (_jsxs(React.Fragment, { children: [initialProps.styles, sheet.getStyleElement()] })) })];
50
48
  case 3:
51
49
  sheet.seal();
52
50
  return [7 /*endfinally*/];
@@ -1,7 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.withKoine = exports.getRewrites = exports.getRedirects = exports.getPathRewrite = exports.getPathRedirect = exports.encodePathname = exports.normaliseUrlPathname = exports.toPath = void 0;
3
+ exports.withKoine = exports.getRewrites = exports.getRedirects = exports.getPathRedirect = exports.getPathRewrite = exports.encodePathname = exports.toPath = exports.normaliseUrlPathname = void 0;
4
4
  var tslib_1 = require("tslib");
5
+ /**
6
+ * Normalise pathname
7
+ *
8
+ * From a path like `/some//malformed/path///` it returns `some/malformed/path`
9
+ *
10
+ * - Removes subsequent slashes
11
+ * - Removing initial and ending slashes
12
+ * - Returns an empty string `"""` if only slashes are given
13
+ */
14
+ function normaliseUrlPathname(pathname) {
15
+ if (pathname === void 0) { pathname = ""; }
16
+ // with return pathname.replace(/\/+\//g, "/").replace(/^\/+(.*?)\/+$/, "$1");
17
+ // we would instead return a single slash if only slashes are given
18
+ return pathname.replace(/\/+\//g, "/").replace(/^\/*(.*?)\/*$/, "$1");
19
+ }
20
+ exports.normaliseUrlPathname = normaliseUrlPathname;
5
21
  /**
6
22
  * Transform to path any absolute or relative URL
7
23
  *
@@ -10,9 +26,7 @@ var tslib_1 = require("tslib");
10
26
  *
11
27
  * From a path like `http://localhost/some//malformed/path///` it returns `/some/malformed/path`
12
28
  *
13
- * - just get the pathname form an absolute URL (if that is given)
14
- * - Removes subsequent slashes
15
- * - Removing initial and ending slashes
29
+ * @see {@link normaliseUrlPathname}
16
30
  */
17
31
  function toPath(urlOrPathname) {
18
32
  if (urlOrPathname === void 0) { urlOrPathname = ""; }
@@ -24,22 +38,11 @@ function toPath(urlOrPathname) {
24
38
  catch (e) {
25
39
  pathname = urlOrPathname;
26
40
  }
27
- return pathname.replace(/\/+\//g, "/").replace(/^\/+(.*?)\/+$/, "$1");
41
+ // with return pathname.replace(/\/+\//g, "/").replace(/^\/+(.*?)\/+$/, "$1");
42
+ // we would instead return a single slash if only slashes are given
43
+ return pathname.replace(/\/+\//g, "/").replace(/^\/*(.*?)\/*$/, "$1");
28
44
  }
29
45
  exports.toPath = toPath;
30
- /**
31
- * Normalise pathname
32
- *
33
- * From a path like `/some//malformed/path///` it returns `some/malformed/path`
34
- *
35
- * - Removes subsequent slashes
36
- * - Removing initial and ending slashes
37
- */
38
- function normaliseUrlPathname(pathname) {
39
- if (pathname === void 0) { pathname = ""; }
40
- return pathname.replace(/\/+\//g, "/").replace(/^\/+(.*?)\/+$/, "$1");
41
- }
42
- exports.normaliseUrlPathname = normaliseUrlPathname;
43
46
  /**
44
47
  * Clean a pathname and encode each part
45
48
  *
@@ -55,53 +58,90 @@ function encodePathname(pathname) {
55
58
  }
56
59
  exports.encodePathname = encodePathname;
57
60
  /**
61
+ * It replaces `/[dynamic-slug]/` with the given replacer.
62
+ */
63
+ function transformRoutePathname(rawPathname, replacer) {
64
+ var pathNameParts = rawPathname.split("/").filter(function (part) { return !!part; });
65
+ return pathNameParts
66
+ .map(function (part, _idx) {
67
+ var isDynamic = part.startsWith("[") && part.endsWith("]");
68
+ part = isDynamic ? replacer : part;
69
+ // if (isDynamic && _idx === pathNameParts.length - 1) {
70
+ // part += "*";
71
+ // }
72
+ return isDynamic ? part : encodeURIComponent(part);
73
+ })
74
+ .join("/");
75
+ }
76
+ /**
77
+ * Get flat key/value routes map dictionary
78
+ */
79
+ function getRoutesMap(map, routes, pathnameBuffer, templateBuffer) {
80
+ if (map === void 0) { map = {}; }
81
+ if (pathnameBuffer === void 0) { pathnameBuffer = ""; }
82
+ if (templateBuffer === void 0) { templateBuffer = ""; }
83
+ for (var key in routes) {
84
+ var pathOrNestedRoutes = routes[key];
85
+ var template = "".concat(templateBuffer, "/").concat(key);
86
+ if (typeof pathOrNestedRoutes === "string") {
87
+ map[template] = pathOrNestedRoutes;
88
+ }
89
+ else {
90
+ getRoutesMap(map, pathOrNestedRoutes, pathnameBuffer, template);
91
+ }
92
+ }
93
+ return map;
94
+ }
95
+ /**
96
+ * Get path rewrite
58
97
  */
59
- function getPathRedirect(locale, pathname, template, dynamic, permanent) {
60
- var suffix = dynamic ? "/:slug*" : "";
61
- var source = "/".concat(locale ? locale + "/" : "").concat(encodePathname(pathname)).concat(suffix);
62
- var destination = "/".concat(encodePathname(template)).concat(suffix);
63
- // console.log(`redirect pathname "${source}" to template "${destination}"`);
98
+ function getPathRewrite(pathname, template) {
99
+ pathname = transformRoutePathname(pathname, ":path");
100
+ template = transformRoutePathname(template, ":path");
101
+ var source = "/".concat(normaliseUrlPathname(pathname));
102
+ var destination = "/".concat(normaliseUrlPathname(template));
103
+ // console.log(`rewrite pathname "${source}" to template "${destination}"`);
64
104
  return {
65
105
  source: source,
66
106
  destination: destination,
67
- permanent: Boolean(permanent),
68
- locale: false,
69
107
  };
70
108
  }
71
- exports.getPathRedirect = getPathRedirect;
109
+ exports.getPathRewrite = getPathRewrite;
72
110
  /**
111
+ * Get path redirect
73
112
  */
74
- function getPathRewrite(pathname, template, dynamic) {
75
- var suffix = dynamic ? "/:path*" : "";
76
- var source = "/".concat(encodePathname(pathname)).concat(suffix);
77
- var destination = "/".concat(encodePathname(template)).concat(suffix);
78
- // console.log(`rewriting pathname "${source}" to template "${destination}"`);
113
+ function getPathRedirect(locale, pathname, template, permanent) {
114
+ if (locale === void 0) { locale = ""; }
115
+ template = transformRoutePathname(template, ":slug");
116
+ pathname = transformRoutePathname(pathname, ":slug");
117
+ var source = "/".concat(normaliseUrlPathname((locale ? "/".concat(locale, "/") : "/") + template));
118
+ var destination = "/".concat(normaliseUrlPathname(pathname));
119
+ // console.log(`redirect template "${source}" to pathname "${destination}"`);
79
120
  return {
80
121
  source: source,
81
122
  destination: destination,
123
+ permanent: Boolean(permanent),
124
+ locale: false,
82
125
  };
83
126
  }
84
- exports.getPathRewrite = getPathRewrite;
127
+ exports.getPathRedirect = getPathRedirect;
85
128
  /**
86
129
  */
87
- function getRedirects(_a) {
88
- var defaultLocale = _a.defaultLocale, routes = _a.routes, dynamicRoutes = _a.dynamicRoutes, permanent = _a.permanent;
130
+ function getRedirects(defaultLocale, routes, permanent, debug) {
89
131
  return tslib_1.__awaiter(this, void 0, void 0, function () {
90
- var redirects;
91
- return tslib_1.__generator(this, function (_b) {
132
+ var redirects, routesMap;
133
+ return tslib_1.__generator(this, function (_a) {
92
134
  redirects = [];
93
- Object.keys(routes).forEach(function (page) {
94
- var dynamic = dynamicRoutes[page];
95
- if (routes[page] !== page) {
96
- if (dynamic) {
97
- redirects.push(getPathRedirect(defaultLocale, page, routes[page], true, permanent));
98
- }
99
- else {
100
- redirects.push(getPathRedirect(defaultLocale, page, routes[page], false, permanent));
101
- }
135
+ routesMap = getRoutesMap({}, routes);
136
+ Object.keys(routesMap).forEach(function (template) {
137
+ var pathname = routesMap[template];
138
+ if (pathname !== template) {
139
+ redirects.push(getPathRedirect(defaultLocale, pathname, template, permanent));
140
+ redirects.push(getPathRedirect("", pathname, template, permanent));
102
141
  }
103
142
  });
104
- // console.log("redirects", redirects);
143
+ if (debug)
144
+ console.log("redirects", redirects);
105
145
  return [2 /*return*/, redirects];
106
146
  });
107
147
  });
@@ -109,24 +149,20 @@ function getRedirects(_a) {
109
149
  exports.getRedirects = getRedirects;
110
150
  /**
111
151
  */
112
- function getRewrites(_a) {
113
- var routes = _a.routes, dynamicRoutes = _a.dynamicRoutes;
152
+ function getRewrites(routes, debug) {
114
153
  return tslib_1.__awaiter(this, void 0, void 0, function () {
115
- var rewrites;
116
- return tslib_1.__generator(this, function (_b) {
154
+ var rewrites, routesMap;
155
+ return tslib_1.__generator(this, function (_a) {
117
156
  rewrites = [];
118
- Object.keys(routes).forEach(function (page) {
119
- var dynamic = dynamicRoutes[page];
120
- if (routes[page] !== page) {
121
- if (dynamic) {
122
- rewrites.push(getPathRewrite(routes[page], page, true));
123
- }
124
- else {
125
- rewrites.push(getPathRewrite(routes[page], page));
126
- }
157
+ routesMap = getRoutesMap({}, routes);
158
+ Object.keys(routesMap).forEach(function (template) {
159
+ var pathname = routesMap[template];
160
+ if (pathname !== template) {
161
+ rewrites.push(getPathRewrite(pathname, template));
127
162
  }
128
163
  });
129
- // console.log("rewrites", rewrites);
164
+ if (debug)
165
+ console.log("rewrites", rewrites);
130
166
  return [2 /*return*/, rewrites];
131
167
  });
132
168
  });
@@ -144,10 +180,12 @@ exports.getRewrites = getRewrites;
144
180
  * and it enables the same for `next-translate`.
145
181
  */
146
182
  function withKoine(_a) {
147
- var _b;
148
- if (_a === void 0) { _a = {}; }
149
- var _c = _a.nx, nx = _c === void 0 ? true : _c, _d = _a.svg, svg = _d === void 0 ? true : _d, _e = _a.sc, sc = _e === void 0 ? true : _e, page = _a.page, nextConfig = tslib_1.__rest(_a, ["nx", "svg", "sc", "page"]);
150
- nextConfig = tslib_1.__assign({
183
+ var _b, _c;
184
+ if (_a === void 0) { _a = {
185
+ i18n: { locales: ["en"], defaultLocale: "en" },
186
+ }; }
187
+ var _d = _a.nx, nx = _d === void 0 ? true : _d, _e = _a.svg, svg = _e === void 0 ? true : _e, _f = _a.sc, sc = _f === void 0 ? true : _f, page = _a.page, routes = _a.routes, permanent = _a.permanent, debug = _a.debug, custom = tslib_1.__rest(_a, ["nx", "svg", "sc", "page", "routes", "permanent", "debug"]);
188
+ var nextConfig = tslib_1.__assign({
151
189
  // @see https://nextjs.org/docs/api-reference/next.config.js/custom-page-extensions#including-non-page-files-in-the-pages-directory
152
190
  pageExtensions: page ? ["page.tsx", "page.ts"] : undefined, eslint: {
153
191
  ignoreDuringBuilds: true, // we have this strict check on each commit
@@ -158,9 +196,9 @@ function withKoine(_a) {
158
196
  // @see critters error https://github.com/vercel/next.js/issues/20742
159
197
  // optimizeCss: true,
160
198
  // @see https://github.com/vercel/next.js/discussions/30174#discussion-3643870
161
- scrollRestoration: true }, (nextConfig.experimental || {})), {
199
+ scrollRestoration: true }, (custom["experimental"] || {})), {
162
200
  // @see https://nextjs.org/docs/advanced-features/compiler#modularize-imports
163
- modularizeImports: tslib_1.__assign(tslib_1.__assign({}, (((_b = nextConfig === null || nextConfig === void 0 ? void 0 : nextConfig.experimental) === null || _b === void 0 ? void 0 : _b.modularizeImports) || {})), {
201
+ modularizeImports: tslib_1.__assign(tslib_1.__assign({}, (((_b = custom === null || custom === void 0 ? void 0 : custom["experimental"]) === null || _b === void 0 ? void 0 : _b.modularizeImports) || {})), {
164
202
  // FIXME: make these work with the right file/folder structure?
165
203
  // "@koine/next/?(((\\w*)?/?)*)": {
166
204
  // transform: "@koine/next/{{ matches.[1] }}/{{member}}",
@@ -173,7 +211,7 @@ function withKoine(_a) {
173
211
  // },
174
212
  "@koine/utils": { transform: "@koine/utils/{{member}}" } }) }),
175
213
  // @see https://github.com/vercel/next.js/issues/7322#issuecomment-887330111
176
- reactStrictMode: true }, nextConfig);
214
+ reactStrictMode: true }, custom);
177
215
  if (svg) {
178
216
  if (nx) {
179
217
  // @see https://github.com/gregberge/svgr
@@ -214,6 +252,56 @@ function withKoine(_a) {
214
252
  styledComponents: true,
215
253
  };
216
254
  }
255
+ if (routes) {
256
+ // we pass the default values, so we can assert I guess
257
+ var defaultLocale_1 = (_c = nextConfig === null || nextConfig === void 0 ? void 0 : nextConfig.i18n) === null || _c === void 0 ? void 0 : _c.defaultLocale;
258
+ return tslib_1.__assign(tslib_1.__assign({}, nextConfig), { redirects: function () {
259
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
260
+ var defaults, customs;
261
+ return tslib_1.__generator(this, function (_a) {
262
+ switch (_a.label) {
263
+ case 0: return [4 /*yield*/, getRedirects(defaultLocale_1, routes, permanent, debug)];
264
+ case 1:
265
+ defaults = _a.sent();
266
+ if (!nextConfig.redirects) return [3 /*break*/, 3];
267
+ return [4 /*yield*/, nextConfig.redirects()];
268
+ case 2:
269
+ customs = _a.sent();
270
+ return [2 /*return*/, tslib_1.__spreadArray(tslib_1.__spreadArray([], defaults, true), customs, true)];
271
+ case 3: return [2 /*return*/, defaults];
272
+ }
273
+ });
274
+ });
275
+ }, rewrites: function () {
276
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
277
+ var defaults, customs;
278
+ return tslib_1.__generator(this, function (_a) {
279
+ switch (_a.label) {
280
+ case 0: return [4 /*yield*/, getRewrites(routes, debug)];
281
+ case 1:
282
+ defaults = _a.sent();
283
+ if (!nextConfig.rewrites) return [3 /*break*/, 3];
284
+ return [4 /*yield*/, nextConfig.rewrites()];
285
+ case 2:
286
+ customs = _a.sent();
287
+ if (Array.isArray(customs)) {
288
+ return [2 /*return*/, {
289
+ beforeFiles: defaults,
290
+ afterFiles: customs,
291
+ fallback: [],
292
+ }];
293
+ }
294
+ return [2 /*return*/, tslib_1.__assign(tslib_1.__assign({}, customs), { beforeFiles: tslib_1.__spreadArray(tslib_1.__spreadArray([], defaults, true), (customs.beforeFiles || []), true) })];
295
+ case 3: return [2 /*return*/, {
296
+ afterFiles: [],
297
+ beforeFiles: defaults,
298
+ fallback: [],
299
+ }];
300
+ }
301
+ });
302
+ });
303
+ } });
304
+ }
217
305
  return nextConfig;
218
306
  }
219
307
  exports.withKoine = withKoine;
@@ -47,9 +47,7 @@ var Document = /** @class */ (function (_super) {
47
47
  return [4 /*yield*/, document_1.default.getInitialProps(ctx)];
48
48
  case 2:
49
49
  initialProps = _a.sent();
50
- return [2 /*return*/, tslib_1.__assign(tslib_1.__assign({}, initialProps), {
51
- // @ts-expect-error FIXME: have they changed type?
52
- styles: ((0, jsx_runtime_1.jsxs)(react_1.default.Fragment, { children: [initialProps.styles, sheet.getStyleElement()] })) })];
50
+ return [2 /*return*/, tslib_1.__assign(tslib_1.__assign({}, initialProps), { styles: ((0, jsx_runtime_1.jsxs)(react_1.default.Fragment, { children: [initialProps.styles, sheet.getStyleElement()] })) })];
53
51
  case 3:
54
52
  sheet.seal();
55
53
  return [7 /*endfinally*/];
package/package.json CHANGED
@@ -17,15 +17,11 @@
17
17
  "dependencies": {},
18
18
  "peerDependencies": {
19
19
  "react": "^16.8 || ^17 || ^18",
20
- "next": "^12.1.6",
21
- "@koine/utils": "1.0.41",
20
+ "next": "^12.2.0",
21
+ "@koine/utils": "1.0.44",
22
22
  "framer-motion": "^6.3.16",
23
- "next-auth": "^4.6.1",
24
- "@mui/material": "^5.8.6",
25
- "@emotion/react": "^11.9.3",
23
+ "@koine/react": "1.0.44",
26
24
  "styled-components": "^5.3.5",
27
- "react-hook-form": "^7.33.0",
28
- "@koine/react": "1.0.41",
29
25
  "@mui/base": "^5.0.0-alpha.87",
30
26
  "react-icons": "^4.4.0",
31
27
  "date-fns": "^2.28.0",
@@ -33,15 +29,19 @@
33
29
  "@tiptap/react": "^2.0.0-beta.114",
34
30
  "@tiptap/starter-kit": "^2.0.0-beta.190",
35
31
  "@kuus/yup": "^1.0.0-beta.4",
32
+ "react-hook-form": "^7.33.0",
36
33
  "type-fest": "^2.14.0",
37
34
  "react-popper": "^2.3.0",
38
35
  "tslib": "^2.4.0",
36
+ "next-auth": "^4.7.0",
37
+ "@mui/material": "^5.8.6",
38
+ "@emotion/react": "^11.9.3",
39
39
  "@emotion/server": "^11.4.0",
40
40
  "@hookform/resolvers": "^2.9.3",
41
41
  "next-translate": "^1.4.0",
42
42
  "next-seo": "^5.4.0"
43
43
  },
44
- "version": "1.0.41",
44
+ "version": "1.0.44",
45
45
  "module": "./index.js",
46
46
  "types": "./index.d.ts"
47
47
  }