@nitrogenbuilder/connector-payload 0.1.29 → 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,9 +71,7 @@ export const NitrogenEditButtonRuntime = ({ collection }) => {
71
71
  const editorBase = instanceUrl ?? '';
72
72
  const isDevelopmentMode = isLocalAdminHost();
73
73
  const encodedAuthorId = encodeURIComponent(authorId);
74
- const developmentFlags = isDevelopmentMode
75
- ? '&development=true&editorDevelopment=true'
76
- : '';
74
+ const developmentFlags = isDevelopmentMode ? '&development=true' : '';
77
75
  const editHref = `${editorBase}/nitrogen-editor?token=${encodeURIComponent(token)}&collection=${encodeURIComponent(collection)}&pageId=${id}&authorId=${encodedAuthorId}&author=${encodedAuthorId}${developmentFlags}`;
78
76
  const viewHref = collection === 'nitrogen-templates'
79
77
  ? `${frontendUrl}/nitrogen-templates/${slug}`
@@ -95,10 +95,18 @@ export function createNitrogenEditorPage(config) {
95
95
  cssInjection: nitrogenConfig.cssInjection || "",
96
96
  };
97
97
  const editId = params.pageId;
98
- // Keep editor HMR opt-in separate from preview-target selection so a
99
- // deployed Payload instance can still preview against a local frontend.
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]";
100
106
  const isEditorDevelopment = params.editorDevelopment === "true";
101
- const isEditorDev = isEditorDevelopment && !!settings.instanceUrl;
107
+ const isEditorDev = !!settings.instanceUrl &&
108
+ (isEditorDevelopment ||
109
+ (isPreviewDevelopment && isLocalRequestHost));
102
110
  const editorOrigin = settings.instanceUrl?.replace(/\/$/, "") || "";
103
111
  const editorAssetsBase = isEditorDev
104
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
- dynamicData = buildDynamicData(associated.docs[0], settings);
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 relativePermalink = `/nitrogen-templates/${doc.slug}`;
47
- const frontendBase = (settings.frontendUrl || '').replace(/\/$/, '');
61
+ const previewTarget = getTemplatePreviewTarget(doc, settings, associatedCollection, associatedDoc);
48
62
  return Response.json({
49
63
  ...response,
50
- permalink: frontendBase ? `${frontendBase}${relativePermalink}` : relativePermalink,
51
- relative_permalink: relativePermalink,
64
+ slug: previewTarget.slug,
65
+ permalink: previewTarget.permalink,
66
+ relative_permalink: previewTarget.relative_permalink,
52
67
  postType: associatedCollection || '',
53
- headerTemplate: associatedCollection !== 'header' && associatedCollection !== 'footer'
54
- ? headerTemplate
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
- dynamicData = buildDynamicData(associated.docs[0], settings);
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 relativePermalink = `/nitrogen-templates/${doc.slug}`;
137
- const frontendBase = (settings.frontendUrl || '').replace(/\/$/, '');
147
+ const previewTarget = getTemplatePreviewTarget(doc, settings, associatedCollection, associatedDoc);
138
148
  return Response.json({
139
149
  ...response,
140
- permalink: frontendBase ? `${frontendBase}${relativePermalink}` : relativePermalink,
141
- relative_permalink: relativePermalink,
150
+ slug: previewTarget.slug,
151
+ permalink: previewTarget.permalink,
152
+ relative_permalink: previewTarget.relative_permalink,
142
153
  postType: associatedCollection || '',
143
- headerTemplate: associatedCollection !== 'header' && associatedCollection !== 'footer'
144
- ? headerTemplate
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitrogenbuilder/connector-payload",
3
- "version": "0.1.29",
3
+ "version": "0.1.30",
4
4
  "description": "Nitrogen page builder connector plugin for Payload CMS 3.x",
5
5
  "author": "Leonardo Dentzien <leo@torchmedia.ca>",
6
6
  "type": "module",