@nitrogenbuilder/connector-payload 0.1.28 → 0.1.30
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.
|
@@ -71,7 +71,8 @@ export const NitrogenEditButtonRuntime = ({ collection }) => {
|
|
|
71
71
|
const editorBase = instanceUrl ?? '';
|
|
72
72
|
const isDevelopmentMode = isLocalAdminHost();
|
|
73
73
|
const encodedAuthorId = encodeURIComponent(authorId);
|
|
74
|
-
const
|
|
74
|
+
const developmentFlags = isDevelopmentMode ? '&development=true' : '';
|
|
75
|
+
const editHref = `${editorBase}/nitrogen-editor?token=${encodeURIComponent(token)}&collection=${encodeURIComponent(collection)}&pageId=${id}&authorId=${encodedAuthorId}&author=${encodedAuthorId}${developmentFlags}`;
|
|
75
76
|
const viewHref = collection === 'nitrogen-templates'
|
|
76
77
|
? `${frontendUrl}/nitrogen-templates/${slug}`
|
|
77
78
|
: `${frontendUrl}/${slug}`;
|
|
@@ -73,8 +73,10 @@ export function createNitrogenEditorPage(config) {
|
|
|
73
73
|
const settings = (await payload.findGlobal({
|
|
74
74
|
slug: "nitrogen-settings",
|
|
75
75
|
}));
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
// `development=true` selects the preview target (`developmentUrl`) without
|
|
77
|
+
// requiring the editor shell itself to come from a local Vite server.
|
|
78
|
+
const isPreviewDevelopment = params.development === "true";
|
|
79
|
+
const siteUrl = getSiteUrl(settings, isPreviewDevelopment);
|
|
78
80
|
const nitrogenConfig = settings.nitrogenConfig || {};
|
|
79
81
|
if (!params.pageId || !params.collection) {
|
|
80
82
|
return redirect("/admin");
|
|
@@ -84,7 +86,7 @@ export function createNitrogenEditorPage(config) {
|
|
|
84
86
|
licenseKey: settings.licenseKey || "",
|
|
85
87
|
provider: {
|
|
86
88
|
type: "payload",
|
|
87
|
-
apiUrl: getNitrogenApiUrl(
|
|
89
|
+
apiUrl: getNitrogenApiUrl(isPreviewDevelopment),
|
|
88
90
|
collection: params.collection,
|
|
89
91
|
},
|
|
90
92
|
siteUrl: siteUrl || "",
|
|
@@ -93,7 +95,18 @@ export function createNitrogenEditorPage(config) {
|
|
|
93
95
|
cssInjection: nitrogenConfig.cssInjection || "",
|
|
94
96
|
};
|
|
95
97
|
const editId = params.pageId;
|
|
96
|
-
|
|
98
|
+
// Preserve the old localhost flow: `development=true` should still imply
|
|
99
|
+
// local editor HMR when the editor page itself is served from localhost.
|
|
100
|
+
// Deployed hosts stay on the built editor unless explicitly opted in.
|
|
101
|
+
const requestHost = headersList.get("host") || "";
|
|
102
|
+
const requestHostname = requestHost.split(":")[0];
|
|
103
|
+
const isLocalRequestHost = requestHostname === "localhost" ||
|
|
104
|
+
requestHostname === "127.0.0.1" ||
|
|
105
|
+
requestHostname === "[::1]";
|
|
106
|
+
const isEditorDevelopment = params.editorDevelopment === "true";
|
|
107
|
+
const isEditorDev = !!settings.instanceUrl &&
|
|
108
|
+
(isEditorDevelopment ||
|
|
109
|
+
(isPreviewDevelopment && isLocalRequestHost));
|
|
97
110
|
const editorOrigin = settings.instanceUrl?.replace(/\/$/, "") || "";
|
|
98
111
|
const editorAssetsBase = isEditorDev
|
|
99
112
|
? editorOrigin
|
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
import { getNitrogenSettings, buildDynamicData, buildPageResponse, buildListItemResponse, getTemplateForType, requireAuth, } from './helpers';
|
|
2
|
+
import { buildPermalink, buildRelativePermalink } from '../collection-registry';
|
|
3
|
+
function isContentTemplateCollection(collection) {
|
|
4
|
+
return !!collection && collection !== 'header' && collection !== 'footer';
|
|
5
|
+
}
|
|
6
|
+
function getTemplatePreviewTarget(doc, settings, associatedCollection, associatedDoc) {
|
|
7
|
+
const previewCollection = associatedDoc && isContentTemplateCollection(associatedCollection)
|
|
8
|
+
? associatedCollection
|
|
9
|
+
: 'nitrogen-templates';
|
|
10
|
+
const previewSlug = String(associatedDoc?.slug || doc.slug || '');
|
|
11
|
+
return {
|
|
12
|
+
slug: previewSlug,
|
|
13
|
+
permalink: buildPermalink(previewCollection, previewSlug, settings.frontendUrl),
|
|
14
|
+
relative_permalink: buildRelativePermalink(previewCollection, previewSlug),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
2
17
|
export const templatesEndpoints = [
|
|
3
18
|
// GET /api/nitrogen/v1/nitrogen-templates/slug/:slug — Get a single template by slug
|
|
4
19
|
{
|
|
@@ -24,10 +39,9 @@ export const templatesEndpoints = [
|
|
|
24
39
|
}
|
|
25
40
|
const settings = await getNitrogenSettings(payload);
|
|
26
41
|
let dynamicData = buildDynamicData(doc, settings);
|
|
42
|
+
let associatedDoc;
|
|
27
43
|
const associatedCollection = doc.associatedCollection;
|
|
28
|
-
if (associatedCollection
|
|
29
|
-
associatedCollection !== 'header' &&
|
|
30
|
-
associatedCollection !== 'footer') {
|
|
44
|
+
if (isContentTemplateCollection(associatedCollection)) {
|
|
31
45
|
try {
|
|
32
46
|
const associated = await payload.find({
|
|
33
47
|
collection: associatedCollection,
|
|
@@ -35,7 +49,8 @@ export const templatesEndpoints = [
|
|
|
35
49
|
depth: 1,
|
|
36
50
|
});
|
|
37
51
|
if (associated.docs.length) {
|
|
38
|
-
|
|
52
|
+
associatedDoc = associated.docs[0];
|
|
53
|
+
dynamicData = buildDynamicData(associatedDoc, settings);
|
|
39
54
|
}
|
|
40
55
|
}
|
|
41
56
|
catch {
|
|
@@ -43,19 +58,15 @@ export const templatesEndpoints = [
|
|
|
43
58
|
}
|
|
44
59
|
}
|
|
45
60
|
const response = buildPageResponse(doc, settings, dynamicData);
|
|
46
|
-
const
|
|
47
|
-
const frontendBase = (settings.frontendUrl || '').replace(/\/$/, '');
|
|
61
|
+
const previewTarget = getTemplatePreviewTarget(doc, settings, associatedCollection, associatedDoc);
|
|
48
62
|
return Response.json({
|
|
49
63
|
...response,
|
|
50
|
-
|
|
51
|
-
|
|
64
|
+
slug: previewTarget.slug,
|
|
65
|
+
permalink: previewTarget.permalink,
|
|
66
|
+
relative_permalink: previewTarget.relative_permalink,
|
|
52
67
|
postType: associatedCollection || '',
|
|
53
|
-
headerTemplate: associatedCollection
|
|
54
|
-
|
|
55
|
-
: null,
|
|
56
|
-
footerTemplate: associatedCollection !== 'header' && associatedCollection !== 'footer'
|
|
57
|
-
? footerTemplate
|
|
58
|
-
: null,
|
|
68
|
+
headerTemplate: isContentTemplateCollection(associatedCollection) ? headerTemplate : null,
|
|
69
|
+
footerTemplate: isContentTemplateCollection(associatedCollection) ? footerTemplate : null,
|
|
59
70
|
});
|
|
60
71
|
}
|
|
61
72
|
catch {
|
|
@@ -114,10 +125,9 @@ export const templatesEndpoints = [
|
|
|
114
125
|
// Build dynamic data from the first document of the associated collection
|
|
115
126
|
// so the template preview has real data to bind to
|
|
116
127
|
let dynamicData = buildDynamicData(doc, settings);
|
|
128
|
+
let associatedDoc;
|
|
117
129
|
const associatedCollection = doc.associatedCollection;
|
|
118
|
-
if (associatedCollection
|
|
119
|
-
associatedCollection !== 'header' &&
|
|
120
|
-
associatedCollection !== 'footer') {
|
|
130
|
+
if (isContentTemplateCollection(associatedCollection)) {
|
|
121
131
|
try {
|
|
122
132
|
const associated = await payload.find({
|
|
123
133
|
collection: associatedCollection,
|
|
@@ -125,7 +135,8 @@ export const templatesEndpoints = [
|
|
|
125
135
|
depth: 1,
|
|
126
136
|
});
|
|
127
137
|
if (associated.docs.length) {
|
|
128
|
-
|
|
138
|
+
associatedDoc = associated.docs[0];
|
|
139
|
+
dynamicData = buildDynamicData(associatedDoc, settings);
|
|
129
140
|
}
|
|
130
141
|
}
|
|
131
142
|
catch {
|
|
@@ -133,19 +144,15 @@ export const templatesEndpoints = [
|
|
|
133
144
|
}
|
|
134
145
|
}
|
|
135
146
|
const response = buildPageResponse(doc, settings, dynamicData);
|
|
136
|
-
const
|
|
137
|
-
const frontendBase = (settings.frontendUrl || '').replace(/\/$/, '');
|
|
147
|
+
const previewTarget = getTemplatePreviewTarget(doc, settings, associatedCollection, associatedDoc);
|
|
138
148
|
return Response.json({
|
|
139
149
|
...response,
|
|
140
|
-
|
|
141
|
-
|
|
150
|
+
slug: previewTarget.slug,
|
|
151
|
+
permalink: previewTarget.permalink,
|
|
152
|
+
relative_permalink: previewTarget.relative_permalink,
|
|
142
153
|
postType: associatedCollection || '',
|
|
143
|
-
headerTemplate: associatedCollection
|
|
144
|
-
|
|
145
|
-
: null,
|
|
146
|
-
footerTemplate: associatedCollection !== 'header' && associatedCollection !== 'footer'
|
|
147
|
-
? footerTemplate
|
|
148
|
-
: null,
|
|
154
|
+
headerTemplate: isContentTemplateCollection(associatedCollection) ? headerTemplate : null,
|
|
155
|
+
footerTemplate: isContentTemplateCollection(associatedCollection) ? footerTemplate : null,
|
|
149
156
|
});
|
|
150
157
|
}
|
|
151
158
|
catch {
|
package/dist/index.js
CHANGED
|
@@ -55,6 +55,11 @@ export const nitrogenConnectorPlugin = (options = {}) => (incomingConfig) => {
|
|
|
55
55
|
// Register templates collection
|
|
56
56
|
registerCollection("nitrogen-templates", "nitrogen-templates", "/nitrogen-templates/[slug]");
|
|
57
57
|
registerCollection("nitrogen_template", "nitrogen-templates", "/nitrogen-templates/[slug]");
|
|
58
|
+
// Register route patterns for non-editable collections too so templates can
|
|
59
|
+
// preview against real frontend URLs for their associated collections.
|
|
60
|
+
for (const [slug, routePattern] of Object.entries(options.collectionRoutes || {})) {
|
|
61
|
+
registerCollection(slug, slug, routePattern);
|
|
62
|
+
}
|
|
58
63
|
// Process collections
|
|
59
64
|
const additionalCollections = options.collections || [];
|
|
60
65
|
for (const slug of additionalCollections) {
|
package/package.json
CHANGED