@akinon/next 1.34.0-rc.7 → 1.34.0-rc.9

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @akinon/next
2
2
 
3
+ ## 1.34.0-rc.9
4
+
5
+ ### Minor Changes
6
+
7
+ - beb499e: ZERO-2551: Add new tsconfig paths
8
+
9
+ ## 1.34.0-rc.8
10
+
11
+ ### Minor Changes
12
+
13
+ - 91265bb: ZERO-2551: Improve pretty url and caching performance
14
+
3
15
  ## 1.34.0-rc.7
4
16
 
5
17
  ## 1.34.0-rc.6
@@ -3,4 +3,4 @@
3
3
  const runScript = require('./run-script');
4
4
 
5
5
  runScript('pz-install-theme.js');
6
- runScript('pz-pre-check-dist.js');
6
+ // runScript('pz-pre-check-dist.js');
@@ -8,7 +8,7 @@ import logger from '../../utils/log';
8
8
 
9
9
  function getCategoryDataHandler(
10
10
  pk: number,
11
- searchParams?: URLSearchParams,
11
+ searchParams?: { [key: string]: string | string[] | undefined },
12
12
  headers?: Record<string, string>
13
13
  ) {
14
14
  return async function () {
@@ -78,7 +78,7 @@ export const getCategoryData = ({
78
78
  headers
79
79
  }: {
80
80
  pk: number;
81
- searchParams?: URLSearchParams;
81
+ searchParams?: { [key: string]: string | string[] | undefined };
82
82
  headers?: Record<string, string>;
83
83
  }) => {
84
84
  return Cache.wrap(
@@ -7,7 +7,7 @@ import { parse } from 'lossless-json';
7
7
  import logger from '../../utils/log';
8
8
 
9
9
  const getListDataHandler = (
10
- searchParams: URLSearchParams,
10
+ searchParams: { [key: string]: string | string[] | undefined },
11
11
  headers?: Record<string, string>
12
12
  ) => {
13
13
  return async function () {
@@ -54,7 +54,7 @@ export const getListData = async ({
54
54
  searchParams,
55
55
  headers
56
56
  }: {
57
- searchParams: URLSearchParams;
57
+ searchParams: { [key: string]: string | string[] | undefined };
58
58
  headers?: Record<string, string>;
59
59
  }) => {
60
60
  return Cache.wrap(
@@ -9,7 +9,7 @@ import appFetch from '../../utils/app-fetch';
9
9
 
10
10
  type GetProduct = {
11
11
  pk: number;
12
- searchParams?: URLSearchParams;
12
+ searchParams?: { [key: string]: string | string[] | undefined };
13
13
  groupProduct?: boolean;
14
14
  };
15
15
 
@@ -74,10 +74,7 @@ export const getProductData = async ({
74
74
  groupProduct
75
75
  }: GetProduct) => {
76
76
  return Cache.wrap(
77
- CacheKey[groupProduct ? 'GroupProduct' : 'Product'](
78
- pk,
79
- searchParams ?? new URLSearchParams()
80
- ),
77
+ CacheKey[groupProduct ? 'GroupProduct' : 'Product'](pk, searchParams ?? {}),
81
78
  getProductDataHandler({ pk, searchParams, groupProduct }),
82
79
  {
83
80
  expire: 300
@@ -7,7 +7,7 @@ import header from '../../redux/reducers/header';
7
7
 
8
8
  const getSpecialPageDataHandler = (
9
9
  pk: number,
10
- searchParams: URLSearchParams,
10
+ searchParams: { [key: string]: string | string[] | undefined },
11
11
  headers?: Record<string, string>
12
12
  ) => {
13
13
  return async function () {
@@ -34,7 +34,7 @@ export const getSpecialPageData = async ({
34
34
  headers
35
35
  }: {
36
36
  pk: number;
37
- searchParams: URLSearchParams;
37
+ searchParams: { [key: string]: string | string[] | undefined };
38
38
  headers?: Record<string, string>;
39
39
  }) => {
40
40
  return Cache.wrap(
@@ -0,0 +1,33 @@
1
+ import { CacheHandler } from '@neshca/cache-handler';
2
+ import createLruHandler from '@neshca/cache-handler/local-lru';
3
+ import createRedisHandler from '@neshca/cache-handler/redis-stack';
4
+ import { createClient } from 'redis';
5
+
6
+ CacheHandler.onCreation(async () => {
7
+ const redisUrl = `redis://${process.env.CACHE_HOST}:${
8
+ process.env.CACHE_PORT
9
+ }/${process.env.CACHE_BUCKET ?? '0'}`;
10
+
11
+ const client = createClient({
12
+ url: redisUrl
13
+ });
14
+
15
+ client.on('error', (error) => {
16
+ console.error('Redis client error', { redisUrl, error });
17
+ });
18
+
19
+ await client.connect();
20
+
21
+ const redisHandler = await createRedisHandler({
22
+ client,
23
+ timeoutMs: 5000
24
+ });
25
+
26
+ const localHandler = createLruHandler();
27
+
28
+ return {
29
+ handlers: [redisHandler, localHandler]
30
+ };
31
+ });
32
+
33
+ export default CacheHandler;
package/lib/cache.ts CHANGED
@@ -20,13 +20,16 @@ const hashCacheKey = (object?: Record<string, string>) => {
20
20
  return `_${encodeURIComponent(cacheKey)}`;
21
21
  };
22
22
  export const CacheKey = {
23
- List: (searchParams: URLSearchParams, headers?: Record<string, string>) =>
23
+ List: (
24
+ searchParams: { [key: string]: string | string[] | undefined },
25
+ headers?: Record<string, string>
26
+ ) =>
24
27
  `list_${encodeURIComponent(JSON.stringify(searchParams))}${hashCacheKey(
25
28
  headers
26
29
  )}`,
27
30
  Category: (
28
31
  pk: number,
29
- searchParams?: URLSearchParams,
32
+ searchParams?: { [key: string]: string | string[] | undefined },
30
33
  headers?: Record<string, string>
31
34
  ) =>
32
35
  `category_${pk}_${encodeURIComponent(
@@ -35,15 +38,20 @@ export const CacheKey = {
35
38
  CategorySlug: (slug: string) => `category_${slug}`,
36
39
  SpecialPage: (
37
40
  pk: number,
38
- searchParams: URLSearchParams,
41
+ searchParams: { [key: string]: string | string[] | undefined },
39
42
  headers?: Record<string, string>
40
43
  ) =>
41
44
  `special_page_${pk}_${encodeURIComponent(
42
45
  JSON.stringify(searchParams)
43
46
  )}${hashCacheKey(headers)}`,
44
- Product: (pk: number, searchParams: URLSearchParams) =>
45
- `product_${pk}_${encodeURIComponent(JSON.stringify(searchParams))}`,
46
- GroupProduct: (pk: number, searchParams: URLSearchParams) =>
47
+ Product: (
48
+ pk: number,
49
+ searchParams: { [key: string]: string | string[] | undefined }
50
+ ) => `product_${pk}_${encodeURIComponent(JSON.stringify(searchParams))}`,
51
+ GroupProduct: (
52
+ pk: number,
53
+ searchParams: { [key: string]: string | string[] | undefined }
54
+ ) =>
47
55
  `group_product_${pk}_${encodeURIComponent(JSON.stringify(searchParams))}`,
48
56
  FlatPage: (pk: number) => `flat_page_${pk}`,
49
57
  LandingPage: (pk: number) => `landing_page_${pk}`,
@@ -158,6 +166,10 @@ export class Cache {
158
166
  handler: () => Promise<T>,
159
167
  options?: CacheOptions
160
168
  ): Promise<T> {
169
+ if (Settings.usePrettyUrlRoute) {
170
+ return await handler();
171
+ }
172
+
161
173
  const requiredVariables = [
162
174
  process.env.CACHE_HOST,
163
175
  process.env.CACHE_PORT,
@@ -200,6 +200,15 @@ const withPzDefault =
200
200
  );
201
201
  }
202
202
 
203
+ if (
204
+ Settings.usePrettyUrlRoute &&
205
+ url.searchParams.toString().length > 0
206
+ ) {
207
+ url.pathname =
208
+ url.pathname +
209
+ `searchparams|${url.searchParams.toString()}`;
210
+ }
211
+
203
212
  Settings.rewrites.forEach((rewrite) => {
204
213
  url.pathname = url.pathname.replace(
205
214
  rewrite.source,
@@ -56,6 +56,10 @@ const resolvePrettyUrl = async (pathname: string, ip: string | null) => {
56
56
  const withPrettyUrl =
57
57
  (middleware: NextMiddleware) =>
58
58
  async (req: PzNextRequest, event: NextFetchEvent) => {
59
+ if (Settings.usePrettyUrlRoute) {
60
+ return middleware(req, event);
61
+ }
62
+
59
63
  const url = req.nextUrl.clone();
60
64
  const matchedLanguagePrefix = url.pathname.match(
61
65
  urlLocaleMatcherRegex
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@akinon/next",
3
3
  "description": "Core package for Project Zero Next",
4
- "version": "1.34.0-rc.7",
4
+ "version": "1.34.0-rc.9",
5
5
  "private": false,
6
6
  "license": "MIT",
7
7
  "bin": {
@@ -20,11 +20,12 @@
20
20
  "@opentelemetry/sdk-trace-node": "1.19.0",
21
21
  "@opentelemetry/semantic-conventions": "1.19.0",
22
22
  "@reduxjs/toolkit": "1.9.7",
23
+ "@neshca/cache-handler": "1.0.7",
23
24
  "cross-spawn": "7.0.3",
24
25
  "generic-pool": "3.9.0",
25
26
  "react-redux": "8.1.3",
26
27
  "react-string-replace": "1.1.1",
27
- "redis": "4.5.1",
28
+ "redis": "4.6.13",
28
29
  "semver": "7.5.4",
29
30
  "set-cookie-parser": "2.6.0"
30
31
  },
@@ -34,7 +35,7 @@
34
35
  "@typescript-eslint/eslint-plugin": "6.7.4",
35
36
  "@typescript-eslint/parser": "6.7.4",
36
37
  "eslint": "^8.14.0",
37
- "@akinon/eslint-plugin-projectzero": "1.34.0-rc.7",
38
+ "@akinon/eslint-plugin-projectzero": "1.34.0-rc.9",
38
39
  "eslint-config-prettier": "8.5.0"
39
40
  }
40
41
  }
@@ -0,0 +1,194 @@
1
+ import { URLS } from '@akinon/next/data/urls';
2
+ import { Metadata, PageProps } from '@akinon/next/types';
3
+ import logger from '@akinon/next/utils/log';
4
+ import { notFound } from 'next/navigation';
5
+
6
+ type PrettyUrlResult = {
7
+ matched: boolean;
8
+ path?: string;
9
+ pk?: number;
10
+ };
11
+
12
+ const resolvePrettyUrlHandler =
13
+ (pathname: string, ip: string | null) => async () => {
14
+ let results = [] as { old_path: string }[];
15
+ let prettyUrlResult: PrettyUrlResult = {
16
+ matched: false
17
+ };
18
+
19
+ try {
20
+ const requestUrl = URLS.misc.prettyUrls(`/${pathname}/`);
21
+
22
+ logger.debug(`Resolving pretty url`, { pathname, requestUrl, ip });
23
+
24
+ const start = Date.now();
25
+ const apiResponse = await fetch(requestUrl, {
26
+ next: {
27
+ revalidate: 0
28
+ }
29
+ });
30
+ const data = await apiResponse.json();
31
+ ({ results } = data);
32
+ const end = Date.now();
33
+ console.warn('Pretty url response time', end - start, requestUrl);
34
+
35
+ const matched = results.length > 0;
36
+ const [{ old_path: path } = { old_path: '' }] = results;
37
+ let pk;
38
+
39
+ if (matched) {
40
+ const pkRegex = /\/(\d+)\/$/;
41
+ const match = path.match(pkRegex);
42
+ pk = match ? parseInt(match[1]) : undefined;
43
+ }
44
+
45
+ prettyUrlResult = {
46
+ matched,
47
+ path,
48
+ pk
49
+ };
50
+
51
+ logger.trace('Pretty url result', { prettyUrlResult, ip });
52
+ } catch (error) {
53
+ logger.error('Error resolving pretty url', { error, pathname, ip });
54
+ }
55
+
56
+ return prettyUrlResult;
57
+ };
58
+
59
+ export async function generateMetadata({ params }: PageProps) {
60
+ let result: Metadata = {};
61
+ const { prettyurl } = params;
62
+ const pageSlug = prettyurl
63
+ .filter((x) => !x.startsWith('searchparams'))
64
+ .join('/');
65
+
66
+ const searchParams = Object.fromEntries(
67
+ new URLSearchParams(
68
+ decodeURIComponent(
69
+ prettyurl
70
+ .find((x) => x.startsWith('searchparams'))
71
+ ?.replace('searchparams%7C', '')
72
+ ) ?? {}
73
+ ).entries()
74
+ );
75
+
76
+ const prettyUrlResult = await resolvePrettyUrlHandler(pageSlug, null)();
77
+
78
+ if (!prettyUrlResult.matched) {
79
+ return notFound();
80
+ }
81
+
82
+ const commonProps = {
83
+ params: {
84
+ ...params,
85
+ pk: prettyUrlResult.pk
86
+ },
87
+ searchParams
88
+ };
89
+
90
+ try {
91
+ if (prettyUrlResult.path.startsWith('/product/')) {
92
+ await import('@product/[pk]/page').then(async (module) => {
93
+ result = await module['generateMetadata']?.(commonProps);
94
+ });
95
+ }
96
+
97
+ if (prettyUrlResult.path.startsWith('/group-product/')) {
98
+ await import('@group-product/[pk]/page').then(async (module) => {
99
+ result = await module['generateMetadata']?.(commonProps);
100
+ });
101
+ }
102
+
103
+ if (prettyUrlResult.path.startsWith('/category/')) {
104
+ await import('@category/[pk]/page').then(async (module) => {
105
+ result = await module['generateMetadata']?.(commonProps);
106
+ });
107
+ }
108
+
109
+ if (prettyUrlResult.path.startsWith('/special-page/')) {
110
+ await import('@special-page/[pk]/page').then(async (module) => {
111
+ result = await module['generateMetadata']?.(commonProps);
112
+ });
113
+ }
114
+
115
+ if (prettyUrlResult.path.startsWith('/flat-page/')) {
116
+ await import('@flat-page/[pk]/page').then(async (module) => {
117
+ result = await module['generateMetadata']?.(commonProps);
118
+ });
119
+ }
120
+ // eslint-disable-next-line no-empty
121
+ } catch (error) {}
122
+
123
+ return result;
124
+ }
125
+
126
+ export const dynamic = 'force-static';
127
+ export const revalidate = 300;
128
+
129
+ export default async function Page({ params }) {
130
+ const { prettyurl } = params;
131
+ const pageSlug = prettyurl
132
+ .filter((x) => !x.startsWith('searchparams'))
133
+ .join('/');
134
+
135
+ const urlSearchParams = new URLSearchParams(
136
+ decodeURIComponent(
137
+ prettyurl
138
+ .find((x) => x.startsWith('searchparams'))
139
+ ?.replace('searchparams%7C', '')
140
+ ) ?? {}
141
+ );
142
+
143
+ const searchParams = {};
144
+
145
+ for (const [key, value] of urlSearchParams.entries() as unknown as Array<
146
+ [string, string]
147
+ >) {
148
+ if (!searchParams[key]) {
149
+ searchParams[key] = [];
150
+ }
151
+ searchParams[key].push(value);
152
+ }
153
+
154
+ const result = await resolvePrettyUrlHandler(pageSlug, null)();
155
+
156
+ if (!result.matched) {
157
+ return notFound();
158
+ }
159
+
160
+ const commonProps = {
161
+ params: {
162
+ ...params,
163
+ pk: result.pk
164
+ },
165
+ searchParams
166
+ };
167
+
168
+ if (result.path.startsWith('/category/')) {
169
+ const CategoryPage = (await import('@category/[pk]/page')).default;
170
+ return <CategoryPage {...commonProps} />;
171
+ }
172
+
173
+ if (result.path.startsWith('/product/')) {
174
+ const ProductPage = (await import('@product/[pk]/page')).default;
175
+ return <ProductPage {...commonProps} />;
176
+ }
177
+
178
+ if (result.path.startsWith('/group-product/')) {
179
+ const GroupProduct = (await import('@group-product/[pk]/page')).default;
180
+ return <GroupProduct {...commonProps} />;
181
+ }
182
+
183
+ if (result.path.startsWith('/special-page/')) {
184
+ const SpecialPage = (await import('@special-page/[pk]/page')).default;
185
+ return <SpecialPage {...commonProps} />;
186
+ }
187
+
188
+ if (result.path.startsWith('/flat-page/')) {
189
+ const FlatPage = (await import('@flat-page/[pk]/page')).default;
190
+ return <FlatPage {...commonProps} />;
191
+ }
192
+
193
+ return null;
194
+ }
package/types/index.ts CHANGED
@@ -70,6 +70,7 @@ export interface Currency {
70
70
  }
71
71
 
72
72
  export interface Settings {
73
+ usePrettyUrlRoute?: boolean;
73
74
  commerceUrl: string;
74
75
  redis: {
75
76
  defaultExpirationTime: number;
@@ -214,7 +215,7 @@ export type Translations = { [key: string]: object };
214
215
 
215
216
  export interface PageProps<T = any> {
216
217
  params: T;
217
- searchParams: URLSearchParams;
218
+ searchParams: { [key: string]: string | string[] | undefined };
218
219
  }
219
220
 
220
221
  export interface LayoutProps<T = any> extends PageProps<T> {
@@ -41,7 +41,7 @@ const appFetch = async <T>(
41
41
  };
42
42
 
43
43
  init.next = {
44
- revalidate: 60
44
+ revalidate: Settings.usePrettyUrlRoute ? 0 : 60
45
45
  };
46
46
 
47
47
  logger.debug(`FETCH START ${url}`, { requestURL, init, ip });
@@ -1,11 +1,15 @@
1
1
  import logger from './log';
2
2
 
3
- export const generateCommerceSearchParams = (searchParams?: URLSearchParams) => {
3
+ export const generateCommerceSearchParams = (searchParams?: {
4
+ [key: string]: string | string[] | undefined;
5
+ }) => {
4
6
  if (!searchParams) {
5
7
  return null;
6
8
  }
7
9
 
8
- const urlSerchParams = new URLSearchParams(searchParams);
10
+ const urlSerchParams = new URLSearchParams(
11
+ searchParams as Record<string, string>
12
+ );
9
13
 
10
14
  Object.entries(searchParams).forEach(([key, value]) => {
11
15
  if (typeof value === 'object') {
package/with-pz-config.js CHANGED
@@ -8,6 +8,7 @@ const defaultConfig = {
8
8
  transpilePackages: ['@akinon/next', ...pzPlugins.map((p) => `@akinon/${p}`)],
9
9
  skipTrailingSlashRedirect: true,
10
10
  poweredByHeader: false,
11
+ cacheMaxMemorySize: 0,
11
12
  env: {
12
13
  NEXT_PUBLIC_SENTRY_DSN: process.env.SENTRY_DSN
13
14
  },
@@ -65,7 +66,12 @@ const defaultConfig = {
65
66
  }
66
67
  };
67
68
 
68
- const withPzConfig = (myNextConfig = {}) => {
69
+ const withPzConfig = (
70
+ myNextConfig = {},
71
+ options = {
72
+ useCacheHandler: false
73
+ }
74
+ ) => {
69
75
  let extendedConfig = deepMerge({}, defaultConfig, myNextConfig);
70
76
 
71
77
  const originalPzHeadersFunction = defaultConfig.headers;
@@ -91,6 +97,10 @@ const withPzConfig = (myNextConfig = {}) => {
91
97
  return [...pzRewrites, ...myRewrites];
92
98
  };
93
99
 
100
+ if (options.useCacheHandler) {
101
+ extendedConfig.cacheHandler = require.resolve('./lib/cache-handler.mjs');
102
+ }
103
+
94
104
  return extendedConfig;
95
105
  };
96
106