@enonic/nextjs-adapter 0.13.0 → 1.0.0-RC2

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/utils.js CHANGED
@@ -12,9 +12,7 @@ export const APP_NAME_DASHED = (APP_NAME || '').replace(/\./g, '-');
12
12
  export const SITE_KEY = (process.env.SITE_KEY || process.env.NEXT_PUBLIC_SITE_KEY);
13
13
  // //////////////////////////////////////////////////////////////////////// Hardcode-able constants
14
14
  // URI parameter marking that a request is for a preview for CS. MUST MATCH THE VALUE OF 'FROM_XP_PARAM' on XP side.
15
- export const FROM_XP_PARAM = '__fromxp__';
16
15
  export const XP_BASE_URL_HEADER = 'xpbaseurl';
17
- export const COMPONENT_SUBPATH_HEADER = 'xp-component-path';
18
16
  export const RENDER_MODE_HEADER = 'content-studio-mode';
19
17
  export const JSESSIONID_HEADER = 'jsessionid';
20
18
  export const PORTAL_COMPONENT_ATTRIBUTE = 'data-portal-component-type';
@@ -49,31 +47,25 @@ export var XP_COMPONENT_TYPE;
49
47
  XP_COMPONENT_TYPE["FRAGMENT"] = "fragment";
50
48
  XP_COMPONENT_TYPE["PAGE"] = "page";
51
49
  })(XP_COMPONENT_TYPE || (XP_COMPONENT_TYPE = {}));
52
- /** Returns true if the context object (from next.js in [[...contentPath]].jsx ) stems from a request that comes from XP in a CS-preview, i.e. has the URI param FROM_XP_PARAM (defined as '__fromXp__' above).
53
- * False if no context, query or FROM_XP_PARAM param */
54
- export const isRequestFromXP = (context) => {
55
- return !!getXPRequestType(context);
56
- };
57
- export const getXPRequestType = (context) => {
58
- var _a;
59
- const headerValue = (((_a = context === null || context === void 0 ? void 0 : context.req) === null || _a === void 0 ? void 0 : _a.headers) || {})[FROM_XP_PARAM];
60
- const enumValue = XP_REQUEST_TYPE[headerValue === null || headerValue === void 0 ? void 0 : headerValue.toUpperCase()];
61
- return enumValue || XP_REQUEST_TYPE.PAGE; // need to have some defaults here in case of rendering without XP
62
- };
63
- const getRenderMode = (context) => {
50
+ export const getRenderMode = (context) => {
64
51
  var _a;
65
52
  const value = (((_a = context === null || context === void 0 ? void 0 : context.req) === null || _a === void 0 ? void 0 : _a.headers) || {})[RENDER_MODE_HEADER];
66
53
  const enumValue = RENDER_MODE[value === null || value === void 0 ? void 0 : value.toUpperCase()];
67
54
  return enumValue || RENDER_MODE[process.env.RENDER_MODE] || RENDER_MODE.NEXT;
68
55
  };
69
- const getSingleComponentPath = (context) => {
70
- var _a;
71
- return (((_a = context === null || context === void 0 ? void 0 : context.req) === null || _a === void 0 ? void 0 : _a.headers) || {})[COMPONENT_SUBPATH_HEADER];
72
- };
73
56
  export function getContentApiUrl(context) {
74
57
  const mode = getRenderMode(context);
75
58
  return mode === RENDER_MODE.NEXT ? CONTENT_API_MASTER : CONTENT_API_DRAFT;
76
59
  }
60
+ export function getJsessionHeaders(context) {
61
+ var _a;
62
+ const headers = {};
63
+ const jsessionid = (_a = context === null || context === void 0 ? void 0 : context.req) === null || _a === void 0 ? void 0 : _a.headers[JSESSIONID_HEADER];
64
+ if (jsessionid) {
65
+ headers['Cookie'] = `${JSESSIONID_HEADER}=${jsessionid}`;
66
+ }
67
+ return headers;
68
+ }
77
69
  /** For '<a href="..."' link values in props when clicking the link should navigate to an XP content item page
78
70
  * and the query returns the XP _path to the target content item:
79
71
  * When viewed directly, the header will have a `<base href='/' />` (see src/pages/_app.tsx), and when viewed through an
@@ -129,13 +121,8 @@ const adapterConstants = {
129
121
  APP_NAME_UNDERSCORED,
130
122
  APP_NAME_DASHED,
131
123
  SITE_KEY,
132
- FROM_XP_PARAM,
133
- COMPONENT_SUBPATH_HEADER,
134
124
  PORTAL_COMPONENT_ATTRIBUTE,
135
125
  PORTAL_REGION_ATTRIBUTE,
136
- getXPRequestType,
137
- getSingleComponentPath,
138
- getRenderMode,
139
126
  };
140
127
  // Verify required values
141
128
  const NOT_REQUIRED = ['IS_DEV_MODE'];
@@ -14,10 +14,17 @@ export function createReplacer(allData, meta, renderMacroInEditMode = true, cust
14
14
  switch (el.tagName) {
15
15
  case UrlProcessor.IMG_TAG:
16
16
  ref = el.attribs[UrlProcessor.IMG_ATTR];
17
- const src = el.attribs['src'];
18
- // do not process content images in next to keep it absolute
19
- if (ref && src && UrlProcessor.isContentImage(ref, allData.images)) {
20
- el.attribs['src'] = getUrl(src, meta);
17
+ if (ref) {
18
+ if (UrlProcessor.isContentImage(ref, allData.images)) {
19
+ const src = el.attribs['src'];
20
+ if (src) {
21
+ el.attribs['src'] = getUrl(src, meta);
22
+ }
23
+ const srcset = el.attribs['srcset'];
24
+ if (srcset) {
25
+ el.attribs['srcset'] = UrlProcessor.processSrcSet(srcset, meta);
26
+ }
27
+ }
21
28
  }
22
29
  break;
23
30
  case UrlProcessor.LINK_TAG: