@enonic/nextjs-adapter 2.0.2 → 2.0.3

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/UrlProcessor.js CHANGED
@@ -1,5 +1,6 @@
1
- import { commonChars, fixDoubleSlashes, getLocaleProjectConfigs, RENDER_MODE } from './utils';
1
+ import { fixDoubleSlashes, getLocaleProjectConfigs, RENDER_MODE } from './utils';
2
2
  import { addBasePath } from 'next/dist/client/add-base-path';
3
+ import { parseUrl } from 'next/dist/shared/lib/router/utils/parse-url';
3
4
  export class UrlProcessor {
4
5
  static process(url, meta, serverSide = false) {
5
6
  if (this.startsWithHash(url) || !meta || (this.isAttachmentUrl(url)) && meta.renderMode === RENDER_MODE.NEXT) {
@@ -9,13 +10,25 @@ export class UrlProcessor {
9
10
  // - attachment urls in NEXT mode
10
11
  return url;
11
12
  }
13
+ let normalUrl;
14
+ if (this.isRelative(url)) {
15
+ normalUrl = url;
16
+ }
17
+ else {
18
+ // url is absolute, try to make it relative by striping apiUrl
19
+ // NB: will fail if content api is not on the same domain as enonic xp
20
+ const apiUrl = this.getApiUrl(meta);
21
+ normalUrl = this.stripApiUrl(url, apiUrl);
22
+ // if url is still absolute, return it as is
23
+ if (!this.isRelative(normalUrl)) {
24
+ return normalUrl;
25
+ }
26
+ }
12
27
  let result;
13
- const apiUrl = this.getApiUrl(meta);
14
- const strippedUrl = this.stripApiUrl(url, apiUrl);
15
28
  const baseUrl = (meta === null || meta === void 0 ? void 0 : meta.baseUrl) && (meta === null || meta === void 0 ? void 0 : meta.baseUrl) !== '/' ? meta.baseUrl : '';
16
29
  if (meta.renderMode === RENDER_MODE.NEXT) {
17
30
  // only add basePath and locale in next mode
18
- result = `/${strippedUrl}`;
31
+ result = `/${normalUrl}`;
19
32
  if (!this.isRelative(url) && meta.locale !== meta.defaultLocale) {
20
33
  // do not append locale to local assets (having relative urls)
21
34
  result = `/${meta.locale}${result}`;
@@ -26,7 +39,7 @@ export class UrlProcessor {
26
39
  }
27
40
  }
28
41
  else {
29
- result = `${baseUrl}/${strippedUrl}`;
42
+ result = `${baseUrl}/${normalUrl}`;
30
43
  }
31
44
  return fixDoubleSlashes(result);
32
45
  }
@@ -57,11 +70,28 @@ export class UrlProcessor {
57
70
  }
58
71
  static stripApiUrl(url, apiUrl) {
59
72
  // normalise localhost-127.0.0.1 if present in urls
60
- const normalUrl = url.replace(this.localhostPattern, '127.0.0.1');
61
- const normalApiUrl = apiUrl.replace(this.localhostPattern, '127.0.0.1');
62
- const common = commonChars(normalUrl, normalApiUrl, '/');
63
- const remaining = common.length > 0 ? normalUrl.substring(common.length) : normalUrl;
64
- return (remaining.length > 0 && remaining.charAt(0) === '/') ? remaining.substring(1) : remaining;
73
+ const normalUrl = parseUrl(url.replace(this.localhostPattern, '127.0.0.1'));
74
+ const normalApiUrl = parseUrl(apiUrl.replace(this.localhostPattern, '127.0.0.1'));
75
+ // WARNING! disregarding the protocol (http/https)
76
+ if (normalUrl.hostname !== normalApiUrl.hostname
77
+ || normalUrl.port !== normalApiUrl.port) {
78
+ // can't strip apiUrl if hostnames or ports are different
79
+ return url;
80
+ }
81
+ const urlPathCrumbs = normalUrl.pathname.split('/');
82
+ const apiUrlPathCrumbs = normalApiUrl.pathname.split('/');
83
+ for (let i = 0; i < Math.min(apiUrlPathCrumbs.length, urlPathCrumbs.length);) {
84
+ const urlPathCrumb = urlPathCrumbs[i];
85
+ const apiUrlPathCrumb = apiUrlPathCrumbs[i];
86
+ if (urlPathCrumb === apiUrlPathCrumb) {
87
+ urlPathCrumbs.shift();
88
+ apiUrlPathCrumbs.shift();
89
+ }
90
+ else {
91
+ i++;
92
+ }
93
+ }
94
+ return `/${urlPathCrumbs.join('/')}` + normalUrl.search + normalUrl.hash;
65
95
  }
66
96
  static isAttachmentUrl(url) {
67
97
  return this.IMG_ATMT_REGEXP.test(url);
@@ -808,6 +808,7 @@ export function fetchContentPathsForLocale(path, config, query = GET_STATIC_PATH
808
808
  query,
809
809
  variables: {
810
810
  path,
811
+ pathRegex: `/content${config.site}/*`,
811
812
  count,
812
813
  },
813
814
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enonic/nextjs-adapter",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "license": "Apache-2.0",
5
5
  "main": "index.js",
6
6
  "publishConfig": {
package/utils.d.ts CHANGED
@@ -37,7 +37,7 @@ export declare const DEFAULT_LOCALE_HEADER = "default-locale";
37
37
  * - media
38
38
  * - paths with '/_' in them
39
39
  */
40
- export declare const GET_STATIC_PATHS_QUERY = "query ($count: Int) {\n guillotine {\n queryDsl(\n first: $count,\n sort: {\n field: \"modifiedTime\",\n direction: DESC \n }\n query: {boolean: {mustNot: [\n {in: {field: \"type\", stringValues: [\"base:folder\", \"base:shortcut\"]}}\n {like: {field: \"type\", value: \"media:*\"}}\n {like: {field: \"_path\", value: \"*/_*\"}}\n ]}}\n ) {\n _name\n _path\n site {_name}\n }\n }\n}";
40
+ export declare const GET_STATIC_PATHS_QUERY = "query ($pathRegex: String!, $count: Int) {\n guillotine {\n queryDsl(\n first: $count,\n sort: {\n field: \"modifiedTime\",\n direction: DESC \n }\n query: {\n boolean: {\n mustNot: [\n {in: {field: \"type\", stringValues: [\"base:shortcut\", \"portal:fragment\", \"portal:template-folder\", \"portal:page-template\"]}}\n {like: {field: \"type\", value: \"media:*\"}}\n ]\n must: [\n {like: {field:\"_path\", value: $pathRegex}}\n ]\n }\n }\n ) {\n _name\n _path\n site {_name}\n }\n }\n}";
41
41
  export declare enum XP_REQUEST_TYPE {
42
42
  COMPONENT = "component",
43
43
  TYPE = "type",
package/utils.js CHANGED
@@ -40,7 +40,7 @@ export const DEFAULT_LOCALE_HEADER = 'default-locale';
40
40
  * - media
41
41
  * - paths with '/_' in them
42
42
  */
43
- export const GET_STATIC_PATHS_QUERY = `query ($count: Int) {
43
+ export const GET_STATIC_PATHS_QUERY = `query ($pathRegex: String!, $count: Int) {
44
44
  guillotine {
45
45
  queryDsl(
46
46
  first: $count,
@@ -48,11 +48,17 @@ export const GET_STATIC_PATHS_QUERY = `query ($count: Int) {
48
48
  field: "modifiedTime",
49
49
  direction: DESC
50
50
  }
51
- query: {boolean: {mustNot: [
52
- {in: {field: "type", stringValues: ["base:folder", "base:shortcut"]}}
53
- {like: {field: "type", value: "media:*"}}
54
- {like: {field: "_path", value: "*/_*"}}
55
- ]}}
51
+ query: {
52
+ boolean: {
53
+ mustNot: [
54
+ {in: {field: "type", stringValues: ["base:shortcut", "portal:fragment", "portal:template-folder", "portal:page-template"]}}
55
+ {like: {field: "type", value: "media:*"}}
56
+ ]
57
+ must: [
58
+ {like: {field:"_path", value: $pathRegex}}
59
+ ]
60
+ }
61
+ }
56
62
  ) {
57
63
  _name
58
64
  _path