@remkoj/optimizely-cms-nextjs 1.0.4 → 2.0.0-pre2
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/dist/cms-page/data.d.ts +18 -19
- package/dist/cms-page/data.js +12 -13
- package/dist/cms-page/data.js.map +1 -1
- package/dist/cms-page/index.d.ts +1 -1
- package/dist/cms-page/layout.d.ts +6 -5
- package/dist/cms-page/layout.js +7 -10
- package/dist/cms-page/layout.js.map +1 -1
- package/dist/cms-page/page.d.ts +33 -18
- package/dist/cms-page/page.js +65 -29
- package/dist/cms-page/page.js.map +1 -1
- package/dist/cms-page/utils.d.ts +11 -2
- package/dist/cms-page/utils.js +14 -4
- package/dist/cms-page/utils.js.map +1 -1
- package/dist/components/on-page-edit.js +27 -18
- package/dist/components/on-page-edit.js.map +1 -1
- package/dist/metadata.d.ts +1 -1
- package/dist/metadata.js +11 -7
- package/dist/metadata.js.map +1 -1
- package/dist/ope/data.js +13 -13
- package/dist/ope/data.js.map +1 -1
- package/dist/ope/page.d.ts +1 -3
- package/dist/ope/page.js +90 -66
- package/dist/ope/page.js.map +1 -1
- package/dist/ope/types.d.ts +32 -25
- package/dist/publish/index.d.ts +25 -8
- package/dist/publish/index.js +38 -30
- package/dist/publish/index.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/package.json +24 -19
package/dist/metadata.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import createClient, { isContentGraphClient } from '@remkoj/optimizely-graph-client';
|
|
2
2
|
import { isOptimizelyNextPageWithMetaData } from './page';
|
|
3
|
-
const DEBUG = process.env.DXP_DEBUG == '1';
|
|
4
3
|
export class MetaDataResolver {
|
|
5
4
|
_cgClient;
|
|
6
5
|
constructor(clientOrConfig) {
|
|
@@ -16,19 +15,24 @@ export class MetaDataResolver {
|
|
|
16
15
|
* @returns A Promise for the metadata of the given content type & instance
|
|
17
16
|
*/
|
|
18
17
|
async resolve(factory, contentLink, contentType, locale) {
|
|
19
|
-
if (
|
|
20
|
-
console.log(
|
|
21
|
-
if (locale.includes("-"))
|
|
22
|
-
throw new Error("Invalid character detected within the locale");
|
|
18
|
+
if (this._cgClient.debug)
|
|
19
|
+
console.log(`⚪ [MetaDataResolver] Resolving metadata for: ${JSON.stringify({ contentLink, contentType, locale })}`);
|
|
20
|
+
if (locale && locale.includes("-"))
|
|
21
|
+
throw new Error("🟠 [MetaDataResolver] Invalid character detected within the locale");
|
|
23
22
|
const Component = factory.resolve(contentType);
|
|
24
23
|
if (!Component)
|
|
25
24
|
return {};
|
|
26
25
|
if (isOptimizelyNextPageWithMetaData(Component) && Component.getMetaData) {
|
|
26
|
+
console.log("⚪ [MetaDataResolver] Component for content type has 'getMetaData, invoking...");
|
|
27
27
|
const meta = await Component.getMetaData(contentLink, locale, this._cgClient);
|
|
28
|
-
if (
|
|
29
|
-
console.log(
|
|
28
|
+
if (this._cgClient.debug)
|
|
29
|
+
console.log(`⚪ [MetaDataResolver] Resolved metadata to: ${JSON.stringify(meta)}`);
|
|
30
30
|
return meta;
|
|
31
31
|
}
|
|
32
|
+
else {
|
|
33
|
+
if (this._cgClient.debug)
|
|
34
|
+
console.warn(`🟠 [MetaDataResolver] Resolved component for ${JSON.stringify(contentType)} does provide additional metadata`);
|
|
35
|
+
}
|
|
32
36
|
return {};
|
|
33
37
|
}
|
|
34
38
|
}
|
package/dist/metadata.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metadata.js","sourceRoot":"","sources":["../src/metadata.ts"],"names":[],"mappings":"AACA,OAAO,YAAY,EAAE,EAAE,oBAAoB,EAAqD,MAAM,iCAAiC,CAAA;AAEvI,OAAO,EAAE,gCAAgC,EAAE,MAAM,QAAQ,CAAA;AAEzD,MAAM,
|
|
1
|
+
{"version":3,"file":"metadata.js","sourceRoot":"","sources":["../src/metadata.ts"],"names":[],"mappings":"AACA,OAAO,YAAY,EAAE,EAAE,oBAAoB,EAAqD,MAAM,iC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;gBACpB,OAAO,CAAC,IAAI,CAAC,gDAAiD,IAAI,CAAC,SAAS,CAAC,WAAW,CAAE,mCAAmC,CAAC,CAAA;QACtI,CAAC;QACD,OAAO,EAAE,CAAA;IACb,CAAC;CACJ;AAED,eAAe,gBAAgB,CAAA"}
|
package/dist/ope/data.js
CHANGED
|
@@ -2,26 +2,26 @@ import { gql } from 'graphql-request';
|
|
|
2
2
|
export const getContentById = async (client, variables) => {
|
|
3
3
|
return await client.request(gqlQuery, variables);
|
|
4
4
|
};
|
|
5
|
-
const gqlQuery = gql `query getContentByIdBase($
|
|
6
|
-
Content(
|
|
5
|
+
const gqlQuery = gql `query getContentByIdBase($key: String!, $version: String, $locale: [Locales!], $path: String, $domain: String) {
|
|
6
|
+
content: Content(
|
|
7
7
|
where: {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
_or: [
|
|
9
|
+
{ _metadata: { key: { eq: $key }, version: { eq: $version } } }
|
|
10
|
+
{ _metadata: { url: { hierarchical: { eq: $path }, base: { eq: $domain } }, version: { eq: $version } } }
|
|
11
|
+
]
|
|
10
12
|
}
|
|
11
13
|
locale: $locale
|
|
12
14
|
) {
|
|
13
15
|
total
|
|
14
16
|
items {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
_metadata {
|
|
18
|
+
key
|
|
19
|
+
locale
|
|
20
|
+
types
|
|
21
|
+
displayName
|
|
22
|
+
version
|
|
20
23
|
}
|
|
21
|
-
|
|
22
|
-
name: Name
|
|
23
|
-
}
|
|
24
|
-
path: RelativePath
|
|
24
|
+
_type: __typename
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
}`;
|
package/dist/ope/data.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data.js","sourceRoot":"","sources":["../../src/ope/data.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"data.js","sourceRoot":"","sources":["../../src/ope/data.ts"],"names":[],"mappings":"AACA,OAAO,EAAuB,GAAG,EAAE,MAAM,iBAAiB,CAAA;AAE1D,MAAM,CAAC,MAAM,cAAc,GAA0B,KAAK,EAAuB,MAAqB,EAAE,SAAwC,EAAE,EAAE;IAEhJ,OAAO,MAAM,MAAM,CAAC,OAAO,CAAgE,QAAQ,EAAE,SAAS,CAAC,CAAA;AACnH,CAAC,CAAA;AAED,MAAM,QAAQ,GAAG,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;EAsBlB,CAAA"}
|
package/dist/ope/page.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import 'server-only';
|
|
2
2
|
import type { EditPageComponent, EditViewOptions } from './types';
|
|
3
|
-
import { type Services } from '@remkoj/optimizely-graph-client';
|
|
4
3
|
import { type ComponentFactory } from '@remkoj/optimizely-cms-react/rsc';
|
|
5
4
|
/**
|
|
6
5
|
* Create the EditPage component needed by Next.JS to render the "On Page
|
|
@@ -8,10 +7,9 @@ import { type ComponentFactory } from '@remkoj/optimizely-cms-react/rsc';
|
|
|
8
7
|
*
|
|
9
8
|
* @param dxpUrl The domain of the CMS instance
|
|
10
9
|
* @param client The Apollo GraphQL client to use
|
|
11
|
-
* @param channel The static site definition to use
|
|
12
10
|
* @param factory The component factory to be used
|
|
13
11
|
* @param options The optional options to use to control the edit page
|
|
14
12
|
* @returns The React Component that can be used by Next.JS to render the page
|
|
15
13
|
*/
|
|
16
|
-
export declare function createEditPageComponent(
|
|
14
|
+
export declare function createEditPageComponent(factory: ComponentFactory, options?: Partial<EditViewOptions>): EditPageComponent;
|
|
17
15
|
export default createEditPageComponent;
|
package/dist/ope/page.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import 'server-only';
|
|
3
3
|
import { AuthMode } from '@remkoj/optimizely-graph-client';
|
|
4
|
+
import { contentLinkToString } from '@remkoj/optimizely-graph-client/utils';
|
|
4
5
|
import { Utils } from '@remkoj/optimizely-cms-react';
|
|
5
6
|
import { CmsContent, getServerContext } from '@remkoj/optimizely-cms-react/rsc';
|
|
6
7
|
import { notFound } from 'next/navigation';
|
|
@@ -9,129 +10,152 @@ import { getAuthorizedServerClient } from '../client';
|
|
|
9
10
|
import React from 'react';
|
|
10
11
|
import Script from 'next/script';
|
|
11
12
|
import { getContentById } from './data';
|
|
13
|
+
function readValue(variableName, defaultValue) {
|
|
14
|
+
try {
|
|
15
|
+
const stringValue = process?.env ? process.env[variableName] : undefined;
|
|
16
|
+
return stringValue || defaultValue;
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return defaultValue;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function readValueAsInt(variableName, defaultValue) {
|
|
23
|
+
const stringValue = readValue(variableName);
|
|
24
|
+
if (!stringValue)
|
|
25
|
+
return defaultValue;
|
|
26
|
+
try {
|
|
27
|
+
return parseInt(stringValue);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return defaultValue;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
12
33
|
const defaultOptions = {
|
|
13
34
|
refreshDelay: 2000,
|
|
14
35
|
refreshNotice: () => _jsx("div", { className: 'optly-refresh-notice', children: "Updating preview, please wait...." }),
|
|
15
36
|
errorNotice: props => _jsxs("div", { className: 'optly-error-notice', children: [_jsx("div", { className: 'optly-error-title', children: props.title }), _jsx("div", { className: 'optly-error-message', children: props.message })] }),
|
|
16
37
|
layout: props => _jsx("div", { className: 'optly-edit-page', "data-locale": props.locale, children: props.children }),
|
|
17
38
|
loader: getContentById,
|
|
18
|
-
clientFactory: (token) => getAuthorizedServerClient(token)
|
|
39
|
+
clientFactory: (token) => getAuthorizedServerClient(token),
|
|
40
|
+
communicationInjectorPath: '/util/javascript/communicationinjector.js'
|
|
19
41
|
};
|
|
42
|
+
// Helper function to read the ContentID & WorkID
|
|
43
|
+
function getContentRequest(path = "", searchParams) {
|
|
44
|
+
try {
|
|
45
|
+
if (searchParams.preview_token == AuthMode.HMAC || searchParams.preview_token == AuthMode.Basic) {
|
|
46
|
+
console.error("🦺 [OnPageEdit] Edit mode requested with developer tokens, falling back to URL parsing to determine content");
|
|
47
|
+
const fullPath = Array.isArray(path) ? path.map(p => decodeURIComponent(p)).join('/') : decodeURI(path);
|
|
48
|
+
const [cmsPath, contentString] = fullPath.split(',,', 3);
|
|
49
|
+
const [contentKey, contentVersion] = (contentString ?? '').split('_', 3);
|
|
50
|
+
const contentPath = (cmsPath.startsWith('/') ? cmsPath.substring(1) : cmsPath).replace(/^(ui\/){0,1}(CMS\/){0,1}(Content\/){0,1}/gi, "");
|
|
51
|
+
const firstSlug = contentPath.split('/')[0];
|
|
52
|
+
const contentLocale = firstSlug?.length == 2 || firstSlug?.length == 5 ? firstSlug : undefined;
|
|
53
|
+
return {
|
|
54
|
+
key: contentKey,
|
|
55
|
+
version: contentVersion,
|
|
56
|
+
locale: contentLocale,
|
|
57
|
+
path: '/' + contentPath + (contentPath.endsWith('/') || contentPath == '' ? '' : '/')
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const tokenInfo = JSON.parse(atob(searchParams.preview_token.split('.')[1]));
|
|
62
|
+
return {
|
|
63
|
+
key: tokenInfo.key,
|
|
64
|
+
version: tokenInfo.ver,
|
|
65
|
+
locale: tokenInfo.loc,
|
|
66
|
+
path: ''
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
20
74
|
/**
|
|
21
75
|
* Create the EditPage component needed by Next.JS to render the "On Page
|
|
22
76
|
* Editing" variant of the content item selected by the editor.
|
|
23
77
|
*
|
|
24
78
|
* @param dxpUrl The domain of the CMS instance
|
|
25
79
|
* @param client The Apollo GraphQL client to use
|
|
26
|
-
* @param channel The static site definition to use
|
|
27
80
|
* @param factory The component factory to be used
|
|
28
81
|
* @param options The optional options to use to control the edit page
|
|
29
82
|
* @returns The React Component that can be used by Next.JS to render the page
|
|
30
83
|
*/
|
|
31
|
-
export function createEditPageComponent(
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
async function EditPage({ params, searchParams }) {
|
|
84
|
+
export function createEditPageComponent(factory, options) {
|
|
85
|
+
const envRefreshDelay = readValueAsInt("OPTIMIZELY_GRAPH_UPDATE_DELAY", defaultOptions.refreshDelay);
|
|
86
|
+
const { layout: PageLayout, refreshNotice: RefreshNotice, refreshDelay, errorNotice: ErrorNotice, loader: getContentById, clientFactory, communicationInjectorPath } = { ...defaultOptions, refreshDelay: envRefreshDelay, ...options };
|
|
87
|
+
async function EditPage({ params: { path }, searchParams }) {
|
|
35
88
|
// Create context
|
|
36
89
|
const context = getServerContext();
|
|
37
90
|
// Validate the search parameters
|
|
38
91
|
const epiEditMode = searchParams?.epieditmode?.toLowerCase();
|
|
39
92
|
if (epiEditMode != 'true' && epiEditMode != 'false') {
|
|
40
|
-
console.error("[OnPageEdit] Edit mode requested without valid EpiEditMode, refused to render the page. Mode set:", searchParams.epieditmode);
|
|
93
|
+
console.error("🔴 [OnPageEdit] Edit mode requested without valid EpiEditMode, refused to render the page. Mode set:", searchParams.epieditmode);
|
|
41
94
|
return notFound();
|
|
42
95
|
}
|
|
43
96
|
// Allow use-hmac as magic token to be used only on a development environment, otherwise require a minimal length string as token
|
|
44
97
|
const token = searchParams.preview_token;
|
|
45
98
|
const validDev = context.isDevelopment && (searchParams.preview_token == AuthMode.HMAC || searchParams.preview_token == AuthMode.Basic);
|
|
46
99
|
if (!token || (token.length < 20 && !validDev)) {
|
|
47
|
-
console.error("[OnPageEdit] Edit mode requested without valid Preview Token, refused to render the page");
|
|
100
|
+
console.error("🔴 [OnPageEdit] Edit mode requested without valid Preview Token, refused to render the page");
|
|
48
101
|
return notFound();
|
|
49
102
|
}
|
|
50
103
|
if (context.isDebug)
|
|
51
|
-
console.log(
|
|
52
|
-
// Helper function to read the ContentID & WorkID
|
|
53
|
-
function getContentIds() {
|
|
54
|
-
try {
|
|
55
|
-
// When using HMAC authentication, fall back to information in the URL
|
|
56
|
-
if (validDev) {
|
|
57
|
-
const contentString = slugs.join('/').split(',,').pop() ?? '';
|
|
58
|
-
const [contentId, workId] = contentString.split('_', 3);
|
|
59
|
-
return [Number.parseInt(contentId), workId ? Number.parseInt(workId) : null];
|
|
60
|
-
}
|
|
61
|
-
// Normally take the information from the token
|
|
62
|
-
const jwt = JSON.parse(Buffer.from((token || '..').split('.', 3)[1], 'base64').toString());
|
|
63
|
-
const contentId = Number.parseInt(jwt?.c_id || '-1', 10);
|
|
64
|
-
const workId = Number.parseInt(jwt?.c_ver || '0') || null;
|
|
65
|
-
if ((jwt.exp * 1000) < Date.now())
|
|
66
|
-
console.warn("[OnPageEdit] Token has expired, it is unlikely that you are able to fetch content with it");
|
|
67
|
-
return [contentId, workId];
|
|
68
|
-
}
|
|
69
|
-
catch {
|
|
70
|
-
return [-1, null];
|
|
71
|
-
}
|
|
72
|
-
}
|
|
104
|
+
console.log(`⚪ [OnPageEdit] Valid edit mode request: EpiEditMode=${searchParams.epieditmode}`);
|
|
73
105
|
// Build context
|
|
74
106
|
const client = clientFactory(token);
|
|
75
107
|
context.setOptimizelyGraphClient(client);
|
|
76
108
|
context.setComponentFactory(factory);
|
|
77
109
|
context.setInEditMode(epiEditMode == 'true');
|
|
78
110
|
// Get information from the Request URI
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const [contentId, workId] = getContentIds();
|
|
85
|
-
context.setLocale(channel.localeToGraphLocale(locale));
|
|
86
|
-
const contentLink = {
|
|
87
|
-
id: contentId,
|
|
88
|
-
workId: workId,
|
|
89
|
-
guidValue: null,
|
|
90
|
-
locale: locale
|
|
91
|
-
};
|
|
92
|
-
const variables = {
|
|
93
|
-
...contentLink,
|
|
94
|
-
locale: contentLink.locale,
|
|
95
|
-
isCommonDraft: !contentLink.workId ? true : null
|
|
96
|
-
};
|
|
111
|
+
const contentRequest = getContentRequest(path, searchParams);
|
|
112
|
+
if (!contentRequest) {
|
|
113
|
+
console.error("🔴 [OnPageEdit] Unable to resolve requested content");
|
|
114
|
+
return notFound();
|
|
115
|
+
}
|
|
97
116
|
if (context.isDebug) {
|
|
98
|
-
console.log("[OnPageEdit] Requested content:", JSON.stringify(
|
|
99
|
-
console.log("[OnPageEdit] Creating GraphQL Client:", token);
|
|
117
|
+
console.log("⚪ [OnPageEdit] Requested content:", JSON.stringify(contentRequest));
|
|
118
|
+
console.log("⚪ [OnPageEdit] Creating GraphQL Client:", token);
|
|
100
119
|
}
|
|
101
120
|
try {
|
|
102
|
-
const contentInfo = await getContentById(client,
|
|
103
|
-
|
|
104
|
-
|
|
121
|
+
const contentInfo = await getContentById(client, contentRequest);
|
|
122
|
+
if ((contentInfo?.content?.total ?? 0) > 1) {
|
|
123
|
+
console.warn("🟠 [OnPageEdit] Content request " + JSON.stringify(contentRequest) + " yielded more then one item, picking first matching");
|
|
124
|
+
}
|
|
125
|
+
const contentItem = (contentInfo?.content?.items ?? [])[0];
|
|
126
|
+
const contentType = Utils.normalizeContentType(contentItem?._metadata.types);
|
|
105
127
|
// Return a 404 if the content item or type could not be resolved
|
|
106
128
|
if (!contentItem) {
|
|
107
|
-
console.warn(
|
|
129
|
+
console.warn(`🟠 [OnPageEdit] The content item for ${JSON.stringify(contentRequest)} could not be loaded from Optimizely Graph`);
|
|
108
130
|
return notFound();
|
|
109
131
|
}
|
|
110
132
|
if (!contentType) {
|
|
111
|
-
console.warn(
|
|
133
|
+
console.warn(`🟠 [OnPageEdit] The content item for ${JSON.stringify(contentRequest)} did not contain content type information`);
|
|
112
134
|
return notFound();
|
|
113
135
|
}
|
|
136
|
+
const contentLink = {
|
|
137
|
+
key: contentItem._metadata.key,
|
|
138
|
+
locale: contentItem._metadata.locale,
|
|
139
|
+
version: contentItem._metadata.version
|
|
140
|
+
};
|
|
114
141
|
if (context.isDebug) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
workId: contentItemId?.workId,
|
|
119
|
-
guidValue: contentItemId?.guidValue,
|
|
120
|
-
locale: contentItem.locale?.name,
|
|
121
|
-
type: (contentItem.contentType ?? []).slice(0, -1).join('/')
|
|
142
|
+
console.log("⚪ [OnPageEdit] Resolved content:", JSON.stringify({
|
|
143
|
+
...contentLink,
|
|
144
|
+
type: (contentItem.contentType ?? []).join('/')
|
|
122
145
|
}));
|
|
123
146
|
}
|
|
124
147
|
// Store the editable content so it can be tested
|
|
125
148
|
context.setEditableContentId(contentLink);
|
|
149
|
+
if (contentLink.locale)
|
|
150
|
+
context.setLocale(contentLink.locale);
|
|
126
151
|
// Render the content, with edit mode context
|
|
127
|
-
const isPage =
|
|
128
|
-
const loadedContentId = Utils.normalizeContentLinkWithLocale({ ...contentItem?.id, locale: contentItem?.locale?.name });
|
|
152
|
+
const isPage = contentType.some(x => x?.toLowerCase() == "page") ?? false;
|
|
129
153
|
const Layout = isPage ? PageLayout : React.Fragment;
|
|
130
|
-
const output = _jsxs(_Fragment, { children: [
|
|
154
|
+
const output = _jsxs(_Fragment, { children: [_jsx(Script, { src: new URL(communicationInjectorPath, client.siteInfo.cmsURL).href, strategy: 'afterInteractive' }), _jsxs(Layout, { locale: contentItem.locale?.name ?? '', children: [_jsx(OnPageEdit, { timeout: refreshDelay, mode: context.inEditMode ? 'edit' : 'preview', children: _jsx(RefreshNotice, {}) }), _jsx(CmsContent, { contentType: contentType, contentLink: contentLink, fragmentData: contentItem })] }), _jsxs("div", { className: 'optly-contentLink', children: ["ContentItem: ", contentLink ? contentLinkToString(contentLink) : "Invalid content link returned from Optimizely Graph"] })] });
|
|
131
155
|
return output;
|
|
132
156
|
}
|
|
133
157
|
catch (e) {
|
|
134
|
-
console.error("[OnPageEdit] Caught error", e);
|
|
158
|
+
console.error("🔴 [OnPageEdit] Caught error", e);
|
|
135
159
|
return notFound();
|
|
136
160
|
}
|
|
137
161
|
}
|
package/dist/ope/page.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"page.js","sourceRoot":"","sources":["../../src/ope/page.tsx"],"names":[],"mappings":";AAAA,OAAO,aAAa,CAAA;AAGpB,OAAO,
|
|
1
|
+
{"version":3,"file":"page.js","sourceRoot":"","sources":["../../src/ope/page.tsx"],"names":[],"mappings":";AAAA,OAAO,aAAa,CAAA;AAGpB,OAAO,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAA;AAC1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,uCAAuC,CAAA;AAC3E,OAAO,EAAE,KAAK,EAA8B,MAAM,8BAA8B,CAAA;AAChF,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAyB,MAAM,kCAAkC,CAAA;AACtG,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAC1C,OAAO,UAAU,MAAM,4BAA4B,CAAA;AACnD,OAAO,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAA;AACrD,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAA;AAEvC,SAAS,SAAS,CAA+B,YAAoB,EAAE,YAAgB;IAEnF,IAAI,CAAC;QACD,MAAM,WAAW,GAAG,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;QACxE,OAAO,WAAW,IAAI,YAAqD,CAAA;IAC/E,CAAC;IAAC,MAAM,CAAC;QACL,OAAO,YAAqD,CAAA;IAChE,CAAC;AACL,CAAC;AAED,SAAS,cAAc,CAA+B,YAAoB,EAAE,YAAgB;IAExF,MAAM,WAAW,GAAG,SAAS,CAAC,YAAY,CAAC,CAAA;IAC3C,IAAI,CAAC,WAAW;QACZ,OAAO,YAAqD,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AAG,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAA;YACnD,MAAM,MAAM,GAAI,8BACZ,KAAC,MAAM,IAAC,GAAG,EAAE,IAAI,GAAG,CAAC,yBAAyB,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,QAAQ,EAAC,kBAAkB,GAAG,EAC5G,MAAC,MAAM,IAAC,MAAM,EAAG,WAAW,CAAC,MAAM,EAAE,IAAI,IAAI,EAAE,aAC3C,KAAC,UAAU,IAAC,OAAO,EAAG,YAAY,EAAG,IAAI,EAAG,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,YAC/E,KAAC,aAAa,KAAG,GACR,EACb,KAAC,UAAU,IAAC,WAAW,EAAG,WAAW,EAAG,WAAW,EAAG,WAAW,EAAG,YAAY,EAAG,WAAW,GAAK,IAC9F,EACT,eAAK,SAAS,EAAC,mBAAmB,8BAAgB,WAAW,CAAC,CAAC,CAAC,mBAAmB,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,qDAAqD,IAAQ,IACjK,CAAA;YACH,OAAO,MAAM,CAAA;QACjB,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACT,OAAO,CAAC,KAAK,CAAC,8BAA8B,EAAE,CAAC,CAAC,CAAA;YAChD,OAAO,QAAQ,EAAE,CAAA;QACrB,CAAC;IACL,CAAC;IACD,OAAO,QAAQ,CAAA;AACnB,CAAC;AAED,eAAe,uBAAuB,CAAA"}
|
package/dist/ope/types.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
3
|
-
import type
|
|
1
|
+
import { type ComponentType, type PropsWithChildren } from 'react';
|
|
2
|
+
import { type ClientFactory } from '@remkoj/optimizely-graph-client';
|
|
3
|
+
import { type GraphQLClient } from 'graphql-request';
|
|
4
|
+
import { type ContentQueryProps } from '@remkoj/optimizely-cms-react';
|
|
5
|
+
export { type ContentQueryProps } from '@remkoj/optimizely-cms-react';
|
|
4
6
|
type ServerPageProps = {
|
|
5
7
|
params: Record<string, string | Array<string>>;
|
|
6
8
|
searchParams: Record<string, string>;
|
|
@@ -44,30 +46,35 @@ export type EditViewOptions = {
|
|
|
44
46
|
* The base content loader to be used for the edit view
|
|
45
47
|
*/
|
|
46
48
|
loader: GetContentByIdMethod;
|
|
49
|
+
/**
|
|
50
|
+
* The factory used to create a new Optimizely Graph Client
|
|
51
|
+
*/
|
|
47
52
|
clientFactory: ClientFactory;
|
|
53
|
+
/**
|
|
54
|
+
* If provided, this allows to override the CommunicationInjector
|
|
55
|
+
* path.
|
|
56
|
+
*/
|
|
57
|
+
communicationInjectorPath: string;
|
|
48
58
|
};
|
|
49
|
-
export type
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
59
|
+
export type GetContentByIdData<LocaleType = string> = {
|
|
60
|
+
content: {
|
|
61
|
+
total: number;
|
|
62
|
+
items: Array<{
|
|
63
|
+
_metadata: {
|
|
64
|
+
key: string;
|
|
65
|
+
locale: LocaleType;
|
|
66
|
+
types: Array<string>;
|
|
67
|
+
displayName: string;
|
|
68
|
+
version?: string | null;
|
|
69
|
+
url?: {
|
|
70
|
+
base?: string | null;
|
|
71
|
+
hierarchical?: string | null;
|
|
72
|
+
default?: string | null;
|
|
73
|
+
} | null;
|
|
64
74
|
};
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
path?: string | null;
|
|
69
|
-
}[];
|
|
75
|
+
_type: string;
|
|
76
|
+
[fieldName: string]: any;
|
|
77
|
+
}>;
|
|
70
78
|
};
|
|
71
79
|
};
|
|
72
|
-
export type GetContentByIdMethod = (client: GraphQLClient, variables:
|
|
73
|
-
export {};
|
|
80
|
+
export type GetContentByIdMethod = <LocaleType = string>(client: GraphQLClient, variables: ContentQueryProps<LocaleType>) => Promise<GetContentByIdData<LocaleType>>;
|
package/dist/publish/index.d.ts
CHANGED
|
@@ -1,10 +1,27 @@
|
|
|
1
|
-
import { type NextRequest, NextResponse } from
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { type NextRequest, NextResponse } from "next/server";
|
|
2
|
+
import { revalidatePath } from "next/cache";
|
|
3
|
+
import { type IOptiGraphClient, type OptimizelyGraphConfig, type ClientFactory } from "@remkoj/optimizely-graph-client";
|
|
4
|
+
type PublishScopes = NonNullable<Parameters<typeof revalidatePath>[1]>;
|
|
5
|
+
type OptiGraphClientFactory = IOptiGraphClient | OptimizelyGraphConfig | ClientFactory;
|
|
6
|
+
type PublishApiOptions = {
|
|
7
|
+
/**
|
|
8
|
+
* The list of paths that your implementation uses with Optimizely CMS managed
|
|
9
|
+
* content
|
|
10
|
+
*/
|
|
11
|
+
paths: Array<string>;
|
|
12
|
+
/**
|
|
13
|
+
* The scopes for which to revalidate the cache
|
|
14
|
+
*/
|
|
15
|
+
scopes?: Array<PublishScopes>;
|
|
16
|
+
/**
|
|
17
|
+
* The tags to revalidate the cache for
|
|
18
|
+
*/
|
|
19
|
+
tags?: Array<string>;
|
|
20
|
+
/**
|
|
21
|
+
* The Optimizely Graph client to use for Graph Operations needed to publish
|
|
22
|
+
* content
|
|
23
|
+
*/
|
|
24
|
+
client?: OptiGraphClientFactory;
|
|
8
25
|
};
|
|
9
|
-
export declare function createPublishApi(
|
|
26
|
+
export declare function createPublishApi(options: PublishApiOptions): (request: NextRequest) => Promise<NextResponse>;
|
|
10
27
|
export default createPublishApi;
|
package/dist/publish/index.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { NextResponse } from
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
import { revalidatePath, revalidateTag } from "next/cache";
|
|
3
|
+
import { isContentGraphClient, ContentGraphClient } from "@remkoj/optimizely-graph-client";
|
|
2
4
|
import { getServerClient } from '../client';
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
export function createPublishApi(options) {
|
|
6
|
+
const defaultOptions = {
|
|
7
|
+
paths: [],
|
|
8
|
+
client: getServerClient,
|
|
9
|
+
scopes: ['page', 'layout'],
|
|
10
|
+
tags: []
|
|
11
|
+
};
|
|
9
12
|
function tryJsonParse(data) {
|
|
10
13
|
if (typeof (data) != 'string')
|
|
11
14
|
return null;
|
|
@@ -16,14 +19,19 @@ export function createPublishApi(client) {
|
|
|
16
19
|
return null;
|
|
17
20
|
}
|
|
18
21
|
}
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
const { client: clientFactory, paths, scopes, tags } = {
|
|
23
|
+
...defaultOptions,
|
|
24
|
+
...options
|
|
25
|
+
};
|
|
26
|
+
const client = typeof clientFactory == 'function' ? clientFactory() : (isContentGraphClient(clientFactory) ? clientFactory : new ContentGraphClient(clientFactory));
|
|
27
|
+
return async function (request) {
|
|
28
|
+
// Authorize the request
|
|
29
|
+
const xAuthToken = request.headers.get("X-OPTLY-PUBLISH") ??
|
|
30
|
+
request.nextUrl.searchParams.get("token") ??
|
|
23
31
|
undefined;
|
|
24
|
-
const serverToken =
|
|
32
|
+
const serverToken = client.siteInfo.publishToken;
|
|
25
33
|
if (!serverToken || serverToken == "") {
|
|
26
|
-
console.error("No authentication configured, publishing has been disabled");
|
|
34
|
+
console.error("[Publish-API] No authentication configured, publishing has been disabled");
|
|
27
35
|
return NextResponse.json({ error: "Not authorized" }, { status: 401 });
|
|
28
36
|
}
|
|
29
37
|
if (serverToken != xAuthToken) {
|
|
@@ -31,24 +39,24 @@ export function createPublishApi(client) {
|
|
|
31
39
|
return NextResponse.json({ error: "Not authorized" }, { status: 401 });
|
|
32
40
|
}
|
|
33
41
|
// Get request data
|
|
34
|
-
const requestBody = tryJsonParse(await
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
42
|
+
const requestBody = tryJsonParse(await request.text());
|
|
43
|
+
console.log("[Publish-API] Graph Event: " + JSON.stringify(requestBody, undefined, 4));
|
|
44
|
+
// Keep track of what has been revalidated
|
|
45
|
+
const revalidated = [];
|
|
46
|
+
// Flush all
|
|
47
|
+
scopes.forEach(scope => {
|
|
48
|
+
paths.forEach(path => {
|
|
49
|
+
revalidatePath(path, scope);
|
|
50
|
+
revalidated.push({ path, scope });
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
tags.forEach(tag => {
|
|
54
|
+
revalidateTag(tag);
|
|
55
|
+
revalidated.push({ tag });
|
|
56
|
+
});
|
|
57
|
+
// Return result
|
|
58
|
+
return NextResponse.json({ success: true, revalidated });
|
|
50
59
|
};
|
|
51
|
-
return publishHandler;
|
|
52
60
|
}
|
|
53
61
|
export default createPublishApi;
|
|
54
62
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/publish/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,YAAY,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/publish/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,YAAY,EAAE,MAAM,aAAa,CAAA;AAC5D,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAC1D,OAAO,EAAqD,oBAAoB,EAAsB,kBAAkB,EAAE,MAAM,iCAAiC,CAAA;AACjK,OAAO,EA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|
@@ -2,5 +2,5 @@ import type { IOptiGraphClient } from "@remkoj/optimizely-graph-client";
|
|
|
2
2
|
import type { CmsComponent, ContentLink } from "@remkoj/optimizely-cms-react";
|
|
3
3
|
import type { Metadata } from 'next';
|
|
4
4
|
export type OptimizelyNextPage<T = {}> = CmsComponent<T> & {
|
|
5
|
-
getMetaData?: (contentLink: ContentLink, locale: string, client: IOptiGraphClient) => Promise<Metadata>;
|
|
5
|
+
getMetaData?: (contentLink: ContentLink, locale: string | null | undefined, client: IOptiGraphClient) => Promise<Metadata>;
|
|
6
6
|
};
|